简体   繁体   中英

FTP file upload successefully but not showing on server

Just a brief introduction / background info:

this can be seen as part 2 of my question from yesterday where @Fred-ii was very helpful. I have managed to rectify the file path errors etc.

My Problem

As stated above I am successfully uploading file to server (or at least getting success message) but when I check for the file in the folder it is not there..? In fact I have fine combed the whole server for the uploaded file but not seeing uploaded file.

All files have proper permissions, is there anything that can cause this problem except for file permissions?

Perhaps something I have to change in php.ini...but then again I would not have received upload success message..?

Has this ever happened to anyone in the past?

$file_upload="true";
$file_up_size=$_FILES['file_up']['size'];
echo $_FILES['file_up']['name'];

if ($_FILES['file_up']['size']>250000){
    $msg=$msg."Your uploaded file size is more than 250KB
 so please reduce the file size and then upload.<BR>";
$file_upload="false";
}

if (!($_FILES['file_up']['type'] =="image/jpeg" OR $_FILES['file_up']['type'] =="image/gif"))
{
    $msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}

$file_name=$_FILES['file_up']['name'];

if($file_upload=="true"){

$ftp_server = "xxxxxxx";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username = "xxxxxx";
$ftp_userpass = "xxxxxx";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
if($login){
    echo '<h1>CONNECTED TO FTP</h1>';
$file = $file_name;

// upload file
if (ftp_put($ftp_conn, $file, 'img/userPics/', FTP_BINARY)) 
  {
  echo "Successfully uploaded $file.";
  }
else
  {
  echo "Error uploading $file.";
  }
}//if file upload == true
}//if result
// close connection
ftp_close($ftp_conn);   

One of the first things you should do when uploading a file or multiple files is check for upload errors.

// Check $_FILES['file_up']['error'] value.
$file_upload=true;
switch ($_FILES['file_up'']['error']) {

    case UPLOAD_ERR_INI_SIZE:
        echo 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_FORM_SIZE:
        echo 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_PARTIAL
        echo 'The uploaded file was only partially uploaded.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_NO_FILE:
        echo 'No file was uploaded.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_NO_TMP_DIR:
        echo 'Missing a temporary folder.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_CANT_WRITE:
        echo 'Failed to write file to disk.';
        $file_upload=false;
        break;
    case UPLOAD_ERR_EXTENSION:
        echo 'A PHP extension stopped the file upload.';
        $file_upload=false;
        break;
}

if ( ! $file_upload ) {
    // no file so do whatever is appropriate on upload fail

    exit;
}
// then the rest of your code

Also notice I changed $file_upload="false"; to $file_upload=false; It much better to use the real boolean values =true and =false rather than a string, then you can test them simply using if ( $file_upload ) asks is it true and if ( ! $file_upload ) asks is it false .

Hopefully this will identify what has actually gone wrong in the upload process.

Now to the other errors in your code!

The most obvious is that you are getting the filename of the file the user uploaded from $file_name=$_FILES['file_up']['name']; but that is just the name and not where the uploaded file actually exists.

The PHP upload process is a 2 phase process, The browser sends the file and PHP stores it in a temporary file. Then your code is run, you get to validate and check sizes and file types etc. You are then supposed to move the temporary file to a permanant file on your file system using move_file_upload()

Now as you are not actually storing this file on your system you can probably forget this step, but that means that the file you FTP to the other server shoudl be sent from $_FILES['file_up']['tmp'];

So change this line

$file_name=$_FILES['file_up']['name'];

To

$file_name=$_FILES['file_up']['tmp'];

and then at least you will be sending a real file over your FTP connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM