Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php://input 作为资源和字符串_Php_Inputstream - Fatal编程技术网

php://input 作为资源和字符串

php://input 作为资源和字符串,php,inputstream,Php,Inputstream,代码如下: $file = fopen('php://input', 'r'); $temp = tmpfile(); $imageSize = stream_copy_to_stream($file, $temp); $imageDimensions = getimagesize($file); // Error here $imageInfos = pathinfo($_GET['selected-image']); 我得到这个错误 getimagesize()要求参数1为字符串,资源给定

代码如下:

$file = fopen('php://input', 'r');
$temp = tmpfile();
$imageSize = stream_copy_to_stream($file, $temp);
$imageDimensions = getimagesize($file); // Error here
$imageInfos = pathinfo($_GET['selected-image']);
我得到这个错误
getimagesize()要求参数1为字符串,资源给定

因为
$file
fopen
的资源。如果我不能阅读
php://input
两次


更新

我试过这个:

$file = fopen('php://input', 'r');
$tempName = tempnam(sys_get_temp_dir(), '.upload');
$imageSize = fwrite(fopen($tempName, 'w+'), stream_get_contents($file));
$imageDimensions = getimagesize($tempName);
$imageInfos = pathinfo($_GET['selected-image']);

// Unlinks and other stuffs
错误:

Notice: getimagesize() [function.getimagesize]: Read error! in 
谢谢

因为只接受字符串文件名,而不接受资源,所以您需要使用创建一个临时文件,该文件的名称可以传递给

这假设运行PHP的用户对目录
/var/tmp
具有写入和读取权限,但如果没有,您可以更改到用户确实具有r/w权限的目录。

用于生成临时文件,而不是
tmpfile()
。然后,您可以将一个文件路径传递到
getimagesize()


为什么
tempnam()
$img
作为第二个参数?@GabrielSantos抱歉,这应该是字符串
'img'
,请参阅更新answer@GabrielSantos当您得到错误
字符串'D:\Webserver\htdocs\santosidaniel\qualasuaponte\application\img9AD3.tmp'(长度=71)时,请显示
var\u dump($tempName)
的输出。但是同样的错误。
var_dump(file_exists($tempName));变量转储(可读($tempName))
$file = fopen('php://input', 'r');
$tempName = tempnam('/var/tmp', 'img_');
$temp = fopen($tempName, 'w');
$imageSize = stream_copy_to_stream($file, $temp);
fclose($temp);
$imageDimensions = getimagesize($tempName);
$file = fopen('php://input', 'r');
$tempname = tempnam('.', 'img');
$tempfile = fopen($tempname, 'w+');
$imageSize = stream_copy_to_stream($file, $tempfile);
$imageDimensions = getimagesize($tempname);
$imageInfos = pathinfo($_GET['selected-image']);

// At the end of the script, you need to remember to...
fclose($tempfile);
unlink($tempname);