我有一个cakephp应用程序。 我想在其中介绍文件上传功能。
我正在使用move_upload_file函数来上传在我的情况下返回false的文件 我已经在代码中添加了代码注释,以显示在运行时传递给move_uploaded_file函数的参数。
请在下面找到我的代码
$id = $this->Auth->user('userid');
$user = $this->Users->get($id, [
'contain' => []
]);
$target_dir = "C/Docs/Misc/";
$target_file = $target_dir . basename($_FILES['resume']['name']); //_$FILES has the proper file uploaded from the web client in the resume index.
if ($this->request->is(['patch', 'post', 'put'])) {
//if($resume["size"]>0){
try {
//move_uploaded_file is returning false.
//first parameter to the function has the file name for e.g. doc.text
//second parameter is C/Docs/Misc/doc.text
if (move_uploaded_file($_FILES['resume']['name'], $target_file)) {
Log::info("The file ". basename( $_FILES['resume']['name']). " has been uploaded.");
} else {
Log::error("Sorry, there was an error uploading your file.");
}
}catch(Exception $e){
Log::error("Sorry there was an error uploading file ",$e);
}
$user = $this->Users->patchEntity($user, $this->request->data);
$user->resume = $target_file;
//$fileData = fread(fopen($resume["tmp_name"],"r"),$resume["size"]);
//$user->resume = $fileData;
//}else{
//$//user->resume = null;
//}
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['controller'=> 'job', 'action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
最诚挚的问候,
Saurav