2012-11-23 2 views
1

jQuery-File-Upload을 PHP 및 데이터베이스와 함께 사용 하시겠습니까? 이미지를 업로드하거나 삭제할 때 이미지에 대한 행을 삽입하거나 삭제하고 해당 이미지를 디렉토리에 업로드 할 때 각 이미지의 이름이 time()이됩니다.Jquery-File-Upload PHP를 마지막 릴리스에서 데이터베이스와 함께 사용하는 방법은 무엇입니까?

내가 upload.class.php하는 편집하지만 마지막 릴리스 has index.php and UploadHandler.php only이 필요하다는 것을 말해 구글을 통해 발견 된 모든 결과 ...

파일 UploadHandler.php 코드

public function post($print_response = true) { 
     if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') { 
      return $this->delete($print_response); 
     } 
     $upload = isset($_FILES[$this->options['param_name']]) ? 
      $_FILES[$this->options['param_name']] : null; 
     // Parse the Content-Disposition header, if available: 
     $file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ? 
      rawurldecode(preg_replace(
       '/(^[^"]+")|("$)/', 
       '', 
       $_SERVER['HTTP_CONTENT_DISPOSITION'] 
      )) : null; 
     $file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ? 
      $_SERVER['HTTP_CONTENT_DESCRIPTION'] : null; 
     // Parse the Content-Range header, which has the following form: 
     // Content-Range: bytes 0-524287/2000000 
     $content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ? 
      preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null; 
     $size = $content_range ? $content_range[3] : null; 
     $info = array(); 
     if ($upload && is_array($upload['tmp_name'])) { 
      // param_name is an array identifier like "files[]", 
      // $_FILES is a multi-dimensional array: 
      foreach ($upload['tmp_name'] as $index => $value) { 
       $info[] = $this->handle_file_upload(
        $upload['tmp_name'][$index], 
        $file_name ? $file_name : $upload['name'][$index], 
        $size ? $size : $upload['size'][$index], 
        $file_type ? $file_type : $upload['type'][$index], 
        $upload['error'][$index], 
        $index, 
        $content_range 
       ); 

      } 
     } else { 
      // param_name is a single object identifier like "file", 
      // $_FILES is a one-dimensional array: 
      $info[] = $this->handle_file_upload(
       isset($upload['tmp_name']) ? $upload['tmp_name'] : null, 
       $file_name ? $file_name : (isset($upload['name']) ? 
         $upload['name'] : null), 
       $size ? $size : (isset($upload['size']) ? 
         $upload['size'] : $_SERVER['CONTENT_LENGTH']), 
       $file_type ? $file_type : (isset($upload['type']) ? 
         $upload['type'] : $_SERVER['CONTENT_TYPE']), 
       isset($upload['error']) ? $upload['error'] : null, 
       null, 
       $content_range 
      ); 
     } 
     return $this->generate_response($info, $print_response); 
    } 



public function delete($print_response = true) { 
      $file_name = $this->get_file_name_param(); 
      $file_path = $this->get_upload_path($file_name); 
      $success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path); 
      if ($success) { 
       foreach($this->options['image_versions'] as $version => $options) { 
        if (!empty($version)) { 
         $file = $this->get_upload_path($file_name, $version); 
         if (is_file($file)) { 
          unlink($file); 
         } 
        } 
       } 
      } 
      return $this->generate_response($success, $print_response); 
     } 

와 클래스 UploadHandler있다 mysql에서 파일 이름을 삽입하거나 삭제하기 위해 어떤 행을 추가해야합니까?

PS : 나는 PHP

답변

5

사용 나는 docementation를 사용하고 지금은 대신 upload.class.php이 편집 파일이 필요 파일라고 할 수 UploadHandler.php 하고 다음을 사용하여보다 :

