【问题标题】:PHP AJAX multiple files and automaticlly upload after selectionPHP AJAX 多文件并选择后自动上传
【发布时间】:2014-10-17 23:58:40
【问题描述】:

我正在尝试为 soundcloud、hulkshare 等音频文件创建一个上传表单,用于上传文件。

它的工作原理是=

  1. 您点击了上传按钮。
  2. 您选择了多个文件。
  3. 点击 ENTER 或 OPEN(windows) 后,文件开始按字母顺序上传,每个文件都有自己的进度条。
  4. 文件上传完成后,您可以单击(侧边栏更改(ajax))它来编辑/添加诸如
    • 艺术家
    • 标题
    • 标签
    • ...
  5. 文件将与标题、艺术家等标准数据一起保存...但如果更改并单击“保存”项目数据将会更改。

到目前为止我所拥有的是......

上传.html

<form action="/process/ajax/upload.php" method="post" class="upload" enctype="multipart/form-data">
<input type="file" name="file"  id="file" style="display:none" multiple>
<div class="main-box center-center  clearfix" id="fileupload">
<div class="center-center text-center">
<button type="submit" onClick="return false;" name="upload" onClick="selectfile" class="btn btn-success upload btn-lg">
<span class="fa fa-upload"></span>
<strong>upload</strong>
</button>
<p>Or you can DRAG & DROP </p>
</div>
</div>
</form>

上传.js

$(document).ready(function(e) {
    $('button.upload').on('click',function(){
        $('input[type=file]').click();
    });

    $('input[type=file]').change(function() {
        var res = $('div#results'); 
    $(res).show('slow');
        var values = $('input[type=file]').val();
        $.ajax({
            url: "/process/ajax/upload.php",
            type: "post",
            data: values,
        });
    });
});

上传.php

if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
    ############ Edit settings ##############
    $UploadDirectory    = '/uploads/'; //specify upload directory ends with / (slash)
    ##########################################

    /*
    Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini". 
    Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit 
    and set them adequately, also check "post_max_size".
    */

    //check if this is an ajax request
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
        die();
    }


    //Is file size is less than allowed size.
    if ($_FILES["FileInput"]["size"] > 5242880) {
        die("File size is too big!");
    }

    //allowed file type Server side check
    switch(strtolower($_FILES['FileInput']['type']))
        {
            //allowed file types
            case 'image/png': 
            case 'image/gif': 
            case 'image/jpeg': 
            case 'image/pjpeg':
            case 'text/plain':
            case 'text/html': //html file
            case 'application/x-zip-compressed':
            case 'application/pdf':
            case 'application/msword':
            case 'application/vnd.ms-excel':
            case 'video/mp4':
                break;
            default:
                die('Unsupported File!'); //output error
    }

    $File_Name          = strtolower($_FILES['FileInput']['name']);
    $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
    $Random_Number      = rand(0, 9999999999); //Random number to be added to name.
    $NewFileName        = $Random_Number.$File_Ext; //new file name

    if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName ))
       {
        // do other stuff 
               die('Success! File Uploaded.');
    }else{
        die('error uploading File!');
    }

}
else
{
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}

这就是我卡住的地方... 这些文件最终应位于 uploads(根)文件夹中。 因此,如果有人可以帮助我通过“上传”部分,我可以自己弄清楚 PHP 部分。

【问题讨论】:

    标签: javascript php jquery ajax file-upload


    【解决方案1】:

    那么问题是什么,文件最终没有出现在上传目录中?你有任何错误吗?假设你的upload.php 和uploads 目录在同一级别,试试

    $DS = DIRECTORY_SEPARATOR;
    $UploadDirectory = dirname(__FILE__) . $DS . 'uploads' . $DS;
    

    还要检查您上传目录的写入权限。如果您在 Windows 上本地工作,则应该没有问题。但是,如果这是您的生产服务器并且很可能是 Linux,请使用您的 FTP 客户端更改目录权限(通常右键单击文件夹并“设置权限”或类似内容)并输入值 775。这样您就可以成功了可由网络服务器用户写入。

    【讨论】:

    • 抱歉,忘记写出问题所在。多亏了你,现在一切正常。
    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多