Php uploads and replaces images. _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Php uploads and replaces images ,. Php uploads and replaces images. First, the form code for creating two files: change.htmland change.phpchange.html is as follows: htmlheadtitlechangefileexample. ti php uploads and replaces images,

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

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

change file example.
 

Here are a few things to pay attention to. First, let's take a look at this sentence.

Here we use the POST method. some browsers also support the PUT method. of course, this requires modifications to the script, which I do not recommend. Enctype = "multipart/form-data must be set in the form. in this way, the server will know that the uploaded file contains the regular form information. Remember, this is required. In addition, a hidden domain is required to limit the maximum length of the uploaded file: Here, the name must be set to MAX_FILE_SIZE, and its value is the maximum length of the uploaded file. the unit is B. here I am limited to 2 MB. Let's look at this sentence again: , Type = "file" describes the file type, so a basic file upload interface is complete. Next, let's talk about how to use PHP to process uploaded files. In addition, your php. the maximum length of the uploaded file set in ini may affect your actual upload. modify the file according to the actual situation. In addition, the PHP upload is first uploaded to the temporary directory, which is being moved to the specified directory; the temporary directory can be modified as needed, or the default value can be used ......

The following code submits the change. php file in a form to see what this file has:

<? Phpheader ("content-type: text/html; charset = utf-8");/*** @ param string $ oldfile name to be replaced (including the specific path name) */function changeFile ($ oldfile) {$ newfile = $ _ FILES ['file'] ['name']; // Get the Upload file name $ fileclass = substr (strrchr ($ newfile ,'. '), 1); // Get the Upload file extension, and use $ type = array ("jpg", "gif", "bmp", "jpeg ", "png"); // set the type of the file to be uploaded if (in_array (strtolower ($ fileclass), $ type) {if (file_exists ($ oldfile )) {unlink ($ oldfile);} if (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name']) {// if (move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], $ oldfile) that must be uploaded through PHP's http post Upload mechanism )) {// output image preview echo"
  
   
You have uploaded the file and uploaded the image to preview it:
  
";}} Else {echo" Upload failed. the file is larger than 2 MB. please upload it again! ";}} Else {$ text = implode (", ", $ type); echo" You can only upload the following types of files: ", $ text ,"
"; // Echo" script alert ('You can only upload the following types of files: $ text') script ";}} changeFile ("./files/1.png ");

You may be a little dizzy at first ~~, Take a look, you will find that this is so easy !! First, let's talk about the principle. This program uses image uploading as an example to determine whether the file type is in the image format. if so, upload the file and then upload the file to and replace the specified file, after a successful upload, the uploaded image is previewed. Here we need to explain some functions in the program. First Read substr (strrchr ($ newfile ,'. '), 1). What is the role of the strrchar () function? let me give you an example. for example, for an image file pic.jpg, we use strrchar(processing, strrchr(pic.jpg,'.'. Will it be returned to .jpg? This function returns the string after the position of the specified character at the end of the string. With substr (), we can get the jpg file, so that we can get the file suffix to determine whether the uploaded file meets the specified format. In this program, the specified format is placed in an array. you can add it as needed during actual use.
Next, we call the function to determine the file type and convert it to lower case strtolower ($ _ FILES ['file'] ['name']). here is a key Dongdong $ _ FILES, which is a super Global Array and stores the form data to be processed. if register_globals is enabled, you can directly access it, but this is not safe. See the upload interface.According to the form name, we can get a lot of information:
$ _ FILES ['file'] ['name'] -- get the file name.
$ _ FILES ['file'] ['tmp _ name'] -- get the temporary storage location
$ _ FILES ['file'] ['size'] -- get the file size.
$ _ FILES ['file'] ['type'] -- obtain the file MIME type.
With this information, you can easily determine the file information. is it very convenient? ^ _ ^. Next, there are some functions that need to be understood. file_exists () -- determine whether the specified directory exists. if it does not exist, we certainly cannot upload it (it seems like it is nonsense !), Is_uploaded_file -- determine whether the file has been uploaded through http post, move_uploaded_file -- move the uploaded file to the specified directory. After a successful Upload, we will preview the output. Otherwise, the upload will fail ......

The content is very detailed, and the process is recorded for your careful research. I hope it will help you learn it.

Articles you may be interested in:
  • PHP image upload code
  • PHP supports uploading images in multiple formats (jpg, png, and gif)
  • Php collects images in articles and replaces them with local ones (implementation code)
  • Php regular expression obtains any attribute in the img Mark (regular expression replacement removes or changes any attribute in the img Mark of the image)
  • PHP swfupload image Upload instance code
  • Thinkphp implements image upload function sharing
  • Php + js implement asynchronous image Upload instance sharing
  • Use ThinkPHP + Uploadify to upload images
  • Php multi-file and image upload examples
  • PHP + jQuery + Ajax for multi-image Upload

First, create two files: change.html and change. php change.html. The form code is as follows: htmlheadtitlechange file 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.