This article says, PHP file upload back-end processing have some skills!
Business Scenario One , we will only choose a single file upload, and do not need to do some real-time validation work. Well, maybe there is no optimization to speak of, because, finally you have to do, just put this file in the table dropdowns the last submission, directly processing can!
Business Scenario Two , need to upload multiple files, and need to verify the internal content of the file, and the corresponding page display. For this situation, after the user chooses to upload the file, we need to upload the file immediately, because we need to read the information in the file, in the final submission, we also need to submit a file. Obviously, there is a duplication of work, a time-consuming user time, two is the cost of server bandwidth resources! optimization, can want to get the method is also very simple, can not upload the file after the first time, the file is kept in the server, the real submission of the form, to read this has been uploaded temporary files. Yes, that's the way we handle it!
business Scene three , similar to scene two, need to upload multiple files, but multiple files may be uploaded separately. That we may have uploaded 10M for the first time, the second upload of 10M, a total of 10 uploaded, then, on the server side, a one-time submission must be exceeded the size of the upload limit, but if we are the upload every time, this is possible, and the final submission, We just need to pass the short text message up!
The idea is really simple, it seems, is no problem, but, maybe I was limited ability, at that time it took me a lot of time to deal with this what ghost! Below, I will give some sample code for reference:
File Upload Tips (a single uploaded file exists on the server side as a temporary file) sample code:
1. Page JS processing
Click to select Finish file, trigger upload file operation, upload file to server temp directory $ ('. Upload-real-file '). Off (). On (' Change ', function () {if (!$ (). Val ())
{return false;
var Responseobjid = $ (this). attr (' Response-id ');
var responseobj = $ (' # ' + Responseobjid);
$ (' #Form '). Ajaxsubmit ({url: '/aa/bb/uploadtmpapktool ', Resetform:false, DataType: ' JSON ',
Beforesubmit:function (option) {window.loading = Layer.load (2);
}, Success:function (data, statustext) {layer.close (window.loading);
if (data.status = = 1) {responseobj.html (data.apkinfohtml); var parentcontainer = Responseobj.parent (). Parent (), Namecontainer = Parentcontainer.find ('. File-name-contai
Ner ');
Namecontainer.html (Data.apkname);
Namecontainer.attr (' title ', Data.apkname); Responseobj.find ('. file-tmp '). HTML (data.fileinfo); Store the file information in a hidden field so that $ (submitid) can be found at the time of submission. RemoveattR (' disabled ');
}else{Layer.alert (Data.info);
}, Error:function (data) {layer.close (window.loading); Layer.alert (' unknown error, please try again later!
');
}
}); Return false;//prevent dialog from automatically closing});
2. Obviously, the page needs to get file information, background processing code (PHP)
$apkConfig = $this->_getapkconfig ();
$params = $this->getfilteredparam (' get ');
$subFile = $_files[' apktoolfiles '];
$apkName = $apkInfoHtml = "";
if (empty ($subFile)) {$this->ajaxreturn (' status ' => -4, ' info ' => ' Please select file to upload ')); foreach ($subFile [' name '] as $subKey => $subVal) {if ($subFile [' name '] [$subKey]) {$fil
Edata = $this->_getfiledata ($subFile, $subKey); $checkData = Array (' maxSize ' => $apkConfig [' file_max_size '], ' Savepath ' => $apkConfig [' Tmp_chil D_path '], ' Extarr ' => array (' apk '), ' Releasename ' => str_replace ('. apk ', ', $fileData [' FileName
']),//unique);
$checkResult = $this->_checkfiledata ($fileData, $checkData);
if ($checkResult [' status ']!= 1) {$this->ajaxreturn ($checkResult); //Move 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 invalid file $this->ajaxreturn (' status ' => 0, ' info ' => ' umeng_channel value if umeng_channel_value)); $TMPFILEARR [' file_info '] = Array (' name ' => $subFile [' name '] [$subKey], ' type ' =& Gt $subFile [' type '] [$subKey], ' Tmp_name ' => str_replace ($apkConfig [' Tmp_child_path '].
'/', ', $filePath ', ' Error ' => $subFile [' Error '] [$subKey], ' size ' => $subFile [' Size '] [$subKey], );
Dump the value, no longer repeat the upload file} else {$this->ajaxreturn (' status ' => 0, ' info ' => ' file cannot be empty '));
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\ "/>"; Be sure to use htmlspecialchars before the output, otherwise it will not display the page face value correctly and get to the correct file information $this->ajaxreturn (' status ' => 1, ' info ' => "upload success", ' Version ' => $version, ' item ' => $item, ' apkname ' => $apkName, ' apkinfohtml ' => $apkInfoHtml, ' FileInfo ' =>
$fileInfoHtml));
}
3. By the combination of two parts of code, we already have the right information on the page, just want to submit the form at the end of the time, do not submit files to the server, in the server-side processing, just upload the temporary files before moving the location can be, so even if it is done!
$ ('. Upload-file-real '). attr (' disabled ', ' disabled '); Disable uploading files before submitting a form
4. Follow-up work
After uploading the temporary files to the server, there is no way to determine whether the user canceled the current operation, if canceled, the temporary files will always exist on the server, so we need a timed cleanup of the temporary directory script. Of course, this is very simple, just need to find this directory, compare the time, such as files more than a day ago to be deleted. Pay attention to control the cleaning frequency can!
5. digression
The log is really important, where the error, where to delete the file, where to clean up the database, we must do a good record!
Upload files to the server temp directory, the back-end processing principle looks very simple, but also need you to carefully debug, at least at the outset I do this small function, really cost a lot of strength just wisp clear!
The above is the entire content of this article, I hope you can grasp the php file upload back-end processing skills, thank you for your reading.