【问题标题】:Jquery fileupload: upload a file to a new folder with name changed in phpJquery fileupload:将文件上传到在php中更改名称的新文件夹
【发布时间】:2015-01-09 16:26:34
【问题描述】:

大家好:我还有另一个挑战:

我正在使用来自 blueimp 的 php 的 Jquery 文件上传器:https://github.com/blueimp/jQuery-File-Upload

我正在修改代码以在我的网络中实现上传器。 我可以正确使用它,但是关于某些添加或模组的文档很差:

我正在尝试修改文件 UploaderHandler.php 以使用用户名(唯一)创建一个文件夹,但我不知道将我的 mkdir() 函数放在哪里...

并且想上传将名称更改为 1.txt、2.pdf、3.doc...等等的文件

有什么帮助吗?

PD:我正在考虑 3 个解决方案:

1) 将我的 mkdir() 函数放在 login.php 中,当用户登录时,它会检查文件夹是否存在并且它是空的......并且每次他重新加载某些 .php 文件时。我猜这不是最好的解决方案。

2) 将 mkdir() 函数从 uploadHandler.php 放入 get_upload_path 中

3) 将我的 rename() 函数放入来自 uploadHandler.php 的 get_unique_filename 中

编辑:我刚刚尝试了 2) 选项:我修改了 UploadHandler.php。

它工作,使用用户名创建文件夹,并将上传的文件放入文件夹中。但在 AJAX 中,我没有收到响应,也没有创建响应行:

上传处理程序.php:

function __construct($options = null, $initialize = true, $error_messages = null){
    ob_start();
    include('../conexion.php');
    include('../session/session.php');

    $url_subida = dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$usuario.'/';
    if(!is_dir($url_subida)){
        mkdir($url_subida, 0777);
    }
    else{
        error_log($url_subida);
    }

    $this->options = array(
        'script_url' => $this->get_full_url().'/',
        'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$usuario.'/',
        'upload_url' => $this->get_full_url().'/'.$usuario.'/',
[...]

HTML/php 中的 AJAX 响应:

$(function(){
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data){
            $.each(data.result.files, function (index, file) {
                numarch++;
                $('<div id="archivo_'+numarch+'" />').appendTo('#files');
                if (file.error){
                    $('<img src="img/x.png" title="Error" alt="Error"/>').appendTo('#archivo_'+numarch);
                    $('<p class="text-danger"/>').text(file.error).appendTo('#archivo_'+numarch);
                }
                else{
                    var newFileDiv = $("<img title='Archivo subido OK' alt='Archivo subido OK'/>
                                        <p>"+file.name+"</p>
                                        <div id='borrarDiv' name='borrarDiv' class='btn btn-danger delete' onclick= borrar_archivo ('archivo_"+numarch+"','"+file.deleteUrl+"','"+file.deleteType+"','"+numarch+"')>
                                            <i class='glyphicon glyphicon-trash'></i>
                                        <span>Borrar</span></div>");
                    $('#archivo_'+numarch).append(newFileDiv);
                }
            });
        },
        progressall: function (e, data){
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').html(progress + '%');
            $('#progress .progress-bar').css('width',progress + '%');
        }
    });
});

你能帮我得到答复吗?

【问题讨论】:

  • 1) 是的,不推荐 #2) 是的,但不是全部,仅在上传开始时检查,如果不存在则创建目录 #2a) 如果您使用会话或类似的东西 -使用 true 或 false 存储类似 'upload_dir_exists' 的 var 并仅在上传时检查,这将比检查 dir 是否存在更快 # 3) 确实,或者像 2a) 将上传文件的当前计数存储在 var 中并仅放置扩展名在它上面 - 然后移动/复制给定目录和名称下的文件

标签: php jquery file-upload jquery-file-upload mkdir


【解决方案1】:

好的,挑战完成:

第一:如何上传文件到自定义URL,由用户名改:

在BlueImp的jQuery Fileupload实现中,必须使用2个文件来控制上传:index.php和uploadhandler.php。

第一个创建对象:

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

$upload_handler = new UploadHandler();

您必须将其更改为:

ob_start(); //get session

//include files to operate with database and session
include("../conection.php");
include("../session/session.php");

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

//add var $options. It is an array, you can see it in uploadhandler.php, in the constructor
//you modify the data: the upload_dir and the upload_url
$options = array('upload_dir'=>$usuario.'/', 'upload_url'=>$usuario.'/');
//then add the array to the constructor declaration
$upload_handler = new UploadHandler($options);

通过此修改,您可以将变量上传路径发送给构造函数。

希望对大家有所帮助。

编辑:空格问题

要消除文件名空格,您必须更改此函数:

protected function get_unique_filename($file_path, $name, $size, $type, $error, $index, $content_range){
    while(is_dir($this->get_upload_path($name))){
        $name = $this->upcount_name($name);
    }
    // Keep an existing filename if this is part of a chunked upload:
    $uploaded_bytes = $this->fix_integer_overflow(intval($content_range[1]));
    while(is_file($this->get_upload_path($name))){
        if ($uploaded_bytes === $this->get_file_size($this->get_upload_path($name))){
            break;
        }
        $name = $this->upcount_name($name);
    }

    //converts the white spaces in _
    $name= str_replace(" ", "_", $name);

    return $name;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多