duande3134 2009-03-01 21:51
浏览 210
已采纳

file_get_contents创建一个空文件

how can I prevent file_get_contents from creating an empty file when being used as a test condition in an if clause?

An empty file is created regardless, which causes a subsequent call in a different method to getimagesize() to fail.

The problem is, that as I have my code setup, the first time it is called will determine to save an image or to display a previously saved imaged. This is in part dependent on the presence of a file. As an empty file is created, this causes problems when calling my code subsequent times.

Is the easiest way to add a check if the file exists and is greater than 0?

Regardless of if my code works, file_get_contents will still output an error. This error is accounted for and dealt with(by my if condition), so I would like to avoid the error interrupting the output of my application if possible. Is there a way to turn this off without hiding actual errors?

if (file_put_contents($imageDir . $pk . '.jpg', file_get_contents($pic_url))) 
{
        return $imageDir . $pk . '.jpg'; 
} 
else 
{
        return 'removed.jpg';
}
  • 写回答

2条回答 默认 最新

  • dongshuo1856 2009-03-02 01:24
    关注

    It isn't file_get_contents() that is creating an empty file, it is file_put_contents().

    file_put_contents() will create a file even if the second parameter is empty. Hence, empty file.

    You'll need to check the file exists first.

    The easiest fix would be to move file_put_contents() inside the conditional, so that it only creates a file if there are contents.

    if (($filecontents = file_get_contents($pic_url)) !== false) 
    {
        file_put_contents($imageDir . $pk . '.jpg', $filecontents);
        return $imageDir . $pk . '.jpg'; 
    } 
    else 
    {
        return 'removed.jpg';
    }
    

    Now, this still leaves you with a bunch of problems.

    • Unless you are validating the $pic_url properly, you are leaving yourself open for security vulnerabilities. What if the user inputs a relative path to a local file?
    • file_get_contents() will throw a warning if the file cannot be found. Normally, you would deal with this by checking file_exists() first, but this is NOT possible here, because the http: wrapper does not support file_exists(). Therefore you can supress the error with @ before file_get_contents(). Suppressing errors like this should be avoided in most cases.
    • Even if you suppress the error with '@', the call to file_get_contents() may still take some time - if the address is wrong, it may result in getting no reply from a server, which will cause it to hit a timeout (probably 30 seconds) during which time your script does not run and therefore an end user can get no feedback. This should be taken into account in your app.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测