使用 PHP 将文件传输到另一台服务器,返回“内部服务器错误”
我在使用 PHP 时遇到了一些奇怪的问题。我有一个 PHP 脚本,它执行文件上传到我的服务器,然后将其传输到远程服务器。该代码是正确的,因为它有效。问题是它并不总是有效。大多数时候,它只是返回内部服务器错误。我的错误日志只是说发生了错误 404,但我没有执行任何“标头”命令。它只是将一些 HTML 和 Javascript 回显到 Iframe。而且当错误发生时,文件会在远程服务器中被剪切,所以我真的无法弄清楚错误发生在哪里。我猜这是我的服务器上的一些配置错误,因为我在 MAMP 中尝试了相同的脚本,并且它工作得很好,没有任何问题。任何人都可以至少帮助我,得到它抛出的错误,以便我知道从哪里开始吗?
我知道这有点简单,但我对 PHP 的经验不像其他语言那样丰富。
这是我的代码:
<?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>";
*/
?>
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>";
*/
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
500 个内部错误记录在服务器的错误日志中。去那里看看到底发生了什么爆炸的细节。
但是,请注意,它们
在文件上传处理脚本中是无用的。这些指令所在的脚本只有在上传完成(或失败)后才会执行。当 PHP 实际将控制权移交给脚本时,上传已经完成或失败。
这些覆盖必须在 .htaccess/httpd.conf 级别使用
php_value
指令或在 php.ini 中完成。此外,您也没有检查上传是否确实成功。如果由于任何原因失败,您的
move_uploaded_file()
命令将会失败,因为没有要移动的文件至少,您的脚本应该以此开头:
错误代码已记录在案 此处。
请注意,您的代码也不会检查任何文件冲突 - 它会默默地用上传的文件覆盖任何现有文件。
500 internal errors get logged in the server's error log. Go look there for details of exactly what blew up.
However, note that
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 moveAt bare minimum, your script should start with this:
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.