2017-02-23 100 views
0

我想从用户那里得到一张照片并插入到数据库中。用户应该在下面的输入字段中上传文件。另外,我想在需要时显示数据库中的图像。在MySql中保存和检索照片

<label>Photo</label> 
<input type="file" name="photo" required> 
<?php 
mysql_connect("localhost", "root", ""); mysql_select_db("prs"); 
if(isset($_POST['submit'])){ 
    $photo = $_FILES['photo']['name']; 
    { 
     $query = mysql_query("insert into date (photo) values ('$photo')"); 
    } 
} 
?> 
+0

上传服务器上的图像。将其路径保存在数据库中并在显示时将其从URL中拉出来 – Satya

+0

您可以请解释一下 – Udhayakkrishna

+0

首先提及[this](http://stackoverflow.com/questions/13470482/php-upload-image-to-directory) – Akshay

回答

1

您可以通过以下过程

  1. 做到这一点上传图像到服务器。你可以将它保存在图像文件夹或你想要的地方。

  2. 只将上传的图像路径存储在数据库中。恩。 /image/default.png。您也可以只保存图像名称作为数据库ex。 default.png

  3. 想要显示图像时从数据库中检索图像路径。

  4. 您可以使用图像标记 前,在网页中显示图像。 <img src="../images/default.png" />

0

最好的办法是在数据库表中保存文件路径或文件名。并将您的文件保存在服务器中。

下面是一个示例MULTIPLE图像上传和检索脚本为您的需要。我要在这里使用3个文件。 front.html,upload.php的最后preview.php

  1. Front.html

该文件包含了基本的HTML表单的内容。

<form enctype="multipart/form-data" action="upload.php" method="post"> 
<input id="uploadFile" type="file" name="files[]" class="img" /> 
<button class="btn btn-primary" name="up_img">POST</button> 
</form> 

2.upload.php

if(isset($_POST['up_img'])){ $errors= array(); 

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ 
     $file_name =$key.$_FILES['files']['name'][$key]; 
     $file_size =$_FILES['files']['size'][$key]; 
     $file_tmp =$_FILES['files']['tmp_name'][$key]; 
     $file_type=$_FILES['files']['type'][$key]; 

     if($file_size > 2097152){ 
      $errors[]='File size must be less than 2 MB'; 
     } 
     $pType='I'; 
     $query="insert into img(user,FILE_NAME)values('$sUser','$file_name') "; 
     $desired_dir="images_folder"; 
     if(empty($errors)==true){ 
      if(is_dir($desired_dir)==false){ 
       mkdir("$desired_dir", 0700);  // Create directory if it does not exist 
      } 
      if(is_dir("$desired_dir/".$file_name)==false){ 
       move_uploaded_file($file_tmp,"$desired_dir/".$file_name); 
      }else{         // rename the file if another one exist 
       $new_dir="$desired_dir/".$file_name.time(); 
       rename($file_tmp,$new_dir) ;    
      } 
     mysql_query($query);   
     }else{ 
       print_r($errors); 
     } 
    } 
    if(empty($error)){ 
    header('Location:preview.php'); 
    } 

    } 

3.preview.php

现在,我已经成功上传我的图像/影像服务器,并准备对其进行检索。记住我的图像文件现在存储在文件夹call images_folder中,它们的路径保存在img表中。

<?php 
$sql=mysql_query('SELECT * FROM img '); 
while($row = mysql_fetch_array($sql)) 
{ 
$sPic=$row['FILE_NAME']; 

echo '<img src="images_folder/'.$sPic.'">'; 

} 
?> 

就是这样。这就是你要如何上载图像到你的服务器,并检索它们。

0

所以这里有一个小小的选项来上传一张图片并将图片保存到mysql 这是没有测试只是键入。

if(isset($_FILES['picture']) //checks for form post if not show form 
    { 
     $title = $_POST['title']; //get title 
     $desc = $_POST['description']; //get description 
     $target = "downloads/pics/".basename($_FILES['uploaded']['name']); //this gets the target folder to save pictures make sure folder exists 
     $ok=1; //basic error set to 1 
     //This is our size condition 
     if ($uploaded_size > 20000000) 
     { 
      $out = "Your file is too large.<br>"; //checks for file size this can be changed to w/e 
      $ok=0; //if over error is set to 0 
     } 
     if ($ok==0) 
     { 
      $out.= "Sorry your file was not uploaded"; 
      return $out; 
     } 
     //If everything is ok we try to upload it 
     else 
     { 
      if(move_uploaded_file($_FILES['picture']['tmp_name'], $target)) //tires to move the uploaded picture to target 
      { 
       $out = "The Picture: <b><u>".basename($_FILES['picture']['name']). "</b></u> has been uploaded"; //if successfull then post was uploaded 
       $sql = "INSERT INTO database SET title=\"".$title."\",picture=\"".basename($_FILES['uploaded']['name'])."\",description=\"".$desc."\""; //sql string to insert the folder and picture name 
        if (!mysql_query($sql)) 
        { 
         die('Error In Picture Upload: - SQL INSERT INTO - '.date('m-d-y').' - SQL ERROR: '.mysql_error()); //if mysql error post error 
        } 
        else 
        { 
         $out.="<br /><br />Return To Your <a href=\"return location\">Downloads</a>"; //if mysql is ok show the return link 
         return $out; 
        } 
      } 
     } 
    else // if form is not submited then show form 
    { 
     $out ="\t\t<h3>".ucwords('new picture')."</h3>\r\n"; 
     $out.="\t\t<form name=\"post\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" enctype=\"multipart/form-data\">\r\n"; 
     $out.="\t\t\t<p>Title:<br /><input type=\"text\" class=\"inputtext\" name=\"title\" value=\"\" /></p>\r\n"; 
     $out.="\t\t\t<p><b>".ucwords('picture upload')."</b>: <input type=\"file\" name=\"picture\" /></p>\r\n"; 
     $out.="\t\t\t<p>Description:<br /><textarea id=\"text9\" name=\"description\"></textarea></p>\r\n"; 
     $out.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords('upload')."\" /></p>\r\n"; 
     $out.="\t\t</form>\r\n"; 
     echo $out; 
    }