Page 1 of 1

How to upload a file with FTP?

Posted: Wed Jan 18, 2006 8:01 am
by jimpa1986
Can anyone help me with making a script where i can upload a file, located on my computer, to a FTP-server.

username: kitabu
Home Directory: /hsphere/local/home/kitabu
Host Name: ws11.surftown.se
Password: ***

I'm very thankful for any help!

Posted: Wed Jan 18, 2006 8:58 am
by timvw
Just get yourself an ftp-client? (PHP typically runs on the server, so there is no way you could use it to access files on the client computer)

Posted: Wed Jan 18, 2006 9:31 am
by jimpa1986
the thing is that i want to upload pics and at the same time add them to a database (mysql)...
is there any other solutions to this?

Posted: Wed Jan 18, 2006 9:47 am
by timvw
Upload them to a directory with your ftp-client.

Now you only have to write a script that reads file from the location where you have uploaded them, eventually move them, and store them in the database.

Posted: Wed Jan 18, 2006 10:48 am
by jimpa1986
let's say that the files is in a dir called img/gallery
can you help me form a script, because i'm totally lost.

Posted: Wed Jan 18, 2006 11:02 am
by m3mn0n

Posted: Wed Jan 18, 2006 11:04 am
by Christopher
If you Google "PHP FTP client" you will find a number of them. Probably one will work for your purposes.

Posted: Fri Jan 20, 2006 3:18 pm
by m3mn0n
Sami wrote:Have a read at http://www.php.net/features.file-upload
I just noticed I posted the wrong link. What I should have posted is: http://www.php.net/ftp
Installation

In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or greater or --with-ftp when using PHP 3.

The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.
Example from that page:

Code: Select all

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

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

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

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
   }

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