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>