开发者

PHP: prompting download from ftp?

开发者 https://www.devze.com 2023-01-07 14:09 出处:网络
hy guys, i really need your help. i\'ve succesfully connected to ftp server via php. i\'m listing all files that are on the server. if i click a file the browser should prompt a download window to dow

hy guys, i really need your help. i've succesfully connected to ftp server via php. i'm listing all files that are on the server. if i click a file the browser should prompt a download window to download the file.

i've ab开发者_高级运维solutely no idea how to do that. which method am i going to use. ftp_get kind of confuses me. it says i have to declare a local_file as well. i just want a file on the server to download to my harddrive.

how can i do that?

regards matt


The remote file has to first be downloaded to your server before you can send it to the user. It's invisible to the user, but you don't have a choice. PHP won't let the browser talk directly to the FTP server.

Create a separate php script that calls ftp_get for a specific file, stores it temporarily to your server to allow the user to download it.

Something like:

<?php
//assume the page was called like download.php?filename=downloaded.pdf
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
$tempFile = 'temp'.rand();
ftp_get($ftp, $tempFile, $_GET['filename'], FTP_BINARY);

readfile($tempFile);

You may add code to delete the tempFile too.


If you provide a link to a file that can't be read by the browser (such as a php file, audio, video, etc.) it will ask you to download the file.

The other way is to use PHP headers on a page and print out the page, and link to that page. http://www.ryboe.com/tutorials/php-headers-force-download

0

精彩评论

暂无评论...
验证码 换一张
取 消