File selection for ftp download.

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 “/”

well now you know why this does not work

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

where have i gone wrong?

Okay, I understand your question better now…

So, your line: $workingDir = ftp_pwd($conn); just gets the current directory (folder) you are currently in.
But, you must set the directory you want to be in. The FTP log-in does NOT set the directory for you.

So, let’s recap this process…

Your current code creates a FTP connection, logs into the connection and then reads the current directory from the connection. Therefore, it will always be in the root folder which is always “” or null. So, as-is your code is doing exactly what you asked of it.

Now, what you should be doing is create a FTP connection, log into the connection and then set the directory (folder) to the folder where your files are, in this case “upgrades”.

So, instead of reading the connection’s current directory, set it to the upgrades folder. Something like this:
ftp_chdir($conn, ‘upgrades’);
After that command, the FTP connection will point at the files and you can pull the list from there.

Hope that helped…

That doesn’t work either :’(
The ‘upgrades’ folder is the parent folder and the final folder will vary depending on the login credentials like so:

upgrades/psu/ or upgrades/mike/ or upgrades/ups/ etc.

The code i have upto now must be getting the correct folder info as it will display the files in the folders,
but when i select it for download the file path is ‘upgrades//’ then the file name when it should be
upgrades/psu/ or upgrades/mike/ or upgrades/ups/ then filename.

[php]$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 “’<a href=upgrades/$workingDir’” . $fList[$i] . “>” . $fList[$i] . “
”;
}
}
else
{
echo “$workingDir contains no files.”;
}

?> [/php]

Okay, I will repeat…

Your code: $workingDir = ftp_pwd($conn); Sets the variable $workingDir to the ROOT directory of the FTP connection. This will NEVER work for you because you NEVER set the directory of the FTP connection. So you are trying to use a NULL as the variable $workingDir. (As I said before!)

You must SET the directory of the FTP to whatever you want to use. Therefore, if you are using the user’s login info as the folder/directory you want to start at in the FTP connection, YOU have to set that before you can assign it to the variable $workingDir.
Something like: ftp_chdir($conn, ‘upgrades/Mike’); or ftp_chdir($conn, ‘upgrades/ups’); or…

So, if you are using the variable “$ftpUser” for the login credentials and IF that is the folder name, use it
something like this: ftp_chdir($conn, ‘upgrades/$ftpUser’); This will set you to the correct folder. And,
then, when you pull the current folder into your $workingDir variable, it will get the correct value.
But, since you are also using a different variable in your version of your code, you could just change it
to something like this: $workingDir = “upgrades/” . $ftpUser; This would bypass the setting of the folder.

Hope you understand this now. Let us know if you do not…

It works ok now.
I changed ‘$workingDir’ to ‘$ftpUser’ and renamed the ftp server directory’s to the user names.

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

I wouldn’t have got this far without all your help, so thanks very much.

Great! Congrats! Always a warm fuzzy feeling to solve a code puzzle… Glad we could help!

See you again in the bitstream… (All that this really is, LOL)

Sponsor our Newsletter | Privacy Policy | Terms of Service