File selection for ftp download.

Hi,
I have got my code to display a list of files on my ftp server but i can’t work out how to get the file name
with the mouse cursor so i can create a download link and been a newbie well :frowning: so any help would be apprieciated.
This is my main code

[php]

Welcome To Your Update Page <?php

if (isset($_SESSION[‘username’]))
{
echo $_SESSION[‘fullname’];
}
else
{
echo ‘Not logged in’;
}
?>

<?php ?>
<?php

$ftpServer = “ftp.microinovations.co.uk”;
$ftpUser = $_SESSION[‘username’];
$ftpPass = $_SESSION[‘password’];

$conn = @ftp_connect($ftpServer)
or die(“Couldn’t connect to FTP server”);

$login = @ftp_login($conn,$ftpUser,$ftpPass)
or die(“Login credentials were rejected”);
$workingDir = ftp_pwd($conn);
$fList = @ftp_nlist($conn, $workingDir);
if(is_array($fList))
{
for($i = 0; $i < sizeof($fList); $i++)
{
echo $fList[$i] . “
”;
}
}
else
{
echo “$workingDir contains no files.”;
}
?>
[/php]

Well just a pointer in the right direction will do.
I have gone through the php manual put i can’t find an instruction that will create a link from a file name when a mouse over occurs.

in your for loop

try to use this: replace scriptName.php with the name of your script php file
[php]
echo “<a href=scriptName.php?id=$i>”$fList[$i]."
";
[/php]
this should create a link a with GET ID on your script get the ID using GET and then download it

hope it works didnt test it

Hi,
Thanks for reply, i get a blank page when i change it to that :o
Any ideas?

yeah my mistake

[php]
echo “<a href=scriptName.php?id=$i>”.$fList[$i]."
";
[/php]

Thanks that works great now ;D

your welcome

i’m glad that it works

Hello peoples,
I can’t get me head round how the “get” and “id” bit’s of php work so as to download a file.
The highlight part works fine (thanks to wilson382) put i can’t seem to get an id link to the file that
is highlighted so could do with a bit of help ::slight_smile:

Well, if you are asking how to set up a link for someone to download a file when it is clicked on, you just point to the file instead of another page.

So, if you have a folder on the server called, let’s say “Files” and you have a list of files in that folder,
you would list the files as anchors with HREF’s pointing to each file. You could pull the list of filenames
using the DIR function and load it into an array and then display all the files as anchors/href’s. Or, if the
filenames do not change, you can just list then.

Something like this is a sample of how the output would look: (3 file links for an example…)
echo “Download the image here…
”;
echo “Download the full PDF here…
”;
echo “Download the zipped versions here…
”;
As you see, you just point to the file you want to allow to be downloaded instead of sending the user
to the page in the link.

Hope that is what you are asking for… Good luck, let us know…

Hi thanks for the quick reply,
I am trying to create download links from the below code.

[php]echo “<a href=scriptName.php?id=$i>”$fList[$i]."
";[/php]

Which highlights the file ok on mouseover but i don’t know how to get the “id” bit to work
to create a link to any file that is clicked on.

Perhaps I was not clear. YOUR link points to a PHP program file, not the actual file you want to download.
I explained you must change the HREF section to point to the file itself not another PHP page. I thought that
was enough for you to figure it out. But, anyway, your current link:
echo “<a href=scriptName.php?id=$i>”$fList[$i]."
";
Says to use the “flist” for the link number $i and when clicked, go to another page called “scriptName.php”
and do whatever it wants to do…

Instead, to download a file, you must replace the redirect to another PHP page with the actual filename.
Something like this should work for you:
[php]
echo “” . $fList[$i] . “
”;
[/php]
This line says to open/save the filename in $fList[$i] when clicked. Note that I used quotes around the HREF in case there are any special chars in the filename.

Hope that is what you need. Let us know either way…

Hi,
Thanks for the help as i am very new to php.
The files are in random folders depending who logs in, the root folder been “upgrades” i have but the root
folder in your code and it works fine, but i get a “file not found” due to the link not including the sub folder
which i am trying to get from “$workingDir” this is what i have up to now.

[php]echo “<a href=upgrades/$workingDir/’” . $fList[$i] . “’>” . $fList[$i] . “
”;[/php]

Where have i gone wrong?

[php]
echo “<a href=upgrades/$workingDir/’” . $fList[$i] . “’>” . $fList[$i] . “
”;
[/php]
the above code didnt work?

No, i just get :o

The requested URL /upgrades///'psu2410fs.pdf' was not found on this server.

It should be “upgrades/usp/psu2410fs.pdf” the folder “usp” is variable depending on login info
i am trying to get the variable folder from,

[php]$workingDir = ftp_pwd($conn);[/php]

what have i done wrong?

Also, is your webpage code in the same folder that the “upgrades” folder is in? If not, you will need to back up a level or two to get to it. Hope that makes sense… To back up a level, you use “…/” for the first folder level…

Ok i did that but i still get,

The requested URL /upgrades///'psu2410fs.pdf' was not found on this server.

?

Well, that error shows that you are not getting the correct folder from your database.
Also, you start the URL with a slash. That means that you have the “upgrades” folder in the root of your site.
Is this true? If not, remove the starting slash. If this does not work for you, please repost your code again.

I am sure it is just the code pulling the folder name from the database. By the error you posted, the
folder names inside the “upgrades” folder is missing from the URL… Good luck…

No “upgrades” is under the root like (root/upgrades/" then the variable folder under them.
This is the code that i have upto now.

[php]$login = @ftp_login($conn,$ftpUser,$ftpPass)
or die(“Login credentials were rejected”);

$workingDir = ftp_pwd($conn);

$fList = @ftp_nlist($conn, $workingDir);

if(is_array($fList))
{
for($i = 0; $i < sizeof($fList); $i++)
{

echo “<a href=upgrades/$workingDir/” . $fList[$i] . “>” . $fList[$i] . “
”;

}
}
else
{
echo “$workingDir contains no files.”;
}

?> [/php]

If i put directory name in the code as below it downloads the files ok

[php]echo “<a href=upgrades/usp/” . $fList[$i] . “>” . $fList[$i] . “
”;[/php]

it’s not getting the directory under “upgrades/” from $workingDir for some reason.

did you try echoing $workingDir to see if is contain any value?

I just get a “/”

Sponsor our Newsletter | Privacy Policy | Terms of Service