【问题标题】:Uploadify script no errors but no uploads eitherUploadify 脚本没有错误,但也没有上传
【发布时间】:2011-11-05 10:48:02
【问题描述】:

我正在使用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>

当我尝试上传图片时,一切正常(似乎也是如此),并且显示 - Complete ...

但没有上传任何内容。

有什么想法吗?

更新:

这是我的服务器结构路径:

我的路径:

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

【问题讨论】:

    标签: javascript jquery uploadify


    【解决方案1】:

    对我来说,问题出在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 的文件夹,这是上传文件的位置。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      我在这里尝试了所有其他建议,但没有一个对我有用。然后我意识到 Uploadify 的 3.2 版本(可能还有以前的版本)需要时间戳和哈希令牌才能完成上传。

      首先,我必须将脚本从外部 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 文件才能找到它。

      【讨论】:

        【解决方案3】:

        尝试在上传文件夹之前添加“~/”

        (或)这里是整个脚本:

        <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>
        

        【讨论】:

        • 我正在使用 php 上传器 .. 所以 ... 'uploader' : '/files/uploadify.php'... 但我已经添加了你的 oncomplete 和 onerror 但我没有得到任何回报。
        • 唯一的问题是所需的上传文件夹确保参考路径与您的uploadify.php中的上传文件夹正确
        【解决方案4】:

        对我来说,我所要做的就是摆脱 $targetPath 定义中的 $_SERVER['DOCUMENT_ROOT'] 。然后我还使用“uploads”而不是“/uploads”作为我的 $targetFolder。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-11
          • 2012-07-20
          • 1970-01-01
          • 1970-01-01
          • 2013-05-08
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多