2011-08-16 55 views

回答

5

實際上,你可以用CodeIgniter的本地File Upload Class以方便上傳與Phil's Rest-Server這樣的:

/** 
* REST API Upload using native CI Upload class. 
* @param userfile - multipart/form-data file 
* @param submit - must be non-null value. 
* @return upload data array || error string 
*/ 
function upload_post(){ 
    if(! $this->post('submit')) { 
     $this->response(NULL, 400); 
    } 
    $this->load->library('upload'); 

    if (! $this->upload->do_upload()) { 
     $this->response(array('error' => strip_tags($this->upload->display_errors())), 404); 
    } else { 
     $upload = $this->upload->data(); 
     $this->response($upload, 200); 
    } 
} 
9

好吧,

這裏有另一種解決方案:FIle upload from a rest client to a rest server

但這些方法都工作了我。

然而,這是什麼工作;實際上工作。

首先,我不知道如果我的文件到達的方法,所以我改變了響應行:

function enter_post() 
    { 

     $this->response($_FILES); 
    } 

注意,這是測試您的REST方法的好方法。

您也可以輸出:

$這個 - >應答($ _ SERVER);

$這 - >響應($ _ POST);

我得到了以下JSON輸出:

{ 「文件」:{ 「名」: 「camel.jpg」, 「類型」:「應用程序/ octet-流「,」tmp_name「:」/ tmp/phpVy8ple「,」error「:0,」size「:102838}}

所以我知道我的文件在那裏。

我再改爲查找和移動文件的方法。我用常見的文件訪問腳本從臨時位置獲取文件並將其移動到新位置:

$uploaddir = '/home/me/public_html/uploads/'; 

    $uploadfile = $uploaddir . basename($_FILES['file']['name']); 

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { 
     $data['status'] = 'uploaded';  
      } else { 
     $data['status'] = 'failed';   
      } 
+1

偉大的答案mr.ssaltman,真正healpfull –

2

試試這個代碼,圖像保存帶有時間戳和它的擴展..

$uploaddir = 'uploads/'; 
    $path = $_FILES['image_param']['name']; 
    $ext = pathinfo($path, PATHINFO_EXTENSION); 
    $user_img = time() . rand() . '.' . $ext; 
    $uploadfile = $uploaddir . $user_img; 
    if ($_FILES["image_param"]["name"]) { 
     if (move_uploaded_file($_FILES["image_param"]["tmp_name"],$uploadfile)) { 
echo "success"; 
    } 
0

以下是圖像路徑和它們各自的屬性在形式在控制器form.php的設定()函數

$config['upload_path']    = 'uploads/images/full'; 
    $config['allowed_types']   = 'gif|jpg|png'; 
    $config['max_width']    = '6000'; 
    $config['max_height']    = '6000'; 
    $config['encrypt_name']    = true; 
    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 
    $uploaded    = $this->upload->do_upload($imgfile); 
    if($uploaded) 

{ $文件名= $這個 - >復的結構llimage_upload1($這 - > upload->數據($ imgfile)); 返回$ filenames; }

這是定義上傳圖像拷貝到不同的文件夾作爲小的函數,縮略圖 功能fullimage_upload1($數據) { $這 - >負載>庫( 'image_lib');

//this is the larger image 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = 'uploads/images/full/'.$data['file_name']; 
    $config['new_image'] = 'uploads/images/small/'.$data['file_name']; 
    $config['maintain_ratio'] = TRUE; 
    /*$config['width'] = 600; 
    $config['height'] = 500;*/ 
    $this->image_lib->initialize($config); 
    $this->image_lib->resize(); 
    $this->image_lib->clear(); 

    //cropped thumbnail 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = 'uploads/images/full/'.$data['file_name']; 
    $config['new_image'] = 'uploads/images/thumbnails/'.$data['file_name']; 
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = 300; 
    $config['height'] = 258; 
    $this->image_lib->initialize($config); 
    $this->image_lib->resize(); 
    $this->image_lib->clear(); 

    return $data['file_name']; 
}