PHP implementation picture upload and replace operation _php Skill

Source: Internet
Author: User
Tags http post

First, create two files: change.html and change.php

The form code for the change.html file is as follows:

 
 

Here are a few places to pay attention to, first of all look at this sentence <form method= "POST" action= "change.php" enctype= "Multipart/form-data"; Here we use the Post method, and the individual browsers also support the Put method, which, of course, requires modifications to the script, which I do not recommend. You must set the enctype= "Multipart/form-data"in the form so that the server knows that the upload file has regular form information, and remember that this must be set. In addition, a hidden field is required to limit the maximum length of the uploaded file : <input type= "hidden" name= " max_file_size" value= "2000000", where name must be set to Max_ File_size, the value is the maximum length of the upload file, the unit is B, here I limit to 2 m. Then look at this : <input name= "file" type= "file" value= "Browse",type= "file" describes the type of files, such a basic upload file interface is completed, Now let's talk about how to use PHP to process uploaded files, In addition, the maximum length of the upload file you set in your php.ini may affect your actual upload, please change according to the actual situation, another PHP upload is uploaded to the temporary directory, moved to the specified directory, the temporary directory can be modified as needed, but also use the default value ...

The following is a form submission change.php file code to see what this file has:

 <?php header ("Content-type:text/html;charset=utf-8"); /** * @param string $oldfile The name of the file to be replaced (including the specific pathname)/function Changefile ($oldfile) {$newfile = $_files[' file ' [' Name '];//get on File name $fileclass = substr (STRRCHR ($newfile, '. '), 1)//Get upload file extension, make a judgment with $type = array ("JPG", "gif", "BMP", "JPEG", "PNG");/Set

Type if (In_array (Strtolower ($fileclass), $type) of the upload file is allowed {if (file_exists ($oldfile)) {unlink ($oldfile);} if (is_uploaded_file ($_files[' file '] [' tmp_name ']) {//Must be uploaded via the HTTP POST upload mechanism of PHP if (Move_uploaded_file $_files[' File ' [' Tmp_name '], $oldfile)] {//Output picture preview echo <center> Your files have been uploaded upload picture preview: </center><br><center
></center> '; }else{echo "<center> upload failed, file is larger than 2M, please upload!"
</center> "; }else{$text = Implode (",", $type); Echo <center> you can only upload the following types of files: ", $text, </center><br>";//echo "<
Script>alert (' You can only upload the following types of files: $text ') </script> ";
} changefile ("./files/1.png"); 

Just look at these you may be a little dizzy ~ ~, look slowly, you will find this thing so easy!! First, the principle, the program to pass pictures for example, the first to determine whether the file type is picture format, if the upload file, and then upload files to and replace the specified file, the successful upload is the output upload picture preview. Here are some explanations for some of the functions in the program. First Looksubstr (STRRCHR ($newfile, '. '), 1),What does the Strrchar () function do, for example, you know, like a picture file pic.jpg, we use Strrchar () processing, STRRCHR (pic.jpg, '. '), it will return. jpg, do you understand? The function returns a string that specifies the position of the specified character after the last occurrence of the string. With substr () we can take JPG so we get the suffix name of the file to determine if the uploaded file matches the specified format. This program places the specified format in an array, which can be added as needed when it is actually used.
Next, we call a function that determines the file type and converts it to lowercasestrtolower ($_files[' file ' [' name ']),Here is a key Dongdong $_files, which is a super global array that holds the form data that needs to be processed, and can be accessed directly if the register_globals is turned on, but it is unsafe. Just look at that upload interface <input.name= "File"Type= "File", according to this form name, we can get a lot of information:
$_files['file' ['] ' name ']--get file name
$_files['file' ['] [' tmp_name ']--get temporary storage location
$_files['file' [' Size ']--get file size
$_files['file'] [' type ']--get file MIME type
To get this information, you can easily determine the file information, is not very convenient? ^_^, then there are a few 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--to determine if the file has been uploaded via an HTTP post, move_uploaded_file--move the upload file to the specified directory. Successful upload, we will output preview, otherwise output upload failed ...

The content is very detailed, the process also records down for everybody to study carefully, hoped for everybody's study to be helpful.

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.