2014-11-12 2 views
1

안녕하세요 : https://github.com/blueimp/jQuery-File-UploadJQuery와 파일 업로드는 : PHP 변경 이름의 새 폴더에 파일을 업로드

내가 코드를 수정하고 있습니다 : blueimp에서 PHP를위한

임 사용 JQuery와 fileuploader : 나는 또 다른 도전이 내 웹에 업 로더를 구현하는 방법. 올바르게 사용할 수는 있지만 특정 추가 또는 개조 물에 관한 문서는 적합하지 않습니다.

UploaderHandler.php 파일을 수정하여 사용자 이름이 고유 한 폴더를 만들려고 시도하지만 WHERE 내에서 mkdir() 함수 ...

을 넣고, 2.pdf, 3.doc을 ... 1.TXT하기 위해 이름을 변경 파일을 업로드하려는 등, 등, 등

어떤 도움 ?

PD : 내가 약 3 솔루션을 생각하고 있어요 :

1)이 login.php 내에서 mkdir() 함수를 놓고, 폴더가 존재하고 사용자가 로그인 할 때, 그것은 확인이 비어 .. . 그리고 그가 특정 .php 파일을 다시로드 할 때마다. 최선의 해결책은 아닙니다.

2) uploadHandler.php

에서 get_upload_path에 MKDIR에게() 함수를 넣어

3) uploadHandler.php

에서 get_unique_filename 내 이름 바꾸기() 함수를 넣어

편집 : 난 그냥 2) 옵션을 tryied : UploadHandler.php를 수정했습니다.

사용자 이름으로 폴더를 만들고 업로드 한 파일을 폴더에 넣습니다. 그러나 AJAX에 나는 응답을받지 못한 및 응답 라인을 생성하지 않는 :

UploadHandler.php 다음 HTML/PHP에서 AJAX에서

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.'/', 
[...] 

응답 :

$(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 + '%'); 
     } 
    }); 
}); 

응답을받을 수 있도록 도와 주시겠습니까?

+0

1) 네,하지 않는 것이 좋습니다 # 2) 야아,하지만 모두를위한, 업로드가 시작될 때만 확인하고 존재하지 않으면 # 2a) 세션을 만들거나 그와 비슷한 작업을한다면 dir을 만듭니다. 'upload_dir_exists'와 같이 var를 true 또는 false로 저장하고 업로드시 확인 만하면됩니다. dir은 # 3) 실제로 존재하거나, 2a)처럼 var에 업로드 된 파일의 현재 수를 저장하고 그것에 확장 만 넣는다. - 주어진 dir & name 아래에서 파일을 이동/복사하십시오. – UnskilledFreak

답변

2

좋아, 과제 완료 :

첫째 : 사용자의 이름으로 변경 사용자 정의 URL, 파일을 업로드하는 방법 :

BlueImp의 jQuery를 파일 업로드의 구현에서, 당신은이 개 파일을 사용해야합니다 업로드를 제어하는 ​​: index.php와 uploadhandler.php. 생성자 변수 업로드 경로에 보낼 수있는이 수정으로

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

:

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

$upload_handler = new UploadHandler(); 

당신은 그것을 변경해야합니다

첫 번째

는 개체를 만듭니다.

여러분 모두에게 도움이되기를 바랍니다.

편집 : 공백

에 문제가이 기능을 변경해야합니다 파일 이름 공백 싸울 :

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; 
} 
관련 문제