dongxue9997 2019-07-06 16:34
浏览 93
已采纳

如何用瓷砖在数据库中显示图像

i am trying to make a page for me to upload files to with a tile i cant get it to show up on the website

if (isset($_POST["submit"]))
{

    $title = $_POST["title"];

 #file name with a random number so that similar dont get replaced
 $pname = rand(1000,10000)."-".$_FILES["file"]["name"];

#temporary file name to store file
$tname = $_FILES["file"]["tmp_name"];

 #upload directory path
$uploads_dir = 'images';
#TO move the uploaded file to specific location
move_uploaded_file($tname, $uploads_dir.'/'.$pname);

#sql query to insert into database
$sql = "INSERT into fileup(title,image) VALUES('$title','$pname')";

if(mysqli_query($conn,$sql)){

echo "File Sucessfully uploaded";
}
else{
    echo "Error";
}
}
  • 写回答

1条回答 默认 最新

  • duangao8359 2019-07-06 17:03
    关注

    In here I save the path of the image into the DB.Then retrieve the image source vai DB.

    for photo uploading .

    $folder ="../uploads/";
    $destFile = $folder . basename($_FILES["photo"]["name"]);
    $sourdeFile = $_FILES["photo"]["tmp_name"];
    
    
    if(move_uploaded_file($sourdeFile,$destFile)){
        echo "File has been uploaded";
        $photo = basename($_FILES["photo"]["name"]);
    }else{
        echo $_FILES['photo']['error'];
        $photo = "images/default.png";
    }
    $sql= "INSERT INTO car(photo) VALUES('$photo') ";
     if(mysqli_query($con,$sql)){
       echo
        "
        <script>
        alert ('Your Image successfully upladed');
        window.location='newindex.php';
        </script>
        ";
    
    }else{
    
        echo "Error:".mysqli_error($con);
    }
    

    To view uploaded images

      $sql=" SELECT *  FROM car";
      $result=mysqli_query($con,$sql);
      $row=mysqli_fetch_array($result);
    
    
       <img src="Your default path name/<?=$row["photo"];?>" class="img-thumbnail" style="max-width:400px;max-height:350px;"/>
    

    In here photo will be uploaded into a folder named uploads. At the begoining of the upload name of the image will be save in to DB table . Then to view uploaded images img src will use the default path name with photo name at the DB. hope you can grab something from this. Also please note that this is only a example. When using SQL please use parameterized query to avoid SQL injections. Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?