tags:

views:

47

answers:

2

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 absolutely 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

A: 

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

Kerry
i don't know how to provide a link to the file. it actually isn't a link to the file itself on the server but more the name of the path. but it doesn't point to the file on the server. i'm connecting via ftp_connect. i have no idea how i can point to the file on the server. the server is protected with username and password.
+1  A: 

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.

nute
i've already tried to do that. i've just no idea how to use ftp_get. what's the local file i have to declare? i don't want to declare a local file. it should just take the server-file and download it to any harddrive.
Download the file "temporarily" to your server first. The file has to make a pit-stop to your server, you can't bypass. Then using that "header" feature and the "readFile" function, the user will be able to immediately download the file to his local hard drive.
nute
wow, thank you! it's almost working. somehow the only problem seems to be that i can't open the file locally. it saves it under the same name (with the right suffix). however every file i download is kind of broken. any idea what that could be?
It means you're not actually saving the data properly to your server (0 file size) or the file you're trying to open using readfile is either a bad reference or isn't able to read it.
jerebear
i think somehow the filetype is wrong. e.g. if i download a .jpg the firefox prompt tells me the type of the file is Hyper Text Markup Language. Google Chrome even adds an additional .jpg.html at the end of the file. ?