0
我試圖將圖片上傳到服務器,並將路徑插入數據庫。插入數據庫工作正常,該文件似乎可以在客戶端(內置角)上傳後在$ _FILES中找到。但是move_uploaded_file函數返回false,並且文件不在任何位置移動。可能是什麼問題呢?move_uploaded_file不會與ng-file-upload一起工作
VIEW.html
<div class="insert col-lg-3 col-sm-12">
<input type="file" name="file" ngf-select="uploadFiles($file)" ngf-pattern="'image/*'" />
</div>
CONTROLLER.js
$scope.uploadFiles = function(file) {
console.log(file);
Upload.upload({
url: '../backend/index.php/user/uploadfile?id=' + 1,
method: 'POST',
file: file,
sendFieldsAs: 'form'
}).then(function successCallback (response) {
console.log(response.data);
});
};
user.php的
/**
* @param $id
* @url uploadfile
* @return bool
*/
public function postUploadfile($id){
$mysqli = $this->db-> getConnection();
if(isset($_FILES)) {
$this->pic = new Pictures($mysqli);
$picture = $this->pic->upload($_FILES['file'], "", "book", $id);
}
}
PICTURES.CLASS.php
namespace BC;
class pictures {
private $img_path = 'test/';
private $m_conn;
function __construct(&$mysqli_conn) {
$this->m_conn = $mysqli_conn;
}
/*
* Finishing the upload of an image and creates the nessecary rows in the
* database.
*
* @param array $FILE Contains the element image_file which is an uploaded file
* @param string $description Description of the image
* @param string $type Which type should this belong to? Eg. periodical
* @param int $foreign_key The index of the row this image belongs to
*
* @return boolean True if everything went fine, otherwise false.
*/
public function upload($FILE, $description, $type, $foreign_key) {
// Insert into database
$SQL = 'INSERT INTO pictures (description) VALUE ("' . $description . '")';
if(!$this->m_conn->query($SQL)) {
return false;
}
// Get the id
$id = $this->m_conn->insert_id;
move_uploaded_file($_FILES['file']['tmp_name'], $this->img_path . $id . '.jpg');
return $this->img_path . $id . '.jpg';
// Create entry for it
$SQL = 'INSERT INTO picture_connections (picture_id, type, foreign_key) VALUES (' . $id . ', "' . $type . '", ' . $foreign_key . ')';
if(!$this->m_conn->query($SQL)) {
return false;
}
return true;
}
}
文件夾測試放在與pictures.class.php相同的級別,並具有正確的權限。似乎無法找出這一個。
其中move_uploaded_file返回false解決了這個?你不檢查任何東西。我認爲問題在於,在您移動圖像後,您會直接返回。下面的插入代碼永遠不會到達。 –
@RaphaelMüller在我測試的地方,如果move_uploaded_file返回false或true,我有一些不同的代碼。我也嘗試刪除返回後,但沒有改變。 – Nirakander