Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

File Handling

Uploading Files Via FTP by Glowball
Posted: 13 Mar 05 (Edited 13 Mar 05)

This script will upload a file from a client machine that does not have PHP installed to an FTP server with PHP installed.  The example specifically uploads a GIF file for testing, but it could be modified to upload any and all file types.  If it fails to change directories, you should FTP to your server and see how it lists the directory you want from within the FTP account.

Note that this example is filled with testing feedback text so it is just a starting point.  Don't forget to change the permissions on your destination directory so you can write to it (not necessary if your server is Windows-based).

Name the single file "ftp_example.php" and set the variables correctly and it should run for you.


if (isset($_POST['upload']) && $_POST['upload'] == 1) {
    // set up the variables
    $ftp_server = "server_name_or_IP_address";
    $ftp_user = "username";
    $ftp_password = "password";
    $destination_directory = "some/directory/from/default/ftp/directory";
    $destination_file = "ftp_upload_test.gif";
    // make sure FTP is enabled on the server
    if (function_exists("ftp_connect"))
        echo "FTP enabled on server...<br>\n";
    else {
        echo "FTP is not enabled on $ftp_server!";
    // set up connection
    $conn_id = ftp_connect($ftp_server);
    if (!$conn_id) {
        echo "Could not connect!";
    else echo "Connected...<br>\n";
    // log in
    $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
    if (!$login_result) {
        echo "Could not log in!";
    else echo "Logged in...<br>\n";
    // set to passive mode
    $set_passive = ftp_pasv($conn_id, true);
    if (!$set_passive) {
        echo "Could not set to passive mode!";
    else echo "Connection is set to passive mode...<br>\n";
    // set the timeout in seconds
    $set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
    if (!$set_timeout) {
        echo "Could not set the timeout!";
    else echo "Set timeout...<br>\n";
    // change directories
    $change_dir = ftp_chdir($conn_id, $destination_directory);
    if (!$change_dir) {
        echo "Failed to change directory!";
    else echo "Changed directory...<br>\n";
    // upload the file
    $source_path = $_FILES['source_file']['tmp_name'];
    $upload = ftp_put($conn_id, $destination_file, $source_path, FTP_BINARY);
    // check upload status
    if (!$upload)
        echo "FTP upload has failed!";
        echo "Uploaded $source_path to $ftp_server as <b>$destination_file</b>";
    // close the FTP stream
    echo "\n<hr>\n";

<form action="ftp_example.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="upload" value="1">
<b>GIF File to upload: </b><input type="file" name="source_file"><br>
<input type="submit" value=" Upload ">

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close