donglin6659 2010-10-23 19: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 18:35
    关注

    The solution was deceptively simple:

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

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word