PHP implementation of image upload and replace, _php tutorial

Source: Internet
Author: User

PHP implementation of image upload and replace the operation,


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['</span>file' [' tmp_name ']--get temporary storage location
$_files['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.

Articles you may be interested in:

    • PHP Image Upload Code
    • PHP supports multiple format image uploads (jpg, PNG, GIF supported)
    • PHP capture images in the article get replaced locally (Implementation code)
    • PHP takes any attribute in the IMG tag (regular substitution removes or alters any attribute in the image img tag)
    • PHP swfupload Image Upload Instance code
    • thinkphp Image upload function sharing
    • PHP+JS implementation of asynchronous image upload instance sharing
    • Using thinkphp+uploadify to implement image upload function
    • PHP multiple file and image upload examples of detailed
    • Php+jquery+ajax achieve multi-image upload effect

http://www.bkjia.com/PHPjc/1110067.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110067.html techarticle PHP Implementation of the image upload and replace the operation, first set up two files: change.html and change.php change.html file form code as follows: Htmlheadtitlechange files Example./ti ...

  • 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.