How to upload a file with FTP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimpa1986
Forum Newbie
Posts: 4
Joined: Wed Jan 18, 2006 4:51 am

How to upload a file with FTP?

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
jimpa1986
Forum Newbie
Posts: 4
Joined: Wed Jan 18, 2006 4:51 am

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
jimpa1986
Forum Newbie
Posts: 4
Joined: Wed Jan 18, 2006 4:51 am

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you Google "PHP FTP client" you will find a number of them. Probably one will work for your purposes.
(#10850)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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);
?>
Post Reply