使用PHP://输入和file_put_contents [英] Using php://input and file_put_contents

查看:223
本文介绍了使用PHP://输入和file_put_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到文件(影像)与阿贾克斯上传到我的PHP脚本,并已完成了使用这个工作:

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);
}

这是行不通的。该文件被创建,但它具有大小为0字节,所以内容不被放入该文件。我不是非常熟悉使用流,但我不明白为什么不应该工作,所以我转向你的帮助。在此先感谢!

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!

推荐答案

解决方案是看似简单:

$input = fopen("php://input", "r");
file_put_contents($path, $input);

这篇关于使用PHP://输入和file_put_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