【问题标题】:Uploading of file is broken文件上传失败
【发布时间】:2012-03-18 09:24:49
【问题描述】:

我正在使用uploadify 上传带有PHP 5.3.3 和Apache 2.2.16 服务器的文件。 *我使用的所有图片文件都是小

有趣的是,对于某些图像文件,uploadify 可以正常工作并正确上传图像文件。但是对于其他人来说,只上传了 8 个字节,这很奇怪。我不确定为什么上传的文件不完整。

Uploadify 以某种方式表示文件已 100% 成功上传,我也在使用 onError 函数。

任何有关如何找出问题的帮助都会非常有帮助。

上传代码:

$('#change_thumb_file').uploadify({
'hideButton'  : true,
'wmode'       : 'transparent',
'folder'      : VG.PROJECT_ROOT + '/static/apps/vialogues', 
    'uploader'    : VG.SITE_STATIC_URL+'uploadify/scripts/uploadify.swf',
    'script'      : VG.APPS_STATIC_URL+"vialogues/php/uploadify.php",
    'buttonText'  : 'Select an image',
    'cancelImg'   : VG.SITE_STATIC_URL+'uploadify/cancel.png',
    'auto'        :  true,
    'sizeLimit'   :  5242880,
    'queueID'     : 'fileQueue',
    'scriptAccess': 'always',
    'method'      : 'POST',
    'queueSizeLimit' : 1,
    'width'       : '100',
    'height'      : '30',
    'fileDesc'    : 'images',
    'fileExt'     : '*.jpg;*.jpeg;*.png;*.bmp;*.gif',
    'wmode'       : 'transparent',
    'altContent'  : '<div id="flash_notice">Flash player version 10.0.0 or above is required to upload a video. Please click the icon below to download Flash player.\
            <br /><a href="https://www.adobe.com/go/getflashplayer">\
            &nbsp;<img src="' + VG.SITE_STATIC_URL + 'uploadify/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33">\
            </a>\
            </div>',
    'onComplete' : function (event, queueID, fileObj, response, data){
        preview_uri = response.replace(VG.PROJECT_ROOT, '');
        $.ajax({
            url:VG.SITE_URL + 'vialogues/api/crop_thumbnail',
            type:'PUT',
            data: {'img': preview_uri}, 
            success: function(data){
                $('#thumb_preview').empty().append('<img src="'+preview_uri+'" />');
            },
            failure: function() {alert("There was an unexpected error. Please try again."); window.location.reload()},
        });
        $('#step_two').fadeIn();
    },               
    'onError' : function(event, queueID, fileObj, errorObj) {
              var errMsg = "There was an error uploading ... \n";
              errMsg += "Error type: " + errorObj.type + "\n";
              errMsg += "Error Info: " + errorObj.info + "\n";
              alert(errMsg);
            }
});

上传文件的代码(uploadify.php):

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_REQUEST['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

$fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
$fileTypes  = str_replace(';','|',$fileTypes);
$typesArray = split('\|',$fileTypes);
$fileParts  = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$typesArray)) {

    $result = move_uploaded_file($tempFile,$targetFile);
    if ($result) {
        echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);         
    }
    else {
        echo 'Upload Failed';
    }
} else {
    echo 'Invalid file type.';
}

}

【问题讨论】:

  • 请为您的问题提供一些代码以进行检查,以便我们为您提供帮助。问候
  • 包含了代码...但我认为问题不在于代码。我觉得文件上传在某个地方中断了,我不知道如何才能弄清楚它在哪里。
  • 您的 Apache 错误日志中有任何内容吗?此外,在上传过程中,检查 /tmp 的内容(假设您的服务器运行 Linux)并查看是否有 PHP 临时文件随着您的上传发送而增长。
  • apache 错误日志中没有输出错误,而且不知何故我无法在 tmp 文件夹中找到该文件。它似乎不存在。
  • 所以到目前为止,我发现文件已成功上传到 /tmp 文件夹,但 move_uploaded_file(php 的功能)不起作用。即使权限都设置了,它也不会给出任何错误。

标签: php apache file-upload uploadify


【解决方案1】:

所以问题是由于权限问题而发生的。我所做的解决方法是在根目录中创建一个单独的文件夹,并将其显式权限授予运行 apache 的 www-data 用户。不知何故 move_uploaded_file 没有抛出任何错误以获得许可。

【讨论】: