简体   繁体   English

你如何处理PHP中的临时图片上传?

[英]How do you deal with temporary picture uploads in PHP?

I'm creating an online game in PHP where users can create playable characters. 我正在用PHP创建一个在线游戏,用户可以在其中创建可玩的角色。 Each character can have a user-uploaded portrait. 每个角色都可以拥有用户上传的肖像。 A player can simultaneously have multiple characters, and the pictures for them can be changed anytime. 玩家可以同时拥有多个角色,并且可以随时更改他们的图片。 Naturally, the pictures have to be resized and re-compressed to avoid huge files. 当然,图片必须调整大小并重新压缩以避免大文件。 Here's my problem: 这是我的问题:

When the player changes his data (among it the picture), and then hits "save", server side validation kicks in. It checks for things like non-unique character names, empty mandatory fields, etc. If any errors are found, they are displayed. 当玩家更改其数据(其中包含图片),然后点击“保存”时,服务器端验证就会启动。它会检查非唯一字符名称,空必填字段等内容。如果发现任何错误,显示。 In this case the form should be pre-populated with the data the player entered, so he only has to change the faulty bit, not re-type everything. 在这种情况下,表单应该预先填充玩家输入的数据,因此他只需要更改故障位,而不是重新输入所有内容。 But how do you save the picture in such a "temporary" state? 但是如何将图片保存在这种“临时”状态?

I cannot pre-populate the file upload field, the browsers don't allow that. 我无法预先填充文件上传字段,浏览器不允许这样做。 If I save it in a temporary file, the picture then has to be cleaned up at some point, because the player can simply close his browser and abort the whole process. 如果我将它保存在一个临时文件中,那么必须在某些时候清理图片,因为玩家可以简单地关闭他的浏览器并中止整个过程。 When should that be? 什么时候应该? And what file name should I choose for the temporary file? 我应该为临时文件选择什么文件名? If the player opens the same character to edit in two browser tabs, they should not conflict (each of them should have their own copy). 如果播放器在两个浏览器选项卡中打开相同的字符进行编辑,则它们不应该冲突(每个都应该有自己的副本)。

How would you solve this problem? 你会如何解决这个问题?

I'd save the file to a temporary location and store both a unique identifier as well as the current timestamp in the filename. 我将文件保存到临时位置,并在文件名中存储唯一标识符和当前时间戳。 Then put the filename in the user's session. 然后将文件名放在用户的会话中。 When a user has successfully created or updated their account, you save the image file to its permanent location and remove the temporary file. 当用户成功创建或更新其帐户时,您将图像文件保存到其永久位置并删除临时文件。 You can run a cron process to scan the temporary directory and check the timestamps, deleting any files older than your expiration (an hour perhaps). 您可以运行cron进程来扫描临时目录并检查时间戳,删除任何早于过期的文件(可能是一小时)。

If you're unable to run a cron job, you could always just launch the directory clean-up each time you have a successful create/update validation. 如果您无法运行cron作业,则每次成功创建/更新验证时都可以始终启动目录清理。 This might be a bit more inefficient (extra directory reads and possibly file operations for every successful submission) but unless you're dealing with a lot of traffic, you probably won't even notice. 这可能会更低效(额外的目录读取以及每次成功提交时可能的文件操作)但除非您处理大量流量,否则您可能甚至都不会注意到。

Create a table to hold references to images. 创建一个表来保存对图像的引用。

When a file is uploaded, if it's a valid image, do all your resizing, etc, and create a record in the table that points at the file. 上传文件时,如果它是有效图像,请执行所有调整大小等操作,并在表中创建指向该文件的记录。

Pass the id of the reference record around with the form data. 使用表单数据传递引用记录的id。 Display the image when you redisplay the form, so the user knows they don't have to re-upload. 重新显示表单时显示图像,因此用户知道他们不必重新上载。

When you finally accept the new character object, set avatar_id or whatever. 当您最终接受新角色对象时,请设置avatar_id或其他。

Run a regular cron-job to cull orphaned image records (deleting the files on disk as well). 运行常规cron-job来剔除孤立的映像记录(也删除磁盘上的文件)。

You could always populate a disabled text box to hold the name of the picture - it won't populate the browse input field, but is better than nothing. 您可以随时填充禁用的文本框以保存图片的名称 - 它不会填充浏览输入字段,但总比没有好。 For editing, to avoid conflicts you could create some a "modifing" column for each user's characters, and on a character editing request change the value to true. 对于编辑,为避免冲突,您可以为每个用户的字符创建一些“修改”列,并在字符编辑请求中将值更改为true。 When the user saves the character, set it back to false. 当用户保存角色时,将其设置为false。 For each edit request, grant it only when the "modifing" is false. 对于每个编辑请求,仅在“修改”为假时才授予它。

I'd recommend either updating the image immediately, regardless of error in the form, or separating the image updating to a separate form. 我建议立即更新图像,无论表单中的错误如何,或者将图像更新分离为单独的表单。 That way you'll get rid of two problems without complex state machines and cleaning up. 这样,没有复杂的状态机和清理,你就可以解决两个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM