Jump to content

How to FTP list directory and each listed item be clickable to download


s1dest3pnate

Recommended Posts

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>

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.

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.

<?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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.