簡體   English   中英

上載多個圖像php,mysql

[英]Uploading multiple images php, mysql

我想上傳一個項目,並附有該項目的多個圖像,這些圖像應存儲在mysql數據庫中,以及一個文件夾“ uploads”。 插入項目的代碼效果很好,但是上傳圖像的代碼未執行。 我有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); 
                    }
               }
        }

      }
    }
    }
    ?>

我加了

   enctype="multipart/form-data"

的形式,它的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM