doubu7425 2016-10-19 20:22
浏览 150
已采纳

找到在浏览器中出现“Not Found”错误的图像URL,但是它们实际存在

I have thousands of image urls stored in a table, one per row. The thing is that some of them have bad formatted names with spaces, accented characters, etc, ie like this:

https://www.greatsite.com/upload/memdocs/111046-carte d'identit� 001-072716141540.jpg

When opening this url in a browser, the following error is output:

Not Found
The requested URL /upload/memdocs/111046-carte d'identit� 001-072716141540.jpg was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I need to programatically find all the image urls that throw this "Not Found" error (in order to later correctly format the image url name).

So far I am trying to use getimagesize() and file_get_contents() but no luck. getimagesize() not always work because I think it kind of fixes the image name, because for example for the url above, it actually does returns and array with the image info. And file_get_contents() always returns something regardless of wether the image url throws the "Not Found" error or not.

Any suggestions on how I could accomplish this? I hope I made sense. Thanks

  • 写回答

2条回答 默认 最新

  • dt3999 2016-10-19 20:52
    关注

    You can get all the images from DB and iterate over them with foreach. In the foreach try checking if file exist. Example:

    foreach ($images as $image) {
        $valid = is_file($imageDir.$image->path);
    }
    

    is_file is optimal way to check if file exist. file_get_contents will read the whole file which is slow.

    Or you can just do regex on the image path:

    foreach ($images as $image) {
        $valid = preg_match('/[0-9a-zA-Z\$-_\.\+!\*'\(\),];\/\?\:\@=\&/', $image->path);
    }
    

    I`m not 100% sure if this regex would properly validate all the urls .... but most of them.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!