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

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名