PHP - 在会话数据中存储图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/754145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Store Images in SESSION data?
提问by Darryl E. Clarke
Can you store an image in a PHP SESSION ?
您可以在 PHP SESSION 中存储图像吗?
I have a multi-step registration process using PHP on my site. On one of the steps, the users can upload their company logo (image).
我在我的网站上有一个使用 PHP 的多步骤注册过程。在其中一个步骤中,用户可以上传他们的公司徽标(图片)。
The last step is to process their credit card.
最后一步是处理他们的信用卡。
So before I write any images to the web server and image location to the database, I want to make sure their credit card is valid and process.
因此,在我将任何图像写入 Web 服务器并将图像位置写入数据库之前,我想确保他们的信用卡有效且经过处理。
As such, is it possible to temporarily store that image data in the SESSION variable?
因此,是否可以将该图像数据临时存储在 SESSION 变量中?
If not, how else do people temporaily store image data on forms before committing that data?
如果没有,人们如何在提交数据之前将图像数据临时存储在表单上?
回答by Darryl E. Clarke
You can but expect memory usage of your session to increase depending on the size of the images. In order to do so, you must save the file contents into a session variable.
您可以但期望会话的内存使用量会根据图像的大小而增加。为此,您必须将文件内容保存到会话变量中。
If it is in session data and you have multiple steps after the upload the image will be reloaded (into the session) every page view until the steps are complete.
如果它在会话数据中并且您在上传后有多个步骤,则每次页面视图都会重新加载图像(进入会话),直到这些步骤完成。
I would personally recommend against using the session for holding a binary file. Saving the image on disk into a temporary location until the registration is complete. I would only save the path to the temporary file in session. When the transaciton is completed move it to a proper location and do your db inserts.
我个人建议不要使用会话来保存二进制文件。将磁盘上的图像保存到临时位置,直到注册完成。我只会在会话中保存临时文件的路径。当事务完成后,将其移动到适当的位置并进行数据库插入。
Also, in essence, session data is stored on disk (or db) anyway so you might as well save the image file once then issue a move command once complete.
此外,本质上,会话数据无论如何都存储在磁盘(或数据库)上,因此您最好保存一次图像文件,然后在完成后发出移动命令。
回答by Robin Barnes
I'd save the file to disk, you could even name it using the user's session id. Then there could be some sort of clean up script which is run as a cron job and deletes the images of people who never successfully paid.
我会将文件保存到磁盘,您甚至可以使用用户的会话 ID 为其命名。然后可能会有某种清理脚本作为 cron 作业运行并删除从未成功付款的人的图像。
If you try and store an image in the session, you're doing it wrong.
如果您尝试在会话中存储图像,那么您就做错了。
回答by davethegr8
When a file is uploaded, it gets assigned a temporary name in the $_FILES array. I don't know the exact lifespan of those files, but you might be able to capture that name in the session and put off the move_uploaded_file() call until after the CC is verified.
上传文件时,它会在 $_FILES 数组中分配一个临时名称。我不知道这些文件的确切生命周期,但您可能能够在会话中捕获该名称并推迟 move_uploaded_file() 调用,直到验证 CC 之后。
Or, you could do the CC step first.
或者,您可以先执行 CC 步骤。
回答by Amirul Momenin
Sometimes We need to preview/confirm page before save data into database.
But Image file to the confirm page is a little bit differnt.You cant do
$_SESSION['s_name'] = $_FILES['f_name']coz SESSION just keep the text file.
In the alternative way keeping file contents/binary value in session.
有时我们需要在将数据保存到数据库之前预览/确认页面。但是确认页面的图像文件有点不同。你不能做
$_SESSION['s_name'] = $_FILES['f_name']因为 SESSION 只保留文本文件。以另一种方式在会话中保留文件内容/二进制值。
$_SESSION['obj_image_session'] = file_get_contents($_FILES['image_name']['tmp_name']);
$file= "new_file.jpg";
$fp=($file,"w");
fwrite($fp,$_SESSION['obj_image_session']);
回答by Kishor Kundan
If you have to keep the data, I would suggest keeping it as base64_encoded string. You can directly send base64_encode image data to the browser.
如果您必须保留数据,我建议将其保留为 base64_encoded 字符串。可以直接向浏览器发送base64_encode图像数据。
If I'd be in similar situation i would have rather saved the image and kept the information about the image in session/db. If for some reason the registration fails, i would unlink the file later. and occasionally run cron jobs to locate missing links with the images.
如果我处于类似的情况,我宁愿保存图像并将有关图像的信息保存在会话/数据库中。如果由于某种原因注册失败,我稍后会取消链接文件。并偶尔运行 cron 作业来定位与图像的缺失链接。
But i will really suggest you to stick to the second option and avoid the hassle altogether.
但我真的建议你坚持第二种选择,完全避免麻烦。
回答by vimal krishna
You can store image data in session as base encoded one easily. In production server you are supposed to have enough RAM. My application needed upto 40MB of 4 images/apps for update and change before putting in mongoDB. (Base encoding makes 1.3 times larger image size.)
您可以轻松地将会话中的图像数据存储为基本编码的图像数据。在生产服务器中,您应该有足够的 RAM。在放入 mongoDB 之前,我的应用程序需要多达 40MB 的 4 个图像/应用程序进行更新和更改。(基本编码使图像大小增加 1.3 倍。)
$tmpNameSS1 = $_FILES["screenshot1"]["tmp_name"];
$fp = fopen($tmpNameSS1, 'r');
$rawDataSS1 = fread($fp, filesize($tmpNameSS1));
fclose($fp);
$SS1FileName = $_FILES["screenshot1"]["name"];
$encodedSS1Data = base64_encode($rawDataSS1);
registry::update('sCreateSS1Name', $SS1FileName);
registry::update('sCreateSS1Data', $encodedSS1Data);
A case will be: you have multiple image to uploaded and both client and server validation for size and type. It is faster to fetch from session. After putting in DB null the variable holding the image.
一种情况是:您有多个图像要上传,并且客户端和服务器都验证了大小和类型。从会话中获取更快。将 DB null 放入保存图像的变量后。
Browser will show show that image with:
浏览器将显示该图像:
<img src="data:image/;base64,<?php echo registry::get('sCreateSS1Data'); ?>"/>
You can update the session for the image with empty string after the code reaches the end of the block. Typical case is updating form field with validation and when the user wants to change the text. You also want to show what image was uploaded between those failed updates. If you want to save the round trip (advisable) keep data for some moments in the session and empty that value after code is about exit.
在代码到达块的末尾后,您可以使用空字符串更新图像的会话。典型的情况是使用验证更新表单字段以及当用户想要更改文本时。您还想显示在这些失败的更新之间上传了什么图像。如果您想保存往返行程(建议),请在会话中保留一段时间的数据,并在代码即将退出后清空该值。
回答by staticsan
Yes, you can store an image in a PHP session. Get it into PHP as a string (i.e. binary data) and then you can put it in the session.
是的,您可以在 PHP 会话中存储图像。将其作为字符串(即二进制数据)放入 PHP 中,然后您可以将其放入会话中。
You will want it to only be as big as it needs to be and you need to delete it as soon as you don't need it because large pieces of information in the session will slow down the session startup.
您会希望它只需要它的大小,并且您需要在不需要它时立即将其删除,因为会话中的大量信息会减慢会话启动速度。

