我想上传一个项目,同时附上项目的多个图像,这些图像应存储在mysql数据库中,文件夹也“上传”。 插入项目的代码运行良好,但上传图像的代码不会执行。我有2个表格图像和具有一对多关系的属性。
<?php
require('DBconnect.php');
if(isset($_POST['submit']))
{
// removes backslashes
$title = stripslashes($_REQUEST['title']);
$title = mysqli_real_escape_string($connection,$title);
$area = stripslashes($_REQUEST['area']);
$area = mysqli_real_escape_string($connection,$area);
$price = stripslashes($_REQUEST['price']);
$price = mysqli_real_escape_string($connection,$price);
$query = "INSERT into `properties` (propertyTitle, propertyArea, propertyPrice)
VALUES ('$title', '$area', '$price')";
$result = mysqli_query($connection,$query) or die(mysqli_error($connection));
$prpId= mysqli_insert_id($connection);
if($result){
echo "Property registered successfully";
$targetFolder = "uploads";
//the code below is not executed by the server. Reasons for which i have no clue
foreach($_FILES as $file => $fileArray)
{
if(!empty($fileArray['name']) && $fileArray['error'] == 0)
{
$getFileExtension = pathinfo($fileArray['name'], PATHINFO_EXTENSION);;
if(($getFileExtension =='jpg') || ($getFileExtension =='jpeg') || ($getFileExtension =='png') || ($getFileExtension =='gif'))
{
if ($fileArray["size"] <= 5000000)
{
$breakImgName = explode(".",$fileArray['name']);
$imageOldNameWithOutExt = $breakImgName[0];
$imageOldExt = $breakImgName[1];
$newFileName = strtotime("now")."-".str_replace(" ","-",strtolower($imageOldNameWithOutExt)).".".$imageOldExt;
$targetPath = $targetFolder."/".$newFileName;
if (move_uploaded_file($fileArray["tmp_name"], $targetPath))
{
$qry = "INSERT INTO images (image, propertyId)
VALUES ('".$newFileName."', '".$prpId."')";
$rs = mysqli_query($connection, $qry);
}
}
}
}
}
}
?>
答案 0 :(得分:0)
我添加了
enctype="multipart/form-data"
表格并且有效。