Two ways to upload images in PHP, _php tutorial

Source: Internet
Author: User

Two kinds of PHP implementation of image upload method,


Image uploads are often used in projects, and few items can be detached from images or file uploads. I'm here to introduce you to two kinds of conventional upload methods. (Note: Here we are only the implementation of the function, not to do too many front-end style)
First, using form form to upload
This is the most primitive way to upload, the front end is a simple form form, back end we have PHP processing transmitted over the file.
First look at the front-end code upload.html

Then create a new PHP file handle.php code as follows

$file = $_files[' file '];//gets the transmitted data//Gets the file name $name = $file [' name ']; $type = Strtolower (substr ($name, Strrpos ($name, '. ') +1)); Get the file type and convert it to lowercase $allow_type = array (' jpg ', ' jpeg ', ' gif ', ' PNG '); Defines the type of upload allowed//Determines whether the file type is allowed to upload if (!in_array ($type, $allow _type)) {  //If not allowed, then directly stop the program to run  return;} Determine if the IF (!is_uploaded_file ($file [' tmp_name ']) upload is uploaded via http POST {  //If the return is not uploaded via HTTP post  ;} $upload _path = "d:/now/"; Upload file path//Start move file to the appropriate folder if (Move_uploaded_file ($file [' Tmp_name '], $upload _path. $file [' name ']) {  echo ") Successfully! ";} else{  echo "failed!";}

Of course, this PHP processing code is not very perfect, where the error situation is not processed, but the use of PHP processing upload file principle is this, we can optimize on this basis, so that it is more perfect.
Second, using uploadify to achieve no refresh and with progress bar upload
First download the Uploadify plugin, save it under a folder in your project, and then introduce three files

 
  

Then reference the following code

  

Styles are as follows

Of course, the style of this button I changed, you can modify the style file according to your needs, change the style of this button
The same PHP code handle.php

$file = $_files[' Filedata '];//gets the transmitted data//Gets the file name $name = $file [' name ']; $type = Strtolower (substr ($name, Strrpos ($name, '. ') +1)); Get the file type and convert it to lowercase $allow_type = array (' jpg ', ' jpeg ', ' gif ', ' PNG '); Defines the type of upload allowed//Determines whether the file type is allowed to upload if (!in_array ($type, $allow _type)) {  //If not allowed, then directly stop the program to run  return;} Determine if the IF (!is_uploaded_file ($file [' tmp_name ']) upload is uploaded via http POST {  //If the return is not uploaded via HTTP post  ;} $upload _path = "d:/now/"; Upload file path//Start move file to the appropriate folder if (Move_uploaded_file ($file [' Tmp_name '], $upload _path. $file [' name ']) {  echo ") Successfully! ";} else{  echo "failed!";}

The above two ways to upload can be said that the principle of PHP processing is the same, but the foreground display of different styles, the second way is no refresh, and with the return value, you can easily do other processing. Although commonly used, but will always be restricted, use is not very flexible.
With the development of HTML5 more and more mature, we can use some of the methods in H5 to achieve the upload function, the background PHP processing code is completely different from the above two ways, and use is very flexible. Due to space limitations, this article I do not introduce this way, interested can refer to the PHP+HTML5 implementation of non-refresh image upload Tutorial , I hope you will like.

Articles you may be interested in:

    • PHP picture upload class with picture display
    • Simple PHP Image upload Program
    • PHP Image upload Class Code
    • PHP image upload storage source and can preview
    • PHP Image Upload Code
    • PHP supports multiple format image uploads (jpg, PNG, GIF supported)
    • thinkphp Image upload function sharing
    • Using thinkphp+uploadify to implement image upload function

http://www.bkjia.com/PHPjc/1094752.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094752.html techarticle Two kinds of PHP implementation of image upload method, image upload is often used in the project, almost no project can be separated from the picture or file upload. This article I introduce to you two ...

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