上传脚本没有错误但没有上传

时间:2011-11-05 10:48:03

标签: javascript jquery uploadify

我正在使用uploadify,我的问题是文件没有上传。

这是我的代码:

在uploadify.php上我将根路径设置为:/

像这样:

$targetFolder = '/'; // Relative to the root

然后是脚本上的其余部分:

<script type="text/javascript">
jQuery(document).ready(function() {


     $('#file_upload').uploadify({
        'uploader'    : '/myIncludes/UploadiftyFolder/uploadify.php',
        'swf'  : '/myIncludes/UploadiftyFolder/uploadify.swf',
        'cancelImg' : '/myIncludes/UploadiftyFolder/cancel.png',
        'folder'    : '/myUploads/UploadsDirectory/images/',
        'auto'      : true,
        'multi'         : false,
        'checkExisting' : false
      });
});
</script>

//Finally 

<input id="file_upload" type="file" name="Filedata" />
<a href="javascript:$('#file_upload').uploadifyUpload();">Upload Files</a>

当我尝试上传图片时,一切运作良好(似乎也是如此),它说 - 完成......

但没有上传任何内容。

有什么想法吗?

更新:

以下是我的服务器结构路径:

我的路径:

root/myIncludes/UploadiftyFolder/  <--Here are all the uploadify files

root/myUploads/UploadsDirectory/images/  <--Here is where I need to upload

以下是我在uploadify上的当前设置:

folder -->  '/myUploads/UploadsDirectory/images/',

and in uploadify.php --> $targetFolder = '/'; // Relative to the root

以下是uploadify.php文件的其余部分......我没有更改任何内容:

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

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

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}

4 个答案:

答案 0 :(得分:4)

对我来说,问题出在uploadify.php文件中。他们正在使用:

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

$targetFolder由您在顶部定义。

然后再说:

move_uploaded_file($tempFile,$targetFile);

默认示例将目标文件夹附加到$_SERVER['DOCUMENT_ROOT']的末尾。我的$_SERVER['DOCUMENT_ROOT']实际上是C:\xampp\htdocs。因此,要使其工作,您的目标文件夹必须是:

$targetFolder = "/yourSiteFolder/wherever/your/upload/folder/is";

我做了什么:

完全摆脱$_SERVER['DOCUMENT_ROOT']。这是我的uploadify.php文件:

<?php
/*
Uploadify v3.1.0
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
//$targetFolder = '/sandbox/uploads'; // Relative to the root

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

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

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($_FILES["Filedata"]["tmp_name"], "uploads/" . $_FILES["Filedata"]["name"]);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
?>

主要的变化是我已经取代了这个:

move_uploaded_file($tempFile,$targetFile);

用这个:

move_uploaded_file($_FILES["Filedata"]["tmp_name"], "uploads/" . $_FILES["Filedata"]["name"]);

然后评论出几条不再需要的行。

这是我的check-exists.php文件:

<?php
/*
Uploadify v3.1.0
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
//$targetFolder = 'uploads'; // Relative to the root and should match the upload folder     in the uploader script

if (file_exists("uploads/" . $_POST['filename'])) {
    echo 1;
} else {
    echo 0;
}
?>

这是我的jquery代码:

$(function() {
    $("#uploadify").uploadify({
        height        : 30,
        swf           : 'uploadify/uploadify.swf',
        uploader      : 'uploadify/uploadify.php',
        width         : 120
    });
});

关于我网站文件结构的说明:

所有uploadify文件都位于我网站的根目录中,名为uploadify。在uploadify内是一个名为uploads的文件夹,这是文件上传的位置。

希望有所帮助。

答案 1 :(得分:2)

我在这里尝试了所有其他建议,但它们都没有为我工作。然后我意识到版本3.2的Uploadify(也可能是以前的版本)需要时间戳和散列令牌才能完成上传。

首先,我必须将脚本从外部JS文件移动到我的PHP文件,以便我可以从PHP获取时间戳。 (您也可以通过隐藏的输入值或其他方法执行此操作,但这是最简单的方法。)然后我必须将'formData' option添加到我的Uploadify调用以及一些获取时间戳并对其进行哈希处理的PHP代码使用唯一的盐(您应该将其更改为随机字符串):

<?php $timestamp = time();?>
<script>
$('#file_upload').uploadify({
    'swf'      : '/uploadify/uploadify.swf',
    'uploader' : '/uploadify/uploadify.php',
    'formData' : {
        'timestamp' : '<?php echo $timestamp;?>',
        'token'     : '<?php echo md5("unique_salt" . $timestamp);?>'
    }
});
</script>

虽然版本3.2中似乎需要此代码,但implementation documentation中未提及此代码。我不得不查看下载包中的index.php文件来查找它。

答案 2 :(得分:1)

尝试在上传文件夹

之前提供“〜/”

(或)这是整个脚本:

<script type="text/javascript">
    $(window).load(
function () {
    $("#fileInput1").uploadify({
        'uploader': 'scripts/uploadify.swf',
        'cancelImg': 'images/cancel.png',
        'buttonText': 'Browse Files',
        'script': 'UploadVB.ashx',
        'folder': 'uploads',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'queueSizeLimit': 9999,
        'simUploadLimit': 2,
        'sizeLimit': 4000000,
        'multi': true,
        'auto': true,
        'onComplete': function (event, queueID, fileObj, response, data) {
                        $("#thumbnail").append(response)

        },

        'onError': function (event, ID, fileObj, errorObj) {
            alert(errorObj.type + ' Error: ' + errorObj.info);
        }


    });
}
);
</script>

答案 3 :(得分:1)

对我来说,我所要做的就是在$ targetPath定义中删除$ _SERVER ['DOCUMENT_ROOT']。然后我还使用“上传”代替“/ uploads”作为我的$ targetFolder。