php – getimagesize()on stream而不是string
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – getimagesize()on stream而不是string,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2543字,纯文字阅读大概需要4分钟。
内容图文
![php – getimagesize()on stream而不是string](/upload/InfoBanner/zyjiaocheng/802/64d668dcecd54d7ca5c60750b408fc3e.jpg)
我正在使用Valum’s file uploader使用AJAX上传图像.这个脚本以我不完全理解的方式将文件提交给我的服务器,因此最好通过显示我的服务器端代码来解释:
$pathToFile = $path . $filename;
//Here I get a file not found error, because the file is not yet at this address
getimagesize($pathToFile);
$input = fopen('php://input', 'r');
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
//Here I get a string expected, resource given error
getimagesize($input);
fclose($input);
$target = fopen($pathToFile, 'w');
fseek($temp, 0, SEEK_SET);
//Here I get a file not found error, because the image is not at the $target yet
getimagesize($pathToFile);
stream_copy_to_stream($temp, $target);
fclose($target);
//Here it works, because the image is at the desired location so I'm able to access it with $pathToFile. However, the (potentially) malicious file is already in my server.
getimagesize($pathToFile);
问题是我想在这里使用getimagesize()执行一些文件验证. getimagesize只支持一个字符串,我只有资源可用,这会导致错误:getimagesize需要一个字符串,给定资源.
当我在脚本结束时执行getimagesize($pathTofile)时它确实有效,但是图像已经上传并且损坏已经完成了.执行此操作并在之后执行检查然后可能删除te文件对我来说似乎是不好的做法.
$_REQUEST中唯一的东西是文件名,我用它来var $pathToFile. $_FILES是空的.
如何在流上执行文件验证?
编辑:
解决方案是首先将文件放在临时目录中,然后在临时文件上执行验证,然后再将其复制到目标目录.
// Store the file in tmp dir, to validate it before storing it in destination dir
$input = fopen('php://input', 'r');
$tmpPath = tempnam(sys_get_temp_dir(), 'upl'); // upl is 3-letter prefix for upload
$tmpStream = fopen($tmpPath, 'w'); // For writing it to tmp dir
stream_copy_to_stream($input, $tmpStream);
fclose($input);
fclose($tmpStream);
// Store the file in destination dir, after validation
$pathToFile = $path . $filename;
$destination = fopen($pathToFile, 'w');
$tmpStream = fopen($tmpPath, 'r'); // For reading it from tmp dir
stream_copy_to_stream($tmpStream, $destination);
fclose($destination);
fclose($tmpStream);
解决方法:
您可以使用tempnam()和sys_get_temp_dir()来创建临时路径,而不是使用tmpfile().
然后使用fopen()来获取它的句柄,复制流.
然后你有一个字符串和一个句柄来处理你需要做的操作.
//Copy PHP's input stream data into a temporary file
$inputStream = fopen('php://input', 'r');
$tempDir = sys_get_temp_dir();
$tempExtension = '.upload';
$tempFile = tempnam($tempDir, $tempExtension);
$tempStream = fopen($tempFile, "w");
$realSize = stream_copy_to_stream($inputStream, $tempStream);
fclose($tempStream);
getimagesize($tempFile);
内容总结
以上是互联网集市为您收集整理的php – getimagesize()on stream而不是string全部内容,希望文章能够帮你解决php – getimagesize()on stream而不是string所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。