PHP implementation file upload example

zhaozj2021-02-16  61

Example 1:

Upload file form page: UpdateFile.htm

Upload File Form </ Title></p> <p></ hEAD></p> <p><body></p> <p><form encType = "multipart / form-data" action = "updatefile.php3" method = "post"></p> <p><Input Type = "Hidden" name = "max_file_size" value = "1000"></p> <p><div align = "center"> <center> Please select the file:</p> <p><Input name = "userfile" type = "file"></p> <p><Input Type = "Submit" value = "send file"></p> <p></ center> </ div></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>[Ctrl A All Choose and Copy]</p> <p>Processing upload file page: UpdateFile.php3</p> <p><html></p> <p><HEAD></p> <p><meta http-equiv = "content-type" content = "text / html;</p> <p>Charset = GB2312 "></p> <p><title> Process Upload File </ Title></p> <p></ hEAD></p> <p><body></p> <p><?</p> <p>Copy ($ Userfile, "NewFileName");</p> <p>Echo $ Userfile. "- User uploaded to the name of the file temporarily stored on the server <br></p> <p>Echo $ userfile_name. "- The original name <BR>" in the user machine;</p> <p>Echo $ userfile_size. "- Upload the actual byte of the file <br>";</p> <p>Echo $ Userfile_Type. "- If the user's browser provides this information,</p> <p>It represents the type of MIME. E.g</p> <p>Image / gif <br></p> <p>?></p> <p></ body></p> <p></ html> [Ctrl A All Choose to copy]</p> <p>Note: After the file is uploaded, you must copy to a new place or rename it, otherwise the upload sample will be deleted after the process is executed.</p> <p>Example 2:</p> <p>PHP3 is a very powerful CGI scripting language, which is basically in C, which is more like Perl on its implementation, and its inherent support for the database makes it a powerful opponent of ASP.</p> <p>First, there must be an interface at the front desk.</p> <p><Html></p> <p><HEAD></p> <p><Title> File Upload Interface </ Title></p> <p></ HEAD></p> <p><Body> <table> <center></p> <p><Form encType = "multipart / form-data" name = "submitform"</p> <p>Action = "Upload.php3" Method = "post"></p> <p><Input type = "hidden" name = "max_file_size" value = "1000000"> <TR></p> <p><TD> <input name = "UPLOADFILE" type = "file" size = "30"> </ td></p> <p></ TR></p> <p><Tr></p> <p><TD> <input name = "submit" value = "Submit" type = "submit"> </ td></p> <p><TD> <input name = "reset" value = "reset" type = "reset"> </ td></p> <p></ TR></p> <p></ Form> </ center> </ table> </ body></p> <p></ Html> [Ctrl A All Choose to copy]</p> <p>It is worth noting that a hidden value domain of a max_file_size, which can limit the size of the upload file by setting its value. Then implement file upload operation in the background. If it is just the most basic upload, you can get it.</p> <p><? If ($ uploadfile! = "None") {copy ($ uploadfile, "$ uploadfile_name); unlink ($ uploadfile);} else {echo" You did not select any file upload! ";}?></p> <p>Because the value of Form is automatically assigned to the same name variable, the uploaded file can be accessed directly through the $ uploadfile, but because this is a saved file variable, the file name must be obtained by another $ uploadFile_name variable. Delete the temporary file (UNLINK) after copying the file.</p> <p>The above code is fully working, but the actual application is loophole, let us gradually improve the first, and the uploaded file must have a fixed directory save, we are saved here, such as $ uploadPath = "/ Home / Flier / Upload / ";</p> <p>Or complicated automatic positioning, such as</p> <p>$ UPLOADPATH = addslashes ($ PATH_TRANSLATED)). "// Upload //"; $ PATH_TRANSLATED Assending Six is ​​the current transfer directory</p> <p>We assume that the uploaded file is saved with a subdirectory named UPLOAD. The dirName function returns its directory name, then add the subdirectory name and save the full upload file name and path with a variable $ filename.</p> <p>$ Filename = $ uploadpath. $ Uploadfile_name;</p> <p>Second, we also want users to know the brief information of uploading files, such as the size IF of uploading files ($ UPLOADFILE_SIZE <1024) {// Upload file size $ filesize = (string) $ uploadfile_size. "Byte";} elseif $ UPLOADFILE_SIZE <(1024 * 1024)) {$ filesize = Number_Format ($ UPLOADFILE_SIZE / 1024), 1). "Kb";} else {$ filesis = number_format ($ uploadfile_size / (1024 * 1024)), 1). "MB";} Number_format function plays a role in formatting the output, please refer to the manual for specific usage. Next We must consider the case where the file already exists and the copy operation failed, and provides the corresponding prompt information if (! File_exists ($ filename)) {if ($ uploadfile, $ filename)) {echo file $ uploadfile_name $ Filesize) Upload success! ";} Else {echo" file $ uploadfile_name upload failed! ";} Unlink ($ uploadfile);} else {echo" file $ uploadFile_name already exists! ";} Then we should consider the big file upload When the timeout is prone to, you can use set_time_limit ($ TIMELIMIT); increase the timeout limit time.</p> <p>Finally, integrate the cross section and implementation code into a separate file. In order to achieve this idea, we add a hidden value to the Form <input type = "hidden" name = "upload" value = "1" pointing out the current State (interface or implementation) to distinguish the treatment code as follows:</p> <p>Upload.php3:</p> <p><?</p> <p>IF ($ uploadAction):</p> <p>?></p> <p><Html></p> <p><HEAD></p> <p><Title> File Upload Interface </ Title></p> <p></ HEAD></p> <p><Body> <table> <center></p> <p><Form encType = "multipart / form-data" name = "submitform"</p> <p>Action = "Upload.php3" Method = "post"></p> <p><Input Type = "Hidden" name = "max_file_size" value = "1000000"></p> <p><Input Type = "Hidden" name = "UploadAction" value = "1"></p> <p><Tr></p> <p><TD> <input name = "UPLOADFILE" type = "file" size = "30"> </ td></p> <p></ TR></p> <p><Tr></p> <p><TD> <input name = "submit" value = "Submit" type = "submit"> </ td></p> <p><TD> <input name = "reset" value = "reset" type = "reset"> </ td></p> <p></ TR></p> <p></ Form> </ center> </ table> </ body></p> <p></ Html></p> <p><?</p> <p>Else:</p> <p>?></p> <p><Html></p> <p><HEAD></p> <p><Title> file upload code </ title></p> <p></ HEAD></p> <p><Body></p> <p><?</p> <p>$ UPLOADACTION = 0;</p> <p>$ TIMELIMIT = 60; / * Set timeout limit</p> <p>Default time is 30 seconds</p> <p>When set to 0, it is not limited. * /</p> <p>SET_TIME_LIMIT ($ TIMELIMIT);</p> <p>IF ($ UPLOADFILE! = "NONE") &&</p> <p>($ UPLOADFILE! = ""))</p> <p>{</p> <p>$ UploadPath = addslashes (DirName ($ PATH_TRANSLATED). "// Upload //";</p> <p>// Upload file storage path</p> <p>$ Filename = $ uploadPath. $ Uploadfile_name; // upload file name</p> <p>IF ($ uploadfile_size <1024) // upload file size</p> <p>{</p> <p>$ Filesize = (string) $ uploadfile_size. "Bytes";</p> <p>}</p> <p>Elseif ($ UPLOADFILE_SIZE <(1024 * 1024)))</p> <p>{</p> <p>$ FileSize = Number_Format (($ UPLOADFILE_SIZE / 1024), 1). "Kb";</p> <p>}</p> <p>Else</p> <p>{</p> <p>$ FileSize = Number_Format ($ UPLOADFILE_SIZE / (1024 * 1024)), 1). "MB";</p> <p>}</p> <p>IF (! File_exists ($ filename))</p> <p>{</p> <p>IF (Copy ($ UPLOADFILE, $ FileName))</p> <p>{</p> <p>Echo "File $ uploadFile_name ($ filesize) upload success!";</p> <p>}</p> <p>Else</p> <p>{</p> <p>Echo "file $ uploadfile_name upload failed!";</p> <p>}</p> <p>Unlink ($ uploadfile);</p> <p>}</p> <p>Else</p> <p>{</p> <p>Echo "file $ uploadfile_name already exists!";</p> <p>}</p> <p>}</p> <p>Else</p> <p>{</p> <p>Echo "You didn't choose any file upload!";</p> <p>}</p> <p>SET_TIME_LIMIT (30); // Restore the default timeout setting</p> <p>?></p> <p><br> <a href = "upload.php3"> Return </A></p> <p></ Body></p> <p></ Html></p> <p><?</p> <p>Endif;</p> <p>?> [Ctrl A All Choose to copy]</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24176.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="24176" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.045</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '2kwL_2B5lVWdSP5rvB0v_2FVNGCB6i5W_2BVHQsYnxkGMTr3cGGi_2Bi_2FdxLxx0cwGvAyhpRgbvc0eDstMx30h_2Fd0tzSWQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>