2012-06-05 69 views
1

我只是想知道。所以這 我有2臺電腦,其中一臺安裝了xampp,另一臺不是。(同一網絡)。都是Windows XP用php下載文件

和我編寫這個測試腳本下載文件。

<?php 
$txt = "http://www.branded3.com/wp-content/uploads/2011/05/Google_Chrome1.jpg"; 
$img = "01.jpg"; 
file_put_contents($img, file_get_contents($txt)); 
?> 

我在我的電腦上運行腳本,該腳本安裝了xampp,並且其工作正常。

但我在另一臺電腦上運行它,不工作。 有人能幫我解決這個問題嗎?

+1

這一個讓我笑了起來。 ; p ....你是否期望它在客戶端機器上下載文件? –

+0

是的,這是不可能的? –

+0

如果不是xampp,你在另一臺電腦上運行的是什麼? – 2012-06-05 09:33:50

回答

1

繼承人一個非常粗糙的方式,你可以代理圖像並提示下載。

<?php 
$url = "http://www.branded3.com/wp-content/uploads/2011/05/Google_Chrome1.jpg"; 

//Get file 
$source = file_get_contents($url); 

//Image Mime types 
$images = array('jpg'=>'image/jpg','png'=>'image/png','png'=>'image/png'); 
//Is it an image extention 
if(in_array(substr($url,-3),$images)){ 
    $type = $images[substr($url,-3)]; 
}else{ 
    //No its somthing else 
    $type = 'application/octet-stream'; 
} 

//Set the headers 
header('Content-Description: File Transfer'); 
header('Content-Type: '.$type); 
header('Content-Disposition: attachment; filename='.basename($url)); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: ' . sprintf("%u", strlen($source))); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Expires: 0'); 
header('Pragma: public'); 

//echo the source 
echo $source; 
?> 
+0

你做到了:)非常感謝你!!!! 但是我如何指定下載的路徑?並在Firefox或資源管理器(自動下載)時忽略下載窗口。 –

+0

我不工作,你不能在沒有提示下載的情況下將文件放在客戶端計算機上,請考慮安全隱患。通過指定路徑,您可以使用$ _GET ['url']等 –