使用uploadify上传文件时出现HTTP 500错误

时间:2014-04-01 10:54:10

标签: php uploadify

我一直被困在上传部分。我正在使用uploadify脚本。普通文件已成功上传,但当我厌倦上传文件名时,如 file's.jpg ,则会出现HTTP 500错误。 我的uploadify脚本是:

<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';
    }
}
?>

上传错误图片如下所示 enter image description here

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

1 个答案:

答案 0 :(得分:0)

如果问题只发生在使用单引号的文件名中,我首先看的是

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

由于您正在连接未转义的输入字符串,因此单引号很可能会导致move_uploaded_file丢失。 This thread演示了类似的问题。

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

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