2014-12-16 12 views
5

Tôi có một biểu mẫu giúp tải nhiều hình ảnh lên thư mục máy chủ và lưu đường dẫn của họ trong cơ sở dữ liệu, vấn đề là đường dẫn của tất cả hình ảnh đang được tải lên một cột, và do đó tôi đang gặp vấn đề khi tìm nạp đường dẫn để hiển thị hình ảnh. Các giải pháp sẽ được rằng tôi hoặc là làm cho hình ảnh được lưu trữ trong cột khác nhau hoặc tìm cách để tách các hình ảnh, tuy nhiên tôi không thể làm cả hai.Lưu trữ đường dẫn của hình ảnh trong cột cơ sở dữ liệu khác nhau

Lối đi được bị lưu trữ trông giống như

uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png04aaa414c21dc057bc594b896124068e.png 

cấu trúc cơ sở dữ liệu bản demo

id offimage offimage1 offimage2 

Mã mà tôi có cho đến nay (theo mẫu và mã php trên trang khác nhau)

<form enctype="multipart/form-data" action="co_insert_office_image.php?id=<?php echo $_GET['id']; ?>" method="post"> 
     First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB. 
    <hr/> 
    <div id="filediv"><input name="file[]" type="file" id="file"/></div><br/> 
    <input type="button" id="add_more" class="upload" value="Add More Files"/> 
    <input type="submit" value="Upload File" name="submit" id="upload" class="upload"/> 
</form> 

co_insert_office_image.php

<?php 
ob_start(); 
include('co_session.php'); 
$con=mysqli_connect("localhost","root","","db"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
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']; 
       $sql = "UPDATE register_office SET offimage='$filepath' WHERE id='$officeid' "; 
          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); 
?> 
+0

Điều gì tạo ra một bảng khác với chỉ mục của cha mẹ và sau đó lưu trữ hình ảnh ở đó? –

+0

@Anand Ghaywankar vẫn sẽ lưu trữ tất cả đường dẫn hình ảnh trong một cột – Sam

+0

Làm cách nào để lưu trữ trong One Col? Một bảng khác sẽ có col như id, parent_id, img_path và nó sẽ có mối quan hệ hasMany với parent_table. Tất cả bạn phải duy trì Id bảng cha trong bảng hình ảnh –

Trả lời

1

cuối cùng tôi đã nhận các giải pháp, tôi thấy một số người đã đánh dấu câu hỏi này và họ có thể tìm kiếm câu trả lời, vì vậy ở đây nó là

đầu tiên kết nối với cơ sở dữ liệu, lấy dữ liệu và sau đó sử dụng đoạn mã sau

while($row1 = mysqli_fetch_assoc($result1)) 
    { 
    $str= $row1["offimage"]; 
    //print_r (explode("*",$str)); 

    $array = explode('*', $str); 
    foreach ($array as $item) { 
     //echo "<li>$item</li>"; 
     echo "<img src=\"http://localhost/cms/1/". $item . "\" height=\"200\" width=\"200\"/>"; 
    } 

} 
Các vấn đề liên quan