3 points · by amrithk · 18y ago · 14 comments
I have a form which allows people to upload images. When a user uploads an image, I save to a database, 1) a modified name for the image, 2) a caption for the image, 3) the id of the user who uploaded the image, 4) Some other information that are used for image retrieval purposes
The form allows users to upload up to 30 images. I do not want to write to the database each time the user wants to upload an image. Rather, I want to store each upload request (plus all the associated information I need like captions etc) and make a large write when a user has confirmed that he/she is satisfied with all of their choices.
How can I do this easily? 1) I have considered storing the attributes and information associated with each image in cookies and retrieving and processing cookie information after the user has confirmed his/her choices. However, I am not comfortable with this option as I cannot control what happens to the cookies when they are stored in the client machine.
2) Another option is to obtain many hidden fields (more than 30) in the HTML form and fill these up with the associated information I need. I could then process the information in these hidden fields before writing to the database. However, this seems pretty clumsy.
3) A third option I was considering was using Amazon's Simple Queue Service to queue image upload requests and then unqueue and process the messages after a user finished making his/her choices and hit a confirm button.
Which solution among these 3 is the cleanest and most flexible implementation? Are there other solutions for this problem?
Thanks all.
If your application stack does not have a session implementation that 'Just Works!'(tm) for you, you will have to implement one yourself.
Generally it will look something like a cross between your solutions 1 and 3, you create an opaque session key which is stored as a cookie and is used to retrieve the data from the session store. What you use to store session data is going to vary depending on how valuable the data and on how tragic failure is.
Avoid hidden fields in the html, they are a pain to maintain and will cause you problems because the temptation is to trust that their contents are what you put into them... it also means that you are repeatedly processing the same data. In sum; hidden form values make baby jesus cry.
We use PHP as our server side scripting language. I was thinking of using the $_SESSION variable but attributes and pointer information for 30 image uploads per user * no of concurrent users seems like a huge drag on server memory.
I'll probably just write the pointer information to the database for now after each upload. It might make the web app slower by a few milliseconds but it reduces unnecessary complications
I have a form that: user can upload as many as s/he wants user clicks submit the browser then sends a stream of data one time
on the server: regex find .multipart/form-data;.boundary=.* from CONTENT_TYPE to make sure it's the right stream save the stream into temp-file, read the temp-file
split file-stream based on .filename=\"(.)\".* into chunks afterward, "calibrate" the chunk (determine start & end write positions) then loop and write the chunks into specific filenames provided in the stream
so there are 1 upload stream and 1 load n+1 write operations
I wonder if it helps since I don't use php (hint: newlisp)
That would save some processing time right? Thanks.
If premature optimization is the root of all evil, I'm afraid you are halfway to hell.
This is not a good reason to make your app more complicated than it needs to be. For one thing, it isn't even easy to tell if your statement is true, let alone how true it is. (i.e. it may be true that you save a few milliseconds, but is that even going to be significant?)
If there is a UI reason why you have to have the user specify 25 uploads on one HTML page and then do them all at once, that's one thing. (I'd use the big-pile-of-hidden-HTML-fields approach; that has the fewest moving parts, it's simple to conceptualize, and I don't think the other methods will make a big difference from the user's perspective.) But don't try to work around your DB until you absolutely have to. Use it. That's what it's there for.
It's a serious question. Have you benchmarked this? Writing BLOB data to any reasonable database (or storing it as BFILE if you must) is imperceptibly slower than a filesystem write. In the case of MySQL the database is just syntactic sugar sprinkled on the filesystem. In the case of Oracle on raw volumes it's going to be faster to write to than a cooked filesystem. If you're worried about huge transactions, just COMMIT each one and add a column for approved_yn that you update in a single transaction when the user is happy.