存储上传图像在数据库中的路径

时间:2014-12-19 08:40:59

标签: php html mysql sql mysqli

这是一个上传多个图片的脚本。如果我上传3张图片,那么3张图片会存储在服务器文件夹中,但在数据库中,每张图片路径都会存储两次,因此它会提供6个条目而不是3个条目

<?php
ob_start();
include('co_session.php');

if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 

    $target_path = "uploads/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable

        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       

      if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                //echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';

                $file_name_all.=$target_path."*";
                $filepath = rtrim($file_name_all, '*'); 
                //echo $filepath;
                $officeid = $_GET['id'];
                echo $officeid;

                $str= $filepath;
                $array =  explode('*', $str);
                foreach ($array as $item) 
                    {

                        $sql="INSERT INTO officeimage (offimage,officeid,lat,lon) VALUES ('$item','$officeid','$user_latitude','$user_longitude')";

                            if (!mysqli_query($con,$sql)) 
                                {
                                    die('Error: ' . mysqli_error($con));
                                }
                    }
            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
    //header("Location: co_request_sent.php ");
}
mysqli_close($con); 
?>

任何人都可以指出错误

1 个答案:

答案 0 :(得分:0)

我建议宣布&#34;常数&#34;像$validextensions外循环一样。 并且您声明$file_extension,但下一行是$ext[count($ext) - 1]

对于多个插入问题:你做一个foreach循环,在for循环中插入每个元素移动它们。

Loop #1
    Move image #1
    Add image #1 to Stack
    Subloop
        Insert image #1 from Stack
Loop #2
    Move image #2
    Add image #2 to Stack
    Subloop
        Insert image #1 from Stack
        Insert image #2 from Stack
Loop #3
    Move image #3
    Add image #3 to Stack
    Subloop
        Insert image #1 from Stack
        Insert image #2 from Stack
        Insert image #3 from Stack

最后:

  • 图片#1插入三次
  • 图片#2插入两次
  • 图片#3插入一次

TOTAL:6个条目

将foreach移动到循环外部或移除堆栈/ foreach以在每个循环转动时插入当前图像。