PHP FTP upload Via HTML form

Posted on 2004-04-16
Last Modified: 2010-05-18
   I am developing a html form that will POST to an PHP module that plugs into my PHP based CMS ( and upload a specified file from a local computer to a remote FTP server. I have spent all afternoon piecing together information and I have sucessfully uploaded a file using a stand alone version of the php script where I have pluged in the filename manually. This is the main problem I am trying to tackle:

    I can't seem to get the ftp_put command to recognize the data from the variable I am trying to pass to it. I think that this may be because when the client browses to the file on their local computer, the information that is passed through to the ftp_put contains their local path. I would think that this path would need to be omited when specifying the destination path on the ftp server. The problems is, I don't know for sure - and although I have tried - I don't know how to fix it.

    Ultimately I will be modifying a php form_mail module that works with my CMS to work as the FTP upload interface. The code that I have included does not include any of the form_mail code, which in and of itself is relatively redundant because all of the action happens once the ftp code is executed.

The following section of this question will contain two sections of code, firstly the HTML form that i am POSTing with and secondly the PHP script that i am working with to upload files from various client sources (for the most part straight from

The Form:

<form action="form_mail" method="post" enctype="multipart/form-data">
  1. Left-Click Browse.<br>
  2. In the dialog that opens, please browse to your zipped batch, select it, and click open.<br>
  3. Left-Click Upload.<br>
<input type=file name=source_file size=20>
<input type="submit" class="form-submit" name="op" value="Send" />

The PHP:

// set up basic connection
$conn_id = ftp_connect("");
$ftp_user_name = "xxxxxx";
$ftp_server = "xxx";
$s_file = $_POST['source_file'];
$d_file = $_POST['source_file'];

// login with username and password
$login_result = ftp_login($conn_id, "xxxxxx", "xxxxxxx");

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

// upload the file
$upload = ftp_put($conn_id, $d_file, $s_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

Thank you for your kind consideration!

Question by:sadian
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

TheClickMaster earned 500 total points
ID: 10847836
After the file is uploaded to your webserver, its path & name will be in the variable


therefore try to change it to

$s_file = $_FILES['userfile']['tmp_name'];
$d_file = $_FILES['userfile']['tmp_name'];

Also, you can check the size, and encoding type of the uploaded file with:


You can use this to validate the file. If no file is uploaded, $_FILES['userfile']['size'] will be 0 and $_FILES['userfile']['tmp_name'] will be empty.

Additionnal stuff:

The original name (on the users machine) is in

And if there were errors you have


which will contain an error code. (Have a look at this page to see the possible error codes: )

Also might have a look at

Hope this helps


Expert Comment

ID: 10847856
After re-reading it you might want to change $s_file to something else depending on how you want it to name the files on the FTP server.

Should they keep the same name? If so try:

$s_file = substr($_FILES['userfile']['tmp_name'],strrpos($_FILES['userfile']['tmp_name'],"/");

Of course this always depends on the directory strucutre of the FTP server maybe you have to put it inside a specific directory...

Expert Comment

ID: 10847861
And that code was mising a closing paranthesis =)

s_file = substr($_FILES['userfile']['tmp_name'],strrpos($_FILES['userfile']['tmp_name'],"/"));

Author Comment

ID: 10878805
First, TCM - your awesome - keep up the great work.

I ended up dropping the idea of utilizing FTP to transfer the files. This is considering that the server is in our local office and the added expense of a professional level FTP server (considering we are hosting with w2k & apache2 (cause IIS doesn't support mod_rewrite.)) I decided to go with a solution that utilizes HTTP uploads. The final code itself contains alot of html but here is the basic code I built my my page upon:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      <title>Upload a File</title>
<?php // upload_file.php
// This script displays and handles an HTML form.
// This script takes a file upload and stores it on the server.

// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if (isset ($_POST['submit'])) { // Handle form.

      // Try to move the uploaded file.
      if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "../uploads/{$_FILES['thefile']['name']}")) {
            print '<p>Your file has been uploaded.</p>';
      } else { // Problem!
            print '<p>Your file could not be uploaded because: <b>';
            // Print a message based upon the error.
            switch ($_FILES['thefile']['error']) {
                  case 1:
                        print 'The file exceeds the upload_max_filesize setting in php.ini';
                  case 2:
                        print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
                  case 3:
                        print 'The file was only partially uploaded';
                  case 4:
                        print 'No file was uploaded';
            print '</b>.</p>';
} // End of SUBMIT IF.

// Leave PHP and display the form.

<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Upload a file using this form: <br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="thefile" /><br /><br />
<input type="submit" name="submit" value="Upload This File" />

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question