jqueryfileupload:将文件上载到一个新文件夹,并在php中更改名称

jqueryfileupload:将文件上载到一个新文件夹,并在php中更改名称,php,jquery,file-upload,jquery-file-upload,mkdir,Php,Jquery,File Upload,Jquery File Upload,Mkdir,大家好:我还有一个挑战: Im使用blueimp的Jquery fileuploader for php: 我正在修改代码以在我的web中实现上传程序。 我可以正确使用它,但文档中关于某些添加或修改的内容很差: 我试图修改UploaderHandler.php文件,创建一个用户名为(unique)的文件夹,但我不知道将mkdir()函数放在哪里 并且想上传文件,把名字改成1.txt,2.pdf,3.doc等等 有什么帮助吗 PD:我正在考虑3种解决方案: 1) 将my mkdir()函数放在lo

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

Im使用blueimp的Jquery fileuploader for php:

我正在修改代码以在我的web中实现上传程序。 我可以正确使用它,但文档中关于某些添加或修改的内容很差:

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

并且想上传文件,把名字改成1.txt,2.pdf,3.doc等等

有什么帮助吗

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

1) 将my mkdir()函数放在login.php中,当用户登录时,它会检查文件夹是否存在以及是否为空。。。每次他都会重新加载某些.php文件。我想这不是最好的解决办法

2) 将mkdir()函数放在uploadHandler.php的get_upload_路径中

3) 将my rename()函数放在uploadHandler.php中的get_unique_filename中

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

它可以工作,使用用户名创建文件夹,并将上载的文件放入文件夹中。但是在AJAX中,我不接收响应,也不创建响应行:

UploadHandler.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 + '%');
        }
    });
});
$(函数(){
$('#fileupload')。fileupload({
数据类型:“json”,
完成:功能(e,数据){
$.each(data.result.files,函数(索引,文件){
numarch++;
$(“”).appendTo(“#文件”);
if(file.error){
$('')。附录('#archivo'+numarch);
$('p class=“text danger”/>).text(file.error).appendTo('archivo+numarch);
}
否则{
var newFileDiv=$(“
“+文件名+”

博尔拉); $('#archivo'+numarch).append(newFileDiv); } }); }, progressall:功能(e、数据){ var progress=parseInt(data.loaded/data.total*100,10); $('#progress.progress bar').html(progress+'%'); $('#progress.progress bar').css('width',progress+'%'); } }); });

你能帮我得到答案吗?

好的,挑战已完成:

第一:如何将文件上载到自定义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;
}
1) 是的,不推荐#2)耶,但不是所有人,只在上传开始时检查,如果不存在则创建目录#2a)如果你使用会话或类似的东西-存储一个变量,如“upload#u dir#u exists”,并带有true或false,并且只在上传时检查,这将比检查目录是否存在更快#3)确实,或者像2a)将上传文件的当前计数存储在一个var中,并仅在其上放置扩展名,然后在给定的dir&name下移动/复制该文件