当我使用file_get_contents()函数时等效$ _FILES [" imageToUpload"] [" tmp_name"] [英] equivalent $_FILES["imageToUpload"]["tmp_name"] when I use file_get_contents() function

查看:190
本文介绍了当我使用file_get_contents()函数时等效$ _FILES [" imageToUpload"] [" tmp_name"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的函数:

fitImageSizeAndSave($_FILES["imageToUpload"]["tmp_name"], $target_file) { ... }

该功能运作良好。第一个参数是 $ _ FILES [imageToUpload] [tmp_name] 。好的,用户从本地计算机上传图像时效果很好。但有时他会输入一个外部链接,我会得到这样的图像:

That function works well. The first argument is $_FILES["imageToUpload"]["tmp_name"]. Ok, it's good when an user upload an image from his local computer. But sometimes he enters a external link and I get that image like this:

$image = file_get_contents($_POST['external_link']);

如何使 $ image $ _ FILES [imageToUpload] [tmp_name] 将其传递给函数?

How can I make $image like $_FILES["imageToUpload"]["tmp_name"] for passing it to the function?

推荐答案


使用 file_put_contents()然后引用刚刚将数据放入的临时文件

use file_put_contents() and then reference the temporary file that you've just put the data into








@Martin听起来不错,请你补充一下答案吗? -

@Martin Sounds a great idea, may you please add an answer? –






$ _ FILES [imageToUpload] [ tmp_name] 只是服务器上文件位置的字符串,它本身不是resuorce。


$_FILES["imageToUpload"]["tmp_name"] is simply a string to a file location on the server, it is not a resuorce in itself.

$filePathLocation = $_SERVER['DOCUMENT_ROOT']."/some-temporary/file/storage";
$imageData = file_get_contents($_POST['external_link']);

if($imageData){
     file_put_contents($imageData, $filepathLocation);
}
else {
     $filepathLocation = $_FILES["imageToUpload"]["tmp_name"];
}

fitImageSizeAndSave($filepathLocation, $target_file) { ... }



以上,一步一步:




  • 设置临时存储位置;可能基于 microtime() 或其他东西(数据库ID,如果相关),以限制不同的进程覆盖相同的文件路径。

    The above, step by step:

    • Set a temporary storage location; possibly based on microtime() or something unique (database Id, if relevant) to limit different processes overwriting the same file path.

      获取 $ _ POST 文件URL的内容。保存为字符串变量。

      get the contents of the $_POST file URL. save to a string variable.

      检查如果变量加载好了; else 使用上传的临时文件位置 这仅用于说明 - 您应该有一个更完整的流程来检查哪些数据使用和已在脚本中设置的所述数据的有效性。

      Check if variable is loaded ok; else use uploaded temporary file location THIS IS FOR ILLUSTRATION ONLY - You should have a more complete process for checking which data to use and the validity of said data already set up in your script.

      将此变量发送到您的自定义函数,已知其填充了上述一种或另一种可能性。

      Send this variable to your custom function, knowning it is populated with one or the other of the possibilities above.

      这篇关于当我使用file_get_contents()函数时等效$ _FILES [" imageToUpload"] [" tmp_name"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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