This article for everyone in detail mainly introduces the PHP AJAX implementation of non-flush attachment upload function of the specific code, with a certain reference value, interested in small partners can refer to
For a website, there is a basic indispensable function, that is the file upload. Using PHP preload to achieve file upload is a unique advantage, then today, together to do a non-flush implementation of the file upload it.
--------------------------------------------------------------------------------
Normal forms
Front Page
<form action= "./fileupload.php" method= "POST" ><p>username<input type= "text" name= "Username"/> </p><p>password<input type= "Password" name= "Password"/></p><p>e-mail<input type= "Text" name= "email"/></p><input type= "Submit" value= "Register"/></form>
Background processing
Print_r ($_post); $username = $_posy[' username '); $password = $_post[' password ']; $email = $_post[' email '];// The relevant business logic can be implemented by comparing the database content information on the server.
···
A form with a file
The form with the file and the normal form are very different, first we want to declare a property on the form, as follows:
<form enctype= ' Multipart/form-data ' >
To tell the server that the form we have uploaded contains file information.
Refresh Mode
When it comes to refresh mode, the page jumps to the business processing interface when the submit is clicked. This is also the data submission for the form that we implemented in the normal way.
Front-End Interface
<form action= "./fileupload.php" enctype= "Multipart/form-data" method= "POST" ><p>username<input type= "Text" name= "username"/></p><p>password<input type= "Password" name= "Password"/></p> <p>e-mail<input type= "text" name= "email"/></p><p>photo<input type= "file" name= "Photo"/ ></p><input type= "Submit" value= "Register"/>
Background page
Background processing In addition to the normal form data, the most important thing is to process the file information. PHP built-in a function, you can easily transfer the uploaded files from the temporary data area to our target folder, the implementation of the business logic upload.
Move_uploaded_file (' Temporary file path ', ' destination file path ');
No Refresh Mode
The only difference between using a no-Refresh method and using refresh mode is to prevent the page from jumping, which we usually do in two ways.
• Add return False at the end of the form submission event.
<script>var form = document.getelementsbytagname (' form ') [0];form.onsubmit = function () { //to do something ··· Block page jump return false;} </script>
• Use H5 related methods.
<script>var form = document.getelementsbytagname (' form ') [0];form.onsubmit = function (event) { //To do something · Block page Jump event.preventdefault ();} </script>
Other handling is consistent with the jump.
Large File Upload
Although PHP makes it easy to upload files, it is also very fast. However, the size of the uploaded file is not unlimited. By default, there are two factors that limit the size of the files we upload.
The extremum of Post
The extremum of upload
We can manually change the php.ini configuration information to achieve the control of the upload file size.
Post Extremum
Post_max_size = 200M
Upload Extremum
upload_max_filesize=200m
Upload Details
Ajax objects have a property called upload, and upload exist as an object. It has a method called onprogress, we can monitor this method to see the percentage of the file upload process.
Front Page
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Background processing
<?php//print_r ($_post);//echo "---------------------". " <BR> ";//print_r ($_files);///Accessories storage location, attachment name, remember must exist upload folder $path =". /upload/";//Gets the original name of the file $origin_name = $_files[' photo ' [' name '];//stitching into the name of the file on the server $server_name = $path. $origin _name;if ( $_files[' photo ' [' ERROR ']>0) {die ("Error! ". $_files[' photo ' [' error ']);} if (Move_uploaded_file ($_files[' photo ' [' tmp_name '], $server _name)) { echo <BR>. " Upload success! ";} else{ echo "<BR>". " Upload failed! ". $_files[' photo ' [' Error ']; }? >
Remember to ensure the existence of the upload folder and the path problem.
Summarize
File uploads are a very basic feature and are not very complex to implement. But this function is so important, we can easily through the file to modify the user such as Avatar, as well as other file information.
The above is the entire content of this article, I hope to be able to help you learn.