drzfnr0275 2015-03-20 01:49
浏览 42
已采纳

PHP file_put_contents()

How can I get this to provide an actual image?

file_put_contents($url,$image);

The size, and name are posted to directory correctly, but the image appears broken. Do I need to encode, or decode?

Just to inform you, $image comes from the following:

    ob_start();
    echo imagejpeg($image,null,30);
    $image = ob_get_clean();
    ob_end_clean();
    $image = addslashes($image);

$image has successfully been posted to mysql as is, but I have been struggling to get it into directory.

  • 写回答

1条回答 默认 最新

  • dsa5211314 2015-03-20 01:59
    关注

    imagejpeg() outputs the image to the output buffer. You're using echo imagepeg() which will output the image then immediately echo the true/false return value of your function call.

    Further, you're corrupting your image by doing this: $image = addslashes($image);, which I imagine is done for the benefit of your database.

    What you need is

    ob_start();
    imagejpeg($image,null,30);
    $image = ob_get_clean();
    file_put_contents($url,$image);
    

    I don't see why you're storing the image in the database and in the file system. The filesystem should be good enough: store the path & file name in the database.

    If you feel you must add the image to the database don't use addslashes() but escape it with the function provided by the API you're using, at the point you want to execute the query. You'd be better using prepared statements so that escaping the data is unnecessary.

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

报告相同问题?

悬赏问题

  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题