dongzhenshen7435 2011-10-20 17:12
浏览 48

用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>";
*/

?>
  • 写回答

1条回答 默认 最新

  • dongmei1911 2011-10-20 17:22
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题