【问题标题】:HTTP 500 error while file uploading using uploadify使用 uploadify 上传文件时出现 HTTP 500 错误
【发布时间】:2014-04-01 10:54:10
【问题描述】:

我一直卡在上传部分。我正在使用uploadify 脚本。正常文件已成功上传,但是当我厌倦了上传带有 file's.jpg 之类的文件名时,会发生 HTTP 500 错误。 我的上传脚本是:

<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
  $('#attachment').uploadify({
    'uploader'  : '<?php echo base_url();?>web/uploadify/uploadify.swf',
    'script'    : '<?php echo base_url();?>web/uploadify/uploadify.php',
    'cancelImg' : '<?php echo base_url();?>web/uploadify/cancel.png',   
    //'folder'    : '../../uploads/project_files/',
    'folder'    : '/admin_panel_new/assets/plupload/uploads/',
    'auto'      : true,
    'multi'       : true,   
    'hideButton': false,
    'buttonImg' :  "<?php echo base_url()?>images/attachment_image.gif",    
    'width'     : 132,
    'height'    : 25,
    'removeCompleted' : false,
    'onSelect' : function(file) {   
            $('#submitFeedback').val('Please wait while uploading...');
            $('#submitFeedback').attr('disabled','disabled');           
    },
    'onComplete'  : function(event, ID, fileObj, response, data) {
        if($('#fileList').val()!='')    
            $('#fileList').val($('#fileList').val()+','+response);                      
        else
            $('#fileList').val(response);   
        //alert($('#fileList').val());  

        $('#submitFeedback').removeAttr('disabled');
        $('#submitFeedback').val('Post Feedback');              
    },
    'onError' : function(event, queueID, fileObj, errorObj) { alert(errorObj.type + ' ' + errorObj.info ); }
  });
});
</script>

我尝试了许多其他解决方案,但都没有运气。

我的uploadify.php代码是:

<?php
$targetFolder = $_POST['targetFolder']; // Relative to the root
$unik =$_POST['timestamp'];

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/'.$unik.'_'.$_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png','txt'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        //
        echo $unik.'_'.$_FILES['Filedata']['name'];
    } else {
        echo 'Invalid file type. Upload Failed';
    }
}
?>

上传错误图片如下所示

如果有人有解决方案,请帮忙。谢谢。

【问题讨论】:

  • 不,实际上文件只是一个 kb 而不是更大的大小。
  • “500 内部服务器错误”状态码的基本意思是:“请检查日志以了解发生了什么”。你试过吗?只是猜测可能需要很长时间。
  • 我已经检查过了,但没有找到任何错误日志。由于相同的代码在 localhost 中像魅力一样工作,但在现场尝试时没有成功。 :(

标签: php uploadify


【解决方案1】:

如果问题只出现在使用单引号的文件名上,我首先要看的是

$_FILES['Filedata']['name']

由于您要连接未转义的输入字符串,因此单引号很有可能与move_uploaded_file 发生冲突。 This thread 演示了类似的问题。

我可能会在存储之前从文件名中删除单引号。或者试试addslashes

$targetFile = rtrim($targetPath,'/') . '/'.$unik.'_'.str_replace("'","",$_FILES['Filedata']['name']);

【讨论】:

  • 感谢您的回答,但同样的错误再次出现在您的代码中。我尝试将文件名上传为 base's_camp_mail.txt 并发生此错误。
  • 您可以访问服务器的错误日志吗?这是我寻找 500 个错误的第一个地方。或者尝试用静态字符串替换名称,例如“test.txt”,看看您是否可以将问题隔离到文件名。
猜你喜欢
  • 2019-09-25
  • 2023-03-30
  • 1970-01-01
  • 2011-05-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多