2012-06-05 73 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']等 –