s1dest3pnate Posted November 4, 2009 Share Posted November 4, 2009 Hello. First off, I am not a PHP coder, I need y'alls help for something that I would think is fairly simple but of course, I wouldn't know. I want to use PHP in a web page to connect to an FTP login and obtain the listing of a directory. I've actually already accomplished this with the code shown below. However, my problem is that I also need this listing of files from the directory to each be downloadable. Just so you know what's going on, there are files generated on my FTP server every couple minutes so I want this web page to give access to that FTP folder (real-time) and allow people to download any one of the files that they'd like by simply clicking on the file and getting the Save As download prompt. Since I don't know much about PHP, if you could modify my code below to include the downloading capability, that would be great. Thanks in advance everyone! <pre> <?php $ftp_server = "localhost"; $ftp_user = "username"; $ftp_pass = "password; // set up a connection or die $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); // try to login if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) { echo "Connected as $ftp_user@$ftp_server\n"; } else { echo "Couldn't connect as $ftp_user\n"; } // change to ftp_rawlist() if you want all the file details: $result = ftp_nlist($conn_id, "public_html"); if(is_array($result)) { foreach($result as $line) { echo "$line\n"; } } else { echo "No data returned."; } // close the connection ftp_close($conn_id); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/180251-how-to-ftp-list-directory-and-each-listed-item-be-clickable-to-download/ Share on other sites More sharing options...
GingerRobot Posted November 4, 2009 Share Posted November 4, 2009 I've not done this before, but i think what you're going to have to do is download the file using ftp_get to a temporary file on your webserver before offering it to the user for download. Link to comment https://forums.phpfreaks.com/topic/180251-how-to-ftp-list-directory-and-each-listed-item-be-clickable-to-download/#findComment-950896 Share on other sites More sharing options...
seanlim Posted November 4, 2009 Share Posted November 4, 2009 To make the files "clickable", add <A> tags to the $line output, for example: echo "<a href=\"".$_SERVER['PHP_SELF']."?file=".$line."\">$line</a>\n"; then, at the start of your script, handle the GET request and use ftp_get to download the file. Link to comment https://forums.phpfreaks.com/topic/180251-how-to-ftp-list-directory-and-each-listed-item-be-clickable-to-download/#findComment-950941 Share on other sites More sharing options...
s1dest3pnate Posted November 4, 2009 Author Share Posted November 4, 2009 To make the files "clickable", add <A> tags to the $line output, for example: echo "<a href=\"".$_SERVER['PHP_SELF']."?file=".$line."\">$line</a>\n"; then, at the start of your script, handle the GET request and use ftp_get to download the file. So this would work without having to specify each file's name right since a new file is generated every couple minutes? Could you modify the code that I attached above to include these additions? I would so really appreciate it. Link to comment https://forums.phpfreaks.com/topic/180251-how-to-ftp-list-directory-and-each-listed-item-be-clickable-to-download/#findComment-951302 Share on other sites More sharing options...
seanlim Posted November 5, 2009 Share Posted November 5, 2009 <?php $ftp_server = "localhost"; $ftp_user = "username"; $ftp_pass = "password; // set up a connection or die $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); // try to login if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) echo "Connected as $ftp_user@$ftp_server\n"; else echo "Couldn't connect as $ftp_user\n"; if(isset($_GET['file'])) ftp_get(...) else{ echo "<pre>"; // change to ftp_rawlist() if you want all the file details: $result = ftp_nlist($conn_id, "public_html"); if(is_array($result)) { foreach($result as $line) echo '<a href="'.$_SERVER['PHP_SELF'].'?file='.$line.'">'.$line.'</a>\n'; } else echo "No data returned."; echo "</pre>"; } // close the connection ftp_close($conn_id); ?> The logic's there, just place the proper parameters into ftp_get(...). I'm too lazy to read the API. All the best. Link to comment https://forums.phpfreaks.com/topic/180251-how-to-ftp-list-directory-and-each-listed-item-be-clickable-to-download/#findComment-951638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.