用PHP将文件传输到另一台返回“内部服务器错误”的服务器

I'm experiencing some weird problem with PHP. I have a PHP script that performs a file upload to my sever and them, transfers it to a remote server. The code is correct as it works. The problem is that it does not ALLWAYS work. Most times, it just returns an Internal Server Error. My Error log just says that it has ocurred an error 404, but I don't do any "Header" commands. It just echoes some HTML and Javascript to an Iframe. And when the error occurs, the file is cut in the remote server, so I really can't figure out where the error is taking place. I guess it is some misconfiguration on my server, becouse I have tried the same script in MAMP and it worked perfectly, with no issues. Can anyone help me at least, to get the error it's throwing so that I know where to start?

I know it's a bit simple, but i'm just not much experienced with PHP as I am with other languages.

Here's my code:

<?php

//Uploads a file

$target_path = "uploadedMp3/";

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

$returnMessage = '';

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{
    //FTP the file to VM Link

    $ftp_ip         = MY_IP;
    $ftp_username = MY_USER_NAME;
    $ftp_password = MY_PASSWORD;

    if($ftp=ftp_connect($ftp_ip,21,3600))
    {
        if(ftp_login($ftp,$ftp_username,$ftp_password))
        {

            // Set to PASV mode

            ftp_pasv( $ftp, 1);


            ftp_chdir($ftp,"/mp3/");


            // Send the file

            //set_time_limit(-1);       // PHP won't timeout
            //ignore_user_abort(true);
            ini_set('upload_max_filesize', '50M'); 
            ini_set('post_max_size', '50M');  

            $upload = ftp_put($ftp, $_FILES['uploadedfile']['name'],$target_path ,  FTP_BINARY); 


            $returnMessage .= "0##The file has been uploaded";

        }
        else
        {
            $returnMessage .= "-1##Ocorreu um erro de conexão com o servidor remoto. Entre em contato com a Mobilevel para mais informações.(1)";
        }
        ftp_close($ftp);

    }
    else
    {
         $returnMessage .= "-1##Ocorreu um erro na conexão com o servidor remoto. Entre em contato com a Mobilevel para mais informações.(2)";
    }
} 
else
{
    $returnMessage .= "-1##Ocorreu um erro no Upload, tente novamente. Se o problema persistir entre em contato com a Mobilevel.";
}

unlink($target_path);
echo "<html><head>";
echo "<scr";
echo "ipt type=\"text/javascript\">parent.uploadCallback('" . $returnMessage . "');</scr";
echo "ipt>";
echo "</head><body>";
echo "</body></html>";
/*
echo "<scr";
echo "ipt type=\"text/javascript\">parent.uploadCallback('" . $returnMessage . "');</scr";
echo "ipt></html>";
*/

?>

500 internal errors get logged in the server's error log. Go look there for details of exactly what blew up.

However, note that

        ini_set('upload_max_filesize', '50M'); 
        ini_set('post_max_size', '50M');  

are useless in a file-upload handling script. The script these directives are in is NOT executed until AFTER the upload completes (or fails). By the time PHP actually hands over control to the script, the upload's already done or failed.

These overrides have to be done at the .htaccess/httpd.conf level with php_value directives, or in php.ini.

As well you're not checking if the upload actually succeeded. If it fails for any reason, your move_uploaded_file() command is going to fail because there's no file to move

At bare minimum, your script should start with this:

if ($_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['uploadedfile']['error']);
}

The error codes are documented here. Note that your code is also not checking for any file collisions - it will silently overwrite any existing file with the uploaded file.