我尝试上传照片并在mySQL Db中保存路径
我需要做什么才能将文件保存到数据库?
控制器:
public function doAddPhoto(){
$view = new View('addPhoto');
if(isset($_POST['uploadBtn']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$photoModel = new PhotoModel();
$photoModel->create($file, $file_type, $file_size);
}
$view->display();
}
型号:
public function create($file, $file_type, $file_size){
$query = "INSERT INTO $this->tableName (file, type, size) VALUES (?, ?, ?)";
$statement = ConnectionHandler::getConnection()->prepare($query);
$statement->bind_param('ssi', $file, $file_type, $file_size);
$success = $statement->execute();
if (!$success) {
throw new Exception($statement->error);
}
}
它显示以下错误消息:
Notice: Undefined index: file in /PhotoController.php on line 6
Notice: Undefined index: file in /PhotoController.php on line 7
Notice: Undefined index: file in /PhotoController.php on line 8
Notice: Undefined index: file in /PhotoController.php on line 9
Fatal error: Call to a member function bind_param() on boolean in /PhotoModel.php on line 5
HTML表单:
<form action="/photo/doAddPhoto" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="uploadBtn">
</form>