dsdeeaquu38538545 2014-04-02 21:07 采纳率: 100%
浏览 36
已采纳

插入MySQL上传查询php

I am trying to make a script to save image link in database like example.com/image.jpg every thing is working fine I just want that if someone enter wrong url like without extension .jpg , .png or .gif it give error to user so they enter correct url

Here is my Php Code

if(isset($_POST['img']));
  $img = $_POST['img'];
  $ti = $_POST['titl'];

  $query = mysqli_query($con,"SELECT * FROM image WHERE url='$img'")

   or die(mysqli_error($con));  
      if(mysqli_num_rows($query) > 0 ) { //check if there is already an entry for that word
        echo "Image already exists! ";
      }
      else {
        mysqli_query($con,"INSERT INTO image (Title, url,) VALUES ('$ti','$img')");
    echo "Image Successfully Added";
 }

And here is html

<form  action="" method="POST" >
  <span>Title:</span>
  <input  type="text" name="titl" maxlength="100" /><br>
  <span>Image URL:</span> 
  <input  type="text" name="img" maxlength="300" />
  <input type="submit" value="Add Image!" id="subm" class="button" />
</form>
  • 写回答

2条回答 默认 最新

  • dragon19720808 2014-04-02 21:15
    关注
    $accept = ['.jpg', '.gif', '.png']; // the file extensions you will accept
    
    if (in_array(substr($img, -4), $accept)) // check last 4 chars in $accept
    {
        echo "This works";
        // update mysql table
    } // if
    else
    {
        echo "This fails";
    } // else
    

    This is just a way to check the file name, not the content of the file. As mentioned by @Fred-ii : The image must still be sanitized for malicious code. One preliminary measure would be to rename the file to something random, and store the random ID in the database. You might also use GD or ImageMagick to copy the file across, 1:1, which would compress the file and remove any malicious code. You can also serve the image from another domain if you have one, to protect your main site and its data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题