Hi guys.

I'm trying to implement an ftp upload system in php and for some reason I had this working a couple of weeks ago but now I can't seem to get it to upload any files it keeps coming up saying the file can not be uploaded. I get the feeling there may be a problem with the $file variable as it does not seem to echo anything. The code is below:

<?php
  if(isset($_POST['SubmitFile'])){
      $myFile = $_FILES['txt_file']; // This will make an array out of the file information that was stored.
      $file = $myFile['tmp_name'];  //Converts the array into a new string containing the path name on the server where your file is.

  $myFileName = basename($_POST['txt_fileName']); //Retrieve filename out of file path

  $destination_file = "/htdocs/new/images/products/".$myFileName;  //where you want to throw the file on the webserver (relative to your login dir)

  // connection settings
  $ftp_server = "***";  //address of ftp server.
  $ftp_user_name = "***"; // Username
  $ftp_user_pass = "***";   // Password

  $conn_id = ftp_connect($ftp_server);// set up basic connection
  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<h2>You do not have access to this ftp server!</h2>");   // login with username and password, or give invalid user message
  if ((!$conn_id) || (!$login_result)) {  // check connection
         // wont ever hit this, b/c of the die call on ftp_login
         echo "FTP connection has failed! <br />";
         echo "Attempted to connect to $ftp_server for user $ftp_user_name";
         exit;
     } else {
         echo "Connected to $ftp_server, for user $ftp_user_name <br />";
		 echo $file;
		 echo $myFileName;
  }

  $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);  // upload the file
  if (!$upload) {  // check upload status
     echo "<h2>FTP upload of $myFileName has failed!</h2> <br />";
  } else {
     echo "Uploading $myFileName Complete!<br /><br />";
  }
echo $destination_file;
      ftp_close($conn_id); // close the FTP stream
  }
?>

<html>
  <head>

  </head>
  <body>
        <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
            Please choose a file: <input name="txt_file" type="file" id="txt_file" tabindex="1" size="35" onChange="txt_fileName.value=txt_file.value" />
            <input name="txt_fileName" type="hidden" id="txt_fileName" tabindex="99" size="1" />

        <input type="submit" name="SubmitFile" value="Upload File" accesskey="ENTER" tabindex="2" />
  </form>
  </body>
<html>

Thanks any any help you can provide. I'm kinda tearing my hair out at this!!

    Could you please provide [php ] [/php ] tags around your code for easy of debugging?

    I do not see anything ovbvious this instance, but in a hurry. To self-help:
    At each step of the project echo out a string (e.g., echo 'a'😉 so you can follow the progress of the upload. If all works fin through to the final step without getting in the error sections, start echoeing out variable names.

      leatherback wrote:

      Could you please provide [php ] [/php ] tags around your code for easy of debugging?

      I do not see anything ovbvious this instance, but in a hurry. To self-help:
      At each step of the project echo out a string (e.g., echo 'a'😉 so you can follow the progress of the upload. If all works fin through to the final step without getting in the error sections, start echoeing out variable names.

      Ah yes it looks much better in php tags 🙂

      I've tried to echo $file variable but no such luck and if im right it should be a string. so I think that may be the problem as it is used later in the upload section below. but I can't figure out what is wrong with it.

        Just been going through it again and it basically gets to this point in the code

              if (!$upload) {  // check upload status
                 echo "<h2>FTP upload of $myFileName has failed!</h2> <br />";
              } else {
                 echo "Uploading $myFileName Complete!<br /><br />";
              }

        And tells me that FTP upload of the file has failed.

        I've tried to echo $file but I get nothing.

        I can echo out $myfilename & $destination_file and they both work however.

          Guys if this is any help I'm getting this error message after turning on error reporting:

          Notice: Undefined index: txt_file in e:\domains\d\deaville.info\user\htdocs\new\upload.php on line 4

            Hehe,

            yeah.. That does not exist..

            have a look at the structure of the returned file array using:

            print_r($_FILES);

            I do not have my codes here, so I cannot tell you exactly where the filename sits, but presumable

            $_FILES['tmp_name']

            edit:

            we are talking about this line:
            $myFile = $_FILES['txt_file']; // This will make an array out of the file

            You are trying to use an index 'text_file' for the $_FILES superglobals array, but that will not be set.

              leatherback wrote:

              Hehe,

              yeah.. That does not exist..

              have a look at the structure of the returned file array using:

              print_r($_FILES);

              I do not have my codes here, so I cannot tell you exactly where the filename sits, but presumable

              $_FILES['tmp_name']

              edit:

              we are talking about this line:
              $myFile = $_FILES['txt_file']; // This will make an array out of the file

              You are trying to use an index 'text_file' for the $_FILES superglobals array, but that will not be set.

              Mmm ok I've just printed $_files and in the brackets on screen there is nothing so I'm guessing its empty 🙂.

              I'm a tad confused now but your last statement and where I should go from here.

              Thanks mucho for any help you can give 🙂

                Not sure if its any help but Register Globals is set to off could that be having any effect on it?

                  Guys

                  I've just had a thought. I'm running on a windows 2003 server and the ftp permissions chmod number is set to 700. Are these permissions correct or should it be different say 777?

                    Write a Reply...