用PHP和mySQL上传文件不需要';行不通
我尝试上传照片并将路径保存在mySQL数据库中 我需要做什么才能将文件保存到数据库 控制器:用PHP和mySQL上传文件不需要';行不通,php,html,mysql,model-view-controller,mysqli,Php,Html,Mysql,Model View Controller,Mysqli,我尝试上传照片并将路径保存在mySQL数据库中 我需要做什么才能将文件保存到数据库 控制器: public function doAddPhoto(){ $view = new View('addPhoto'); if(isset($_POST['uploadBtn'])) { $file = rand(1000,100000)."-".$_FILES['file']['name']; $file_loc = $_FILES['file'
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>
选择要上载的图像:
您也可以发布html吗?找不到输入的名称。看起来您的$\u文件['file']
变量不存在。你能出示你的表格吗?上传的文件()方法在哪里?你也能发布html吗?找不到输入的名称。看起来您的$\u文件['file']
变量不存在。请出示您的表格好吗?上传的文件()方法在哪里?