Helpful Information
 
 
Category: FTP Help
download files using ftp and php

Hi,

I want to download files from a ftp server using a php script with ftp_get(). Is it possible to download files from a server using a script with ftp_get. I mean are the files saved on the users computer or on the webserver? I want to use this script on computers that are behind a firewall that only alows access to the internet on port 80 and the http protocol.

I've got this:


<?php
// set up basic connection
$conn_id = ftp_connect($server);

// login with username and password
$login_result = ftp_login($conn_id, $naam, $w8);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "Ftp connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
die;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_get($conn_id, $naar, $bestand, FTP_BINARY);

// check upload status
if (!$upload) {
echo "Ftp download has failed!";
} else {
echo "Download $bestand from $server as $naar";
}

// close the FTP stream
ftp_close($conn_id);

?>


Thanks,

Sebastiaan

Since PHP is a server side langauage, the files you fetch will end up on the web server.
You can however force a download box to appear on the users machine by passing the downloaded file forward to the browser after it has finished downloading. (Everything takes place behind the scenes from the clients point of view)

There are threads here in the forum on how to do this. I don't know the exact syntax without some research, but as I mentioned. It has been discussed before about how to make the download box appear on the clients box.

1. Make the ftp connection and retrieve the files into a location on the server.
2. Find the threads that talk about making the download box appear and pass through the files to that, so the user will have the option of saving the files.
(if there are multiple files, you might want to zip them together before you pass them through).
3. Done.

Hope this will get you started

About the firewall.
They better have a very good reason for not letting the users make outside ftp connections. It is one thing, not letting users from the outside make ftp connections to the private network, but the other way around? I don't know. I assume you do.

Thanks.

Preventing acces to ftp servers is to prevent students to download the newest movies and games. Our school doesn't like that :(










privacy (GDPR)