A. php include 能包含远程文件吗
可以,但是需要修改配置程序。具体如下:
最好检查一下php.ini中的配置选项allow_url_include,如果为on则可以包含,否则不能包含
; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
allow_url_include = Off
做个简单的测试,此时allow_url_include的值为Off
测试前配置一下hosts文件,这样可以在一台电脑上面进行模拟测试
192.168.1.101 www.test1.com
192.168.1.102 www.test2.com
path.php文件内容为:
<?php
echo "This is file path.php<br />\n";
include("http://www.test2.com/research/path/path.php");
?>
path1.php文件内容为:
<?php
echo "This is file path1.php in root directory\n";
?>
执行http://www.test1.com/research/path/path.php,输出如下
This is file path.php
Warning: include() [function.include]: URL file-access is disabled in the server configuration in E:\myphp\research\path\path.php on line 3
Warning: include(http://www.test2.com/research/path/path.php) [function.include]: failed to open stream: no suitable wrapper could be found in E:\myphp\research\path\path.php on line 3
Warning: include() [function.include]: Failed opening 'http://www.test2.com/research/path/path.php' for inclusion (include_path='.;C:\php5\pear') in E:\myphp\research\path\path.php on line 3
将php.ini中的allow_url_include改为On,重新启动web服务器,再次执行http://www.test1.com/research/path/path.php,输出如下:
This is file path.php
This is file path1.php in root directory
将allow_url_include设为On以后,就可以包含远程文件了,并且包含的是远程文件执行的结果。
B. 用php程序自动读取远程文件并更新到本地,每天一次,如何做
windows:
准备:
1.将 php.exe 的路径加入 windows 的环境变量
2.编写文件:
D:\fileGeter.php
<?php
$filelist = Array(
"http://**********/a.txt",
"http://**********/b.txt",
);
$saveas="D:\\" ;
$endl = ".txt"
function getfile(){
foreach( $filelist as $k => $file )
file_put_contents( $saveas . $k . $endl , file_get_contents( $file ) ) ;
}
getfile();
?>
3.执行cmd命令
at 11:20 /every:1,2,3,4,5,6,7 "php D:\fileGeter.php"
linux 更方便
直接把此文件包含进 你要写的程序里就OK了,
fileGeter.php:
<?php
...
...
$saveas = "./";
...
..
?>
index.php:
<?php
require_once("fileGeter.php");
//and so on .....
.....
....
....
?>
C. php代码保存远程图片到本地,出现重复建立文件夹,是怎么回事出现嵌套建立文件夹
没用过php,但如果出现这个问题就一定是if(file_exists($fname))判断出的错,试试将
$fname='yong/'.$id;改成
$fname='/yong/'.$id;试试看
或者要检查的话,就在if(file_exists($fname)){ 上面加一行输出一下file_exists($fname)看看每次判断的结果。
D. php 函数的远程保存图片问题
我一般不用,直接使用file_get_contents()取得文件内容,再写入本地文件中去
E. 急求PHP源码,能够远程下载其他网站的文件并保存到自己网站的目录里或者指定目录,谢谢高手!
可以很负责人的告诉你 没有 要下载其他网站的文件,只能下载如RAR HTML等文件,不能下载ASP PHP JSP等脚本文件
F. thinkphp用file_put_contents()保存远程图片到服务器
你在逗我,怎么可能直接用程序直接写入另一个服务器。
我的思路是(没有试验过)
转换为图片为二进制文件进行上传到另一个服务器进行处理保存
转换图片为base64 用curl请求服务器进行处理
模拟表单请求
G. php保存远程文件到文件夹
具体看步骤吧:
function getFile($url,$save_dir='',$filename='',$type=0){
if(trim($url)==''){
return false;
}
if(trim($save_dir)==''){
$save_dir='./';
}
if(0!==strrpos($save_dir,'/')){
$save_dir.='/';
}
//创建保存目录
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return false;
}
//获取远程文件所采用的方法
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$content=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$content=ob_get_contents();
ob_end_clean();
}
$size=strlen($content);
//文件大小
$fp2=@fopen($save_dir.$filename,'a');
fwrite($fp2,$content);
fclose($fp2);
unset($content,$url);
return array('file_name'=>$filename,'save_path'=>$save_dir.$filename);
}
getFile($url,$save_dir,$filename,1)//调用
H. php如何把远程xml文件写到本地保存,比如知道远程xml文件的URL:http://api.t.sina.com.cn/users/show.xml
可以用('http://api.t.sina.com.cn/users/show.xml', 'e:/xml/show.xml');
这个函数
I. php保存远程图片到本地
$img = file_get_contents('http://www.91cici.com/images/logo.gif');
file_put_contents('test.gif',$img);
J. php curl get 下载远程zip文件保存在本地例子
<?php
if($_POST['submit']){
$url=$_POST['url']; //取得提交过来的地址http://hu60.cn/wap/0wap/addown.php/fetion_sms.zip
$url=urldecode($url);
$fname=basename("$url"); //返回路径中的文件名部分 fetion_sms.zip
$str_name=pathinfo($fname); //以数组的形式返回文件路径的信息
$extname=strtolower($str_name['extension']); //把扩展名转换成小写
//$uptypes=explode(",",$forum_upload); //取得可以上传的文件格式
//$size=getFileSize($url);
$time=date("Ymd",time());
$upload_dir="./upload/";//上传的路径
$file_name=$time.rand(1000,9999).'.'.$fname;
$dir=$upload_dir.$file_name;//创建上传目录
//判断目录是否存在 不存在则创建
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777,true);
}
$contents=curl_download($url,$dir);
if($contents){
echo "下载成功";
}else{
echo "下载失败";
}
}
function curl_download($url, $dir) {
$ch = curl_init($url);
$fp = fopen($dir, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res=curl_exec($ch);
curl_close($ch);
fclose($fp);
return $res;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>远程下载文件</title>
<form name="upform" method="post" action="" enctype='multipart/form-data'>
<input name='url' type='text' size='20'/>
<input type='submit' name='submit' value='远程下载'/>
</form>
</body>
</html>