Jquery fileupload: upload a file to a new folder with name changed in php -
hi all: have challenge:
im using jquery fileuploader php blueimp: https://github.com/blueimp/jquery-file-upload
i'm modifying code implement uploader in web. can use correctly, documentation poor adds or mods:
i'm trying modify file uploaderhandler.php create folder username(unique), don't know put mkdir() function...
and want upload files changing names 1.txt,2.pdf,3.doc...etc,etc,etc
any help?
pd: i'm thinking 3 solutions:
1) put mkdir() function in login.php, , when user logs in, check folder exists , it's empty... , each time reloads .php files. not best solution, guess.
2) put mkdir() function in get_upload_path uploadhandler.php
3) put rename() function in get_unique_filename uploadhandler.php
edit: tryied 2) option: modified uploadhandler.php.
it works, create folder username, , put uploaded file in folder. in ajax don't receive response, , don't create response line:
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.'/', [...]
response in ajax in html/php:
$(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 + '%'); } }); });
could me response?
ok, challenge completed:
first: how upload files custom url, changed name of user:
in implementation of jquery fileupload of blueimp, must use 2 files control uploads: index.php , uploadhandler.php.
the first creates object:
error_reporting(e_all | e_strict); require('uploadhandler.php'); $upload_handler = new uploadhandler();
you must change to:
ob_start(); //get session //include files operate database , session include("../conection.php"); include("../session/session.php"); error_reporting(e_all | e_strict); require('uploadhandler.php'); //add var $options. array, can see in uploadhandler.php, in constructor //you modify data: upload_dir , upload_url $options = array('upload_dir'=>$usuario.'/', 'upload_url'=>$usuario.'/'); //then add array constructor declaration $upload_handler = new uploadhandler($options);
with modification can send constructor variable upload path.
i hope all.
edit: problem white spaces
to fight filename white spaces must change function:
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 existing filename if part of 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 white spaces in _ $name= str_replace(" ", "_", $name); return $name; }
Comments
Post a Comment