Tips for uploading PHP files to the backend: _ PHP Tutorial

Source: Internet
Author: User
Tips for uploading php files to the backend ,. Tips for uploading php files to the backend. This article describes some tips for uploading php files to the backend! Business Scenario 1: We only select a single file upload. tips for uploading php files to the backend,

In this article, I will talk about some tips for uploading php files to the backend!

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 =""; // Be 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

LogsIt is really important to keep a record of what went wrong, where to delete the file, and where to clean up the database!

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!

The above is all the content of this article. I hope you can master the backend processing skills for uploading php files. thank you for reading this article.

Tips: What are the tips for processing php files uploaded to the backend! Business Scenario 1: We only select a single file to upload ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.