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.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)