dongtuanzi1080
2015-10-22 20:47
浏览 152
已采纳

使用Web浏览器下载文件的PHP脚本

I am working on a small project to download files from a ftp server using a web based query. I have create a HTML form as a front end, which takes from user the ftp server name and the file names, and then interacts with the php script, to connect with ftp server and download the files specified. I have kept both the html and php files on my university webserver. I am able to download files on the machine running the webserver when I run the PHP script directly form the command line from the server. But I am not able to download files on my local computer using a web browser.

| Web-browser running on local machine | <--not able to download file--> | My PHP script running on web-server machine | <--Able to download file-->| FTP Server |

<?php
$targetFile = 'file.txt';
$curl = curl_init();
$fh = fopen(dirname(__FILE__) . '/'.$targetFile,'w+b');
if ($fh == FALSE){ 
      print "File not opened<br>"; 
           exit; 
}

echo "<br>configuring curl...<br>";

curl_setopt($curl, CURLOPT_URL, "ftp://full_path_name_of_file");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_exec($curl);
echo curl_error($curl);
curl_close($curl);
fclose($fh);
?>

File is downloaded successfully when I run this php script from the command line from the server machine. But when I invoke the script from the web browser using my personal machine, I get an error "File not opened".

Can you please tell is there any way I can download the file via my web browser.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个小项目,使用基于Web的查询从ftp服务器下载文件。 我创建了一个HTML表单作为前端,它从用户获取ftp服务器名称和文件名,然后与php脚本交互,连接ftp服务器并下载指定的文件。 我在我的大学网络服务器上保存了html和php文件。 当我从服务器直接从命令行运行PHP脚本时,我能够在运行Web服务器的机器上下载文件。 但我无法使用网络浏览器在本地计算机上下载文件。

| 在本地机器上运行的Web浏览器 &lt; - 无法下载文件 - &gt; | 我的PHP脚本在web服务器机器上运行 &lt; - 能够下载文件 - &gt; | FTP服务器|

 &lt;?php 
 $ targetFile ='file.txt'; 
 $ curl = curl_init(); 
 $ fh = fopen(dirname(  __FILE__)。'/'。$ targetFile,'w + b'); 
nif($ fh == FALSE){
 print“文件未打开&lt; br&gt;”;  
退出;  
} 
 
echo“&lt; br&gt;配置curl ...&lt; br&gt;”; 
 
 ncurl_setopt($ curl,CURLOPT_URL,“ftp:// full_path_name_of_file”); 
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,  1); 
curl_setopt($ curl,CURLOPT_HEADER,0); 
curl_setopt($ curl,CURLOPT_VERBOSE,1); 
curl_setopt($ curl,CURLOPT_FILE,$ fh); 
curl_exec($ curl); 
echo curl_error($ curl  ); 
; ncurl_close($ curl); 
 nclclose($ fh); 
?&gt; 
   
 
 

从我运行此php脚本时,文件已成功下载 来自服务器机器的命令行。 但是当我使用我的个人计算机从Web浏览器调用脚本时,出现错误“文件未打开”。

请告诉我有什么方法可以通过网络浏览器下载文件。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzuita7325 2015-10-22 21:46
    已采纳
    1. this might be a file ownership issue

      Please check the permission and the ownership of the file.

      In order to debug this a bit better, you might use parts of the script provided here:

      https://stackoverflow.com/a/10377285/1163786

    2. check the php configuration

      there is a difference between the php configuration of the CLI and the one for the webserver. the later might have some restrictions, when compared to the CLI one. please compare or diff the files (to see the configuration difference).

    3. the download itself is not initiated

      The script downloads a file via curl from a ftp server and stores it to a folder on your webserver, but your are not pointing the browser (client) to the downloaded file nor initiating a downstream to the browser from the script.

      I would add a check, that the script is not called from the CLI and then do a header forward to the downloaded file.

      if(PHP_SAPI !== 'cli') {
          header("Location: WWW_URL_SERVER . $path_to . $targetFile);
      }
      

      You can test the download standalone by using the "direct link" in your browser to initiate a download. This is again a permission thing, this time the webserver itself serves the static file and needs permission to do so.

      Referencing: Redirect page after process complete in PHP

    点赞 打赏 评论

相关推荐 更多相似问题