2012-01-04 86 views
0

我使用Jquery File Script來替換我現有的上傳腳本。在我當前的腳本中,我捕獲原始圖像數據並將其推送到我的數據庫,但它似乎不適用於jquery上傳腳本。爲jquery文件上傳腳本創建jpeg圖像數據

這裏是我如何使用它的私有函數內部:handle_file_upload

private function handle_file_upload($uploaded_file, $name, $size, $type, $error) { 
    $file = new stdClass(); 
    $file->name = $this->trim_file_name($name, $type); 
    $file->size = intval($size); 
    $file->type = $type; 
    $error = $this->has_error($uploaded_file, $file, $error); 
    if (!$error && $file->name) { 
     $file_path = $this->options['upload_dir'].$file->name; 
     $append_file = !$this->options['discard_aborted_uploads'] && 
      is_file($file_path) && $file->size > filesize($file_path); 
     clearstatcache(); 
     if ($uploaded_file && is_uploaded_file($uploaded_file)) { 
      // multipart/formdata uploads (POST method uploads) 
      if ($append_file) { 
       file_put_contents(
        $file_path, 
        fopen($uploaded_file, 'r'), 
        FILE_APPEND 
       ); 
      } else { 
       move_uploaded_file($uploaded_file, $file_path); 
      } 
     } else { 
      // Non-multipart uploads (PUT method support) 
      file_put_contents(
       $file_path, 
       fopen('php://input', 'r'), 
       $append_file ? FILE_APPEND : 0 
      ); 
     } 
     $file_size = filesize($file_path); 
     if ($file_size === $file->size) { 
       if ($this->options['orient_image']) { 
        $this->orient_image($file_path); 
       } 
      $file->url = $this->options['upload_url'].rawurlencode($file->name); 
      foreach($this->options['image_versions'] as $version => $options) { 
       if ($this->create_scaled_image($file->name, $options)) { 
        $file->{$version.'_url'} = $options['upload_url'] 
         .rawurlencode($file->name); 
       } 
      } 
     } else if ($this->options['discard_aborted_uploads']) { 
      unlink($file_path); 
      $file->error = 'abort'; 
     } 
     $file->size = $file_size; 
     $file->delete_url = $this->options['script_url'] 
      .'?file='.rawurlencode($file->name); 
     $file->delete_type = 'DELETE'; 


     // ====== NEW DB CODE ======= // 

     $server = 'XXXX'; 
     $user = 'XXX'; 
     $pass = 'XXX'; 
     $database= 'XXX'; 

     $db = mysql_connect($server,$user,$pass); 
     $db_select = mysql_select_db($database,$db); 

     ob_start(); // Start capturing stdout. 
     imagejpeg($uploaded_file); // As though output to browser. 
     $image_bin = mysql_real_escape_string(ob_get_contents()); // the raw jpeg image data. 
     ob_end_clean(); // Dump the stdout so it does not screw other output. 

     $user_id = 5; 
     $title = ''; 
     $caption = ''; 

     $mapimage = 0; 

     $SQL = "INSERT INTO images VALUES (NULL, {$user_id}, '{$title}', " 
     . "'{$caption}', '{$image_bin}')"; 
     $mrh = mysql_query($SQL); 
     if (!$mrh) { 
      $res->st=false; 
      $res->error="Error creating image record: " .$SQL. mysql_error($dbh); 
      return $res; 
     } 

     // ====== END NEW DB CODE ======= // 



    } 
    else { 
     $file->error = $error; 
    } 
    return $file; 
} 

,它把圖像,但vieing我得到一個破碎圖像圖標這讓我覺得它不是插入圖像時什麼它應該是。圖像的數據庫內容也很小(字節),這告訴我我沒有插入我認爲的我。

任何想法可能會失蹤?

回答

1

可能:

imagejpeg($file_path); 

代替:

imagejpeg($uploaded_file); 
+0

我看到文件大小在DB增加,但它仍然是非常小的,並且不顯示圖像... – Paul 2012-01-04 14:54:40

+0

嘗試使用的var_dump ($ image_bin)和那裏有什麼 – kaz 2012-01-04 14:56:00

+0

在這條線上是這個錯誤?你在你的PHP代碼中有錯誤。雙「或類似的東西 – kaz 2012-01-04 15:10:44