我要我的答案作爲一個更全面的解釋添加@ SachinMarwa自己的答案。我提交的代碼並不是不同的代碼,而是添加了他的答案中未提及的一些行和特定內容。
即使看起來他的回答在技術上是正確的,也許他的回答也是正確的,但他們並不適合我。我必須研究這個問題以找出真正發生的事情,並且我瞭解瞭如何編寫我自己的解決方案的過程中瞭解了足夠多。
首先,請參閱Nana Partykar的評論,「在您的控制器中,我無法看到任何is_uploaded_file()函數?」該評論告訴我們,人們誤解了兩個名稱相似但文件不同的文件。我知道,因爲有一段時間我以爲他們一定是指同一個文件,控制器文件(名爲「Uploader.php」)。我幾乎可以看到所有這些問題都提到了相同的「如何使用Ajax上傳多個文件」教程,其中包括我自己的版本。我們都在使用的代碼是完全一樣的。
但是,控制器文件是「Uploader.php」。你看到$ this-> upload-> do_upload()或$ this-> upload-> do_upload('userfile')甚至$ this-> upload-> do_upload('files'),這是指系統/庫模塊文件名爲「Upload.php」。請注意,在調用do_upload()函數之前,您必須調用以下行:$ this-> load-> library('upload',$ config);
Sachin Marwha給了我們一個循環遍歷$ _FILES ['userfile']數組的循環。假設你上傳了三張照片。每個$ _FILES ['userfile']元素本身由5個「屬性」組成:name,type,tmp_name,error,size。您可以在PHP上看到這些$ _FILE屬性。
你只是想傳遞一個文件在一次do_upload()。您不希望一次將所有三個(甚至20個)文件傳遞給do_upload。這意味着在調用do_upload()之前,必須將$ _FILES ['userfile']數組分解爲單個文件。爲此,我創建$ _FILES數組的$ _FILES ['f']元素。我通過在do_upload($ file ='userfile')函數中的system/library/Upload.php文件中設置斷點來了解這一點,以瞭解我在哪裏得到臭名昭着的「沒有選擇要上傳的文件」 (包括我自己)一直在抱怨。你會發現,這個函數使用你的表單發送給你的控制器的原始$ _FILES數組。但它實際上只使用表單中輸入類型=文件的名稱。如果您不告訴它表單輸入的名稱,它將默認爲$ _FILES ['userfile']。事實證明,這是我最大的問題,因爲如果我使用輸入字段的名稱,那麼該字段會傳遞一個數組或一組文件,而不僅僅是一個文件。所以我不得不做一個特殊的$ _FILES ['f]元素,並且只傳遞$ _FILES ['f']。
這裏是我做的方式,相信我,我嘗試了所有版本的這個頁面和其他人,而不僅僅是一個StackOverflow的,但其他的教程上,還有:
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i < $cpt; $i++)
{
unset($config);
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = FALSE;
$config['file_name'] = $_FILES['userfile']['name'][$i];
// Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop.
$_FILES['f']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (! $this->upload->do_upload('f'))
{
$data['errors'] = $this->upload->display_errors();
}
else
{
$data['errors'] = "SUCCESS";
}
unset($config);
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $path . $_FILES['userfile']['name'][$i];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['thumb_marker'] = '.thumb';
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$types = array('.jpg');
}
凡會取消for循環中的$ config數組,然後重新映射$ config數組,這就是爲每個圖片文件製作縮略圖的部分。
完整控制器上載功能:
public function upload_asset_photo()
{
$data = array();
$dateArray = explode("/",$this->input->post('date'));
$date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day
$cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys.
$padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003"
$path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path
if (!is_dir($path)) {
mkdir($path,0755,true); //makes the ile path, if it doesn't exist
}
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i < $cpt; $i++)
{
unset($config);
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = FALSE;
$config['file_name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (! $this->upload->do_upload('f'))
{
$data['errors'] = $this->upload->display_errors();
}
else
{
$data['errors'] = "SUCCESS";
}
unset($config);
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $path . $_FILES['userfile']['name'][$i];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['thumb_marker'] = '.thumb';
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$types = array('.jpg');
}
header('Content-Type: application/json');
echo json_encode($data);
}
在你** **控制器,我不能看到任何'is_uploaded_file()'函數? –
不,我沒有使用過這個功能..... –
do_upload();是用 –