donglin6659 2010-10-23 11:36
浏览 153
已采纳

使用php:// input和file_put_contents

I'm receiving files (images) uploaded with Ajax into my PHP script and have got it to work using this:

$input = fopen("php://input", "r");
file_put_contents('image.jpg', $input);

Obviously I will sanitize input before this operation.

One thing I wanted to check was the file size prior to creating the new file, as follows:

$input = fopen("php://input", "r");
$temp = tmpfile();
$realsize = stream_copy_to_stream($input, $temp);
if ($realsize === $_SERVER["CONTENT_LENGTH"]) {
  file_put_contents('image.jpg', $temp);
}

And that doesn't work. The file is created, but it has a size of 0 bytes, so the content isn't being put into the file. I'm not awfully familiar with using streams, but I don't see why that shouldn't work, so I'm turning to you for help. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dops57958 2010-10-26 10:35
    关注

    The solution was deceptively simple:

    $input = fopen("php://input", "r");
    file_put_contents($path, $input);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部