PHP File Upload backend processing tips,
PHP File Upload backend Processing
Quote: As mentioned in the previous article, you can use hidden methods to make your upload page look beautiful. However, there is no such thing as performance. Isn't there any processing skills in background processing? The so-called background skills should include faster uploading and larger uploading files! In this article, we will talk about some tips for backend processing!
Business scenario 1, We only select a single file to upload, and do not need to do some real-time verification work. So there may be no optimizations, because all you need to do is put the file in the form and submit it together for direct processing!
Business scenario 2, You need to upload multiple files, and verify the internal content of the file from time to time, and the corresponding page is displayed. In this case, we need to upload the file immediately after the user selects to upload the file, because we need to read the information in the file and submit the file at the end. Obviously, there is a repetitive upload task, which consumes user time and server bandwidth resources! Optimization: the desired method is also very simple. Can you keep the file on the server after the first file is uploaded and submit the form, read the uploaded temporary file. Yes, this is our solution!
Business scenario 3. Similar to scenario 2, multiple files need to be uploaded, but multiple files may be uploaded separately. That is to say, we may have uploaded 10 M for the first time, 10 M for the second time, and 10 M for the total. So, on the server side, one-time submission must have exceeded the upload size limit. However, if we upload each time, this is acceptable. At the end of the submission, we only need to pass the short text information!
The idea is indeed simple. It seems that there is no problem, but maybe I have limited capabilities. At that time, it took me a lot of time to deal with this ghost! Below, I will provide some sample code for your reference:
Example code:
1. Page js Processing
// Click "complete file" to trigger file upload and upload the file to the temporary directory on the server $ ('. upload-real-file '). off (). on ('change', function () {if (! $ (This ). val () {return false;} var responseObjId = $ (this ). attr ('response-id'); var responseObj = $ ('#' + responseObjId); $ ('# form '). ajaxSubmit ({url: '/aa/bb/uploadtmpapktool', resetForm: false, ype: 'json', beforeSubmit: function (option) {window. loading = layer. load (2) ;}, success: function (data, statusText) {layer. close (window. loading); if (data. status = 1) {responseObj.html(data.apk InfoHtm L); var parentContainer = responseObj. parent (). parent (), nameContainer = parentContainer. find ('. file-name-iner '); nameContainer.html(data.apk Name); nameContainer. attr ('title', data.apk Name); responseobj.find('.file-tmp'pai.html (data. fileInfo); // store the file information in a hidden domain so that you can locate $ (submitId) when submitting the file ). removeAttr ('Disabled ');} else {layer. alert (data.info) ;}}, error: function (data) {layer. close (window. loading); laye R. alert ('unknown error. Please try again later! ') ;}}); Return false; // prevents automatic close of dialog });
2. Obviously, you need to obtain the file information on the page and process the code in the background (PHP)
$ ApkConfig = $ this-> _ getApkConfig (); $ params = $ this-> getFilteredParam ('get'); $ subFile = $ _ FILES ['apktoolfile']; $ apkName = $ apkInfoHtml = ""; if (empty ($ subFile) {$ this-> ajaxReturn (array ('status' =>-4, 'info' => 'select the file to be uploaded ');} foreach ($ subFile ['name'] as $ subKey => $ subVal) {if ($ subFile ['name'] [$ subKey]) {$ fileData = $ this-> _ getFileData ($ subFile, $ subKey ); $ checkData = array ('maxsize' => $ ApkConfig ['file _ MAX_SIZE '], 'savepath' => $ apkConfig ['tmp _ CHILD_PATH'], 'extarr' => array ('apk '), 'releasename' => str_replace('.apk ', '', $ fileData ['filename']), // special); $ checkResult = $ this-> _ checkFileData ($ fileData, $ checkData); if ($ checkResult ['status']! = 1) {$ this-> ajaxReturn ($ checkResult);} // move the file $ filePath = $ checkData ['savepath']. '/'. $ fileData ['filename']. '. tmp '. genRandStr (6); $ this-> _ moveUploadedFile ($ fileData ['tmpname'], $ filePath); $ apkInfo = $ this-> _ apkParser ($ filePath ); // parse if ($ apkInfo ['umeng _ channel']! = 'Umeng _ CHANNEL_VALUE ') {@ unlink ($ filePath); // delete an invalid file $ this-> ajaxReturn (array ('status' => 0, 'info' => "The UMENG_CHANNEL value must be UMENG_CHANNEL_VALUE "));} $ tmpFileArr ['file _ info'] = array ('name' => $ subFile ['name'] [$ subKey], 'type' => $ subFile ['type'] [$ subKey], 'tmp _ name' => str_replace ($ apkConfig ['tmp _ CHILD_PATH ']. '/', '', $ filePath), 'error' => $ subFile ['error'] [$ subKey], 'SIZE' => $ subFile ['SIZE'] [$ subKey],); // store the value, do not upload files repeatedly} else {$ this-> ajaxReturn (array ('status' => 0, 'info' => "file cannot be blank "));} foreach ($ apkInfo as $ key => $ val) {$ apkInfoHtml. = "{$ key }:{ $ val} \ r \ n";} $ apkName = $ fileData ['filename']; $ version = $ apkInfo ['versionname'];} $ fileInfo = htmlspecialchars (json_encode ($ tmpFileArr ['file _ info']); $ fileInfoHtml = "<input name = \" apkToolFileTmp [] \ "value = '{$ fileInfo}' type = \" hidden \ "/> "; // make sure to use htmlspecialchars before output. Otherwise, the page value cannot be correctly displayed and the correct file information is obtained. $ this-> ajaxReturn (array ('status' => 1, 'info' => "uploaded successfully", 'version' => $ version, 'item' => $ item, 'apkname' => $ apkName, 'apkinfohtml '=>$ apkInfoHtml, 'fileinfo' =>$ fileInfoHtml ));}
3. with the cooperation of two parts of code, we already have the correct information on the page. Instead of submitting the file to the server when the form is submitted, when processing on the server side, you only need to move the previously uploaded temporary file to another location. This is a big success!
$ ('. Upload-file-real'). attr ('Disabled', 'Disabled '); // disable file upload before submitting a form.
4. Follow-up work
After uploading a temporary file to the server, you cannot determine whether to cancel the current operation. If yes, the temporary file will always exist on the server, we need a script to regularly clean up the temporary directory. Of course, this is very simple. You just need to find this directory and compare the time, for example, delete the file more than one day ago. Pay attention to the cleaning frequency!
5. digress
Logs are really important. If something goes wrong, where to delete files, and where to clean up the database, make sure to keep a record. Otherwise, when looking for the cause, call for help!
Uploading files to the temporary directory on the server seems simple to the backend, but you need to debug them carefully. At least it took a lot of effort to understand what I did when I was doing this little trick!
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!