이 줄 검색 - > $ this-> 옵션 = 배열 ​​(

다음 줄에 다음 코드를 추가합니다

// mysql connection settings 
'database' => '**YOUR DATABASE**', 
'host' => '**localhost**', 
'username' => '**YOUR USERNAME**', 
'password' => '**YOUR PASSWORD**', 
// end 
function query($query) { 
$database = $this->options['database']; 
$host = $this->options['host']; 
$username = $this->options['username']; 
$password = $this->options['password']; 
$link = mysql_connect($host,$username,$password); 
if (!$link) { 
die(mysql_error()); 
} 
$db_selected = mysql_select_db($database); 
if (!$db_selected) { 
die(mysql_error()); 
} 
$result = mysql_query($query); 
mysql_close($link); 
return $result; 
} 

그래서, 내가 사진을 업로드이 기능을 설명 데이터베이스 에 파일 세부 정보를 추가 :그래서 지금 당신은 handle_file_upload 기능 후에 예를 들어, 다음 코드를 붙여 넣습니다, SQL 쿼리에 대한 함수를 작성 &을 복사해야 여기에 우리가 데이터베이스에 사진 이름은 우리가 클램프 사이의 문자열 함수 쿼리를 호출이 기능에 너무도 또한 upload.class.php

function add_img($whichimg) 
{ 
$add_to_db = $this->query("INSERT INTO yourtable (**yourcolumnone**) VALUES ('".$whichimg."')") or die(mysql_error()); 
return $add_to_db; 
} 

을이 기능을 추가 저장할 수 있습니다.

다른 세부 정보 (예 : 파일 크기)도 삽입 할 수 있습니다.

적어도 handle_file_upload 함수의 끝에 다음 코드를 사용하여이 함수를 호출해야합니다. 이 코드의 아래 또는 위에 다음 코드를 붙여 넣으십시오 : $ file-> size = $ file_size; 우리가 전에 만든 항목을 삭제 우리가 만든 항목을 삭제

$file->upload_to_db = $this->add_img($file->name); 

우리는 또한 그것을 삭제하는 SQL 쿼리를 만드는 새로운 함수를 작성, 매우 간단합니다.

function delete_img($delimg) 
{ 
$delete_from_db = $this->query("DELETE FROM yourtable WHERE yourcolumnone = '$delimg'") or die(mysql_error()); 
return $delete_from_db; 
} 

이제는 delete 함수의 함수를 호출해야합니다. delete 함수로 이동하여 다음 행을 검색하십시오. if ($ success) {다음 코드를 붙여 넣으십시오. 사용자 정의 기능을 추가하기 위해) 문서에 설명 된대로 (기본 방법에 과부하를해야

+0

로키, 당신에 의해 뜻 무엇을'를 붙여 이 코드는 다음과 같습니다. if ($ success) {'를'$ this-> delete_img ($ file_name);'로 대체하거나'if ($ success) {'??? – Wilf

+0

제 해석 : Loki가 제공하는 코드로 * if 구조체 내의 코드를 덮어 씁니다. 결국, 다음과 같이 보일 것입니다 :'if ($ success) {$ this-> delete_img ($ file_name); }' – gibberish

+0

그가 의미하는 바는'if ($ success) {'그러나'foreach ($ this-> options [ 'image_versions'] $ version => $ options) {'success ' '. 나는 그것을 사용하고 그것은 작동합니다. – tatty27

0

$this->delete_img($file_name); 

즐기 =). 원본 파일을 수정하면 플러그인의 새 버전 (또는 수정 사항)이 출시 될 때 더 많은 작업이 수행됩니다.내 자신의 목적을 위해

, 나는 기본적으로 단지 /server/php/index.php 파일 원본을 확장하고 수정 한 사용자 정의 클래스 정의 :

class myUploadHandler extends UploadHandler { 
    //do your stuff 
    //for exemple if you are using a DB: 
    //overload methods like handle_file_upload() for insertion in a DB, 
    //set_additional_file_properties() if you need more fields linked to each uploaded file 
    // or delete() also if you want to remove from DB 
} 
$upload_handler = new myUploadHandler(array(
    'user_dirs' => true, 
    'download_via_php' => true, 
));