出于学习目的,我正在oop php中针对属性/广告开发cms。我正在尝试上传通过具有特定属性的数据透视表连接的多张照片,但是我无法插入这些照片。当我插入包含两张或更多张照片的属性时,我需要这些照片在数据透视表中具有不同的ID,但属性具有相同的ID。当时我成功拍摄了一张照片,但是拍摄了多张却出现了错误:
警告:explode()期望参数2为字符串,数组形式为 当我var dump $ tmp时,在第177行的C:\ xampp \ htdocs \ App \ Models \ Ad.php 变量,我得到null和
警告:end()期望参数1为数组,在中为null var dump时,第179行的C:\ xampp \ htdocs \ App \ Models \ Ad.php $ file_ext变量我得到空字符串
我正在使用三个表来做到这一点。照片(名称,扩展名,created_at,updated_at),property_photo(property_id,photo_id),属性(标题,描述,type_of_property,use_of_the_property,正交,位置...)。这是我的代码:
Ad Model:
public function createAd($data, $pht)
{
if (isset($data['photoExtension'])) {
$this->photoExtension = preg_replace('~(?<=a)\w~', "", $data['photoExtension']);
}
$this->photoExtension = strtolower(strrchr( $pht, '.' ));
$this->db->query("INSERT INTO properties (title, description, type_of_property, use_of_the_property, quadrature, location, price, sales_clerk_info, booked, type_of_market, type_of_payment, status) VALUES (:title, :description, :type_of_property, :use_of_the_property, :quadrature, :location, :price, :sales_clerk_info, :booked, :type_of_market, :type_of_payment, :status) ");
$this->db->bind(':title', $data['title']);
$this->db->bind(':description', $data['description']);
$this->db->bind(':type_of_property', $data['type_of_property']);
$this->db->bind(':use_of_the_property', $data['use_of_the_property']);
$this->db->bind(':quadrature', $data['quadrature']);
$this->db->bind(':location', $data['location']);
$this->db->bind(':price', $data['price']);
$this->db->bind(':sales_clerk_info', $data['sales_clerk_info']);
$this->db->bind(':booked', $data['booked']);
$this->db->bind(':type_of_market', $data['type_of_market']);
$this->db->bind(':type_of_payment', $data['type_of_payment']);
$this->db->bind(':status','1');
$this->db->execute();
$property_last_id = $this->db->lastId();
$this->db->query('INSERT INTO photos (name, extension) VALUES (:name, :extension)');
$this->db->bind(':name', $pht);
$this->db->bind(':extension', $this->photoExtension, PDO::PARAM_STR );
$this->db->execute();
$photo_last_id = $this->db->lastId();
$this->db->query('INSERT INTO property_photo (property_id, photo_id) VALUES (:property_id, :photo_id)');
$this->db->bind(':property_id', $property_last_id);
$this->db->bind(':photo_id', $photo_last_id);
$this->db->execute();
return true;
}
public function photoValidate($file)
{
if (!empty($file['name'])) {
$file_name = $file['name'];
$file_size = $file['size'];
$file_tmp = $file['tmp_name'];
$file_type = $file['type'];
$file_error = $file['error'];
$random = sha1(microtime());
$tmp = explode('.', $file_name);
$new_photo_name = $random . '.' . $tmp[1];
$file_ext = strtolower(end($tmp));
//var_dump($tmp); null
//var_dump($file_ext); empty string
$photo_validate = '';
$extensions = ["jpeg", "jpg", "png"];
if (in_array($file_ext, $extensions) === false) {
return 'extension not allowed, please choose a JPEG or PNG file.';
} else {
if ($file_size > 2097152 || $file_error === 1) {
return 'File size must be less than 2 MB';
} else {
$value = true;
return $data = [$value, $file_tmp, $new_photo_name];
}
}
} else {
return false;
}
}
广告控制器:
public function createAction()
{
$userinfo = $this->Auth->Auth(array('admin', 'moderator'));
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$data = [
'title' => trim($_POST['title']),
'description' => trim($_POST['description']),
'type_of_property' => trim($_POST['type_of_property']),
'use_of_the_property' => trim($_POST['use_of_the_property']),
'quadrature' => trim($_POST['quadrature']),
'location' => trim($_POST['location']),
'price' => trim($_POST['price']),
'sales_clerk_info' => trim($_POST['sales_clerk_info']),
'booked' => trim($_POST['booked']),
'type_of_market' => trim($_POST['type_of_market']),
'type_of_payment' => trim($_POST['type_of_payment']),
'title_err' => '',
'description_err' => '',
'type_of_property_err' => '',
'use_of_the_property_err' => '',
'quadrature_err' => '',
'location_err' => '',
'price_err' => '',
'sales_clerk_info_err' => '',
'booked_err' => '',
'type_of_market_err' => '',
'type_of_payment_err' => ''
];
if (empty($data['title'])) {
$data['title_err'] = 'Please enter your title!!!';
}
if (empty($data['description'])) {
$data['description_err'] = 'Please enter your description!!!';
}
if (empty($data['type_of_property'])) {
$data['type_of_property_err'] = 'Please select your type!!!';
}
if (empty($data['use_of_the_property'])) {
$data['use_of_the_property_err'] = 'Please enter use of the property!!!';
}
if (empty($data['quadrature'])) {
$data['quadrature_err'] = 'Please enter your quadrature!!!';
}
if (empty($data['location'])) {
$data['location_err'] = 'Please enter your location!!!';
}
if (empty($data['price'])) {
$data['price_err'] = 'Please enter your price!!!';
}
if (empty($data['sales_clerk_info'])) {
$data['sales_clerk_info_err'] = 'Please enter your info!!!';
}
if (empty($data['booked'])) {
$data['booked_err'] = 'Please select!!!';
}
if (empty($data['type_of_market'])) {
$data['type_of_market_err'] = 'Please select your type of market!!!';
}
if (empty($data['type_of_payment'])) {
$data['type_of_payment_err'] = 'Please select your type of payment!!!';
}
$photo_validate = $this->AdModel->photoValidate($_FILES['photo']);
if (empty($data['title_err']) && empty($data['description_err']) && empty($data['type_of_property_err']) && empty($data['use_of_the_property_err']) && empty($data['quadrature_err']) && empty($data['location_err']) && empty($data['price_err']) && empty($data['sales_clerk_info_err']) && empty($data['booked_err']) && empty($data['type_of_market_err']) && empty($data['type_of_payment_err']) && $photo_validate[0] === true) {
move_uploaded_file($photo_validate[1],"public/photos/".$photo_validate[2]);
if ($this->AdModel->createAd($data, $photo_validate[2])) {
redirect('ads/index');
} else {
if ($photo_validate === false) {
$photo_validate='Please select image';
} else {
if ($photo_validate[0] === true) {
$photo_validate='';
}
}
$data=[
'photo_validate'=>$photo_validate
];
die('Something went wrong!');
}
} else {
$this->view->render('ads/create', $data, $userinfo);
}
} else {
$data = [
'photo_validate'=>'',
'title' => '',
'description' => '',
'type_of_property' => '',
'use_of_the_property' => '',
'quadrature' => '',
'location' => '',
'price' => '',
'sales_clerk_info' => '',
'booked' => '',
'type_of_market_id' => '',
'type_of_payment' => '',
'title_err' => '',
'description_err' => '',
'type_of_property_err' => '',
'use_of_the_property_err' => '',
'quadrature_err' => '',
'location_err' => '',
'price_err' => '',
'sales_clerk_info_err' => '',
'booked_err' => '',
'type_of_market_err' => '',
'type_of_payment_err' => ''
];
$this->view->render('ads/create', $data, $userinfo);
}
}
create.php
<form action="/ads/create" method="POST" enctype="multipart/form-data">
<div class="form-group row">
<div class="col-sm-12">
<h5>Upload property image</h6>
<input type="file" name="photo[]" multiple class="form-control form-control-lg"/>
</div>
/div>
<div class="form-group">
<button type="submit" name="submit" class="form-control btn btn-primary">Submit</button>
</div>
</form>
任何帮助将不胜感激。
答案 0 :(得分:0)
看看用于插入多个文件的$ _FILES数组格式。 This answer和this的php文档页面将对您有用。
您期望在photoValidate()$ file ['name']中输入字符串,但是有一个数组,所以出现错误。
最好和最简单的方法是使用symfony http-foundation component之类的东西。
控制器:
public function createAction()
{
$request = Request::createFromGlobals();
//...
$photo_validate = $this->AdModel->photoValidate($request->files->get('photo'));
//...
}
此外,这种验证非常混乱。您也可以使用symfony validator component。