dealing with concurrent & aborted uploads, codeigniter

心已入冬 提交于 2021-01-29 07:46:20

问题


Does anyone know the best way to deal with concurrent uploads and what todo if a user only partially uploads a file and then quits for some reason? (or their internet dies).

Here's my controller: http://pastebin.com/wqnFAge6

And the model: http://pastebin.com/0S5ai2re

the upload in the controller

           if($this->input->post('upload')) {               
                    $uploaded_image_ids = $this->Site_model->do_upload();
                    //$uploaded_image_id = $this->Site_model->get_last();

                    $values = array(
                    'image_id' => implode(",",$uploaded_image_ids),
                    'session_id' => $this->session->set_userdata('session_id')
                    );
                    $this->session->set_userdata('edit', $values);

                    //show uploaded image
                    redirect(implode(",",$uploaded_image_ids) . '?links');

            }

All is working well now except if i attempt to upload multiple files at once it'll skip generating a thumbnail for one sometimes, also if an upload is cut off it leaves the uploaded file in /images/<- instead of removing it like it should after an upload and processing is done.

Any idea how I can fix these issues? Many thanks.


回答1:


If your client should lose his connection while uploading files via POST the traditional way (without AJAX1) hence breaking the upload, your controller will never be executed since the request body has no end marker.

As for concurrent uploads; there is no such thing (except maybe with AJAX1). When a user uploads several files in one form, these are processed serially. Here's a little guide I found by googling to handle multiple uploads.

1Since there are various different implementations of asynchronous file uploads and I'm not familiar with all of them, this answer may be invalid for at least one of them.


Edit:

In PHP your application is launched per-request so there is no concurrency at that level; no two request will be served from the same instance of your application. However, assuming that you are working with a single DB, two instances of your app may access the same resource concurrently and it's your DB engines's responsibility to handle them. You can still screw up on the application level by not using transactions where you should for instance, but as long as you CRUD your DB correctly, your application will not have to handle concurrency explicitly.

As for your code, I don't have the time to learn it thoroughly as to give you a definitive yes/no answer, but if you are sure that per each uploaded image there's an entry in your DB and when updating an image the corresponding data will as well be updated in the DB, then you will have no trouble handling several such requests simultaneously.



来源:https://stackoverflow.com/questions/8984581/dealing-with-concurrent-aborted-uploads-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!