Create two files first: change.html and change.php
The form code for the change.html file is as follows:
Change file example.
Here are a few things to pay attention to, first look at this sentence
,Here we use the Post method, the individual browser also supports the Put method, of course, this need to modify the script, I do not recommend this. The form must be set enctype= "Multipart/form-data, so that the server knows the upload file has regular form information, remember, this must be set. Also requires a hidden field to limit the maximum length of uploaded files :,Here the name must be set to Max_file_size, its value is the maximum length of the uploaded file, the unit is B, here I limit to 2 m. Look at that again. :,Type= "File" describes the type of files, so that a basic upload file interface is complete, then the next talk about how to use PHP to process the uploaded files, In addition, the maximum length of upload file you set in the php.ini may affect your actual upload, please modify according to the actual situation, and another PHP upload is uploaded to the temporary directory, in the designated directory, the temporary directory can be modified as needed, you can also use the default value ...
Here is the form submission change.php file code to see what this file has:
<?phpheader ("Content-type:text/html;charset=utf-8"); /*** @param string $oldfile The file name that needs to be replaced (including the specific pathname) */function Changefile ($oldfile) {$newfile = $_files[' file '] [' name '];// Get upload filename $fileclass = substr (STRRCHR ($newfile, '. '), 1);//Get upload file extension, make judgment with $type = array ("JPG", "gif", "BMP", "JPEG", "PNG") ;//Set the type of allow file to be uploaded if (In_array (Strtolower ($fileclass), $type)) {if (file_exists ($oldfile)) {unlink ($oldfile);} if (is_uploaded_file ($_files[' file ' [' Tmp_name '])) {//Must be uploaded via PHP's HTTP POST upload mechanism if (move_uploaded_file ($_files[') File '] [' tmp_name '], $oldfile) {//Output picture preview echo "
Your file has been uploaded upload picture preview:
";}} Else{echo "
Upload failed, file is larger than 2M, please re-upload!
";}} else{$text = Implode (",", $type); echo "
You can only upload the following types of files: ", $text,"
";//Echo" ";}} Changefile ("./files/1.png");
Just look at these you may be a little dizzy ~ ~, look slowly, you will find actually this thing so easy!! First, the principle, the program above the picture as an example, first determine whether the file type is a picture format, if the upload file, then upload the file to and replace the specified file, the successful upload output upload image preview. Some of the functions in the program are explained here. Look first.substr (STRRCHR ($newfile, '. '), 1),What does the Strrchar () function do, and I give an example that everyone knows, like a picture file pic.jpg, we use Strrchar () processing, STRRCHR (pic.jpg, '. '), which will return. jpg, do you understand? The function returns the string of the specified character after the last occurrence of the string. With substr () we can take a jpg, so we get the file suffix, to determine whether the upload file conforms to the specified format. This program puts the specified format in an array, which can be added as needed when actually used.
Next, we call the function that determines the file type and converts it to lowercasestrtolower ($_files[' file ' [' name ']),Here's a very important thing $_files, this is a super global array that preserves the form data that needs to be processed, and if Register_globals is turned on, it can be accessed directly, but it's not safe. Look at that upload interface.Name= "File"Type= "File", according to this form name, we can get a lot of information:
$_files['file' [' name ']--get the file name
$_files['file' [' tmp_name ']--get temporary storage location
$_files['</span>file' [' Size ']--get file size
$_files['file' [' type ']--get file MIME type
Get this information, you can easily judge the information of the document, is not very convenient? ^_^, then there are some functions to understand, file_exists ()--to determine whether the specified directory exists, does not exist we certainly can not upload (like nonsense!). ), is_uploaded_file--determines whether the file has been uploaded via HTTP post, move_uploaded_file--moves the uploaded file to the specified directory. Upload successfully, we will output preview, otherwise the output upload failed ...
The content is very detailed, the process is also recorded for everyone to study carefully, hope that everyone's study helps.
The above describes the PHP implementation of the image upload and replace the operation, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.