Two kinds of PHP implementation picture upload Method _php Skill

Source: Internet
Author: User

Picture uploads are often used in projects, and almost no item can be detached from a picture or uploaded to a file. In this article I will introduce you to the two kinds of conventional upload methods. (Note: Here we are just implementing the function, not doing too many front end styles)
first, use form form to upload
This is the most original way to upload, the front end is a simple form form, the back end we have the processing of PHP files transferred over.
First look at the front end of the code upload.html

<form action= "handle.php" name= "form" method= "POST" enctype= "Multipart/form-data" >
  <input type= "File" Name= "file"/>
  <input type= "Submit" name= "submit" value= "Upload"/>
</form>

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

$file = $_files[' file '];//get the transmitted data
//Get the file name
$name = $file [' name '];
$type = Strtolower (substr ($name, Strrpos ($name, '. ') +1)); Get file types, and all are converted to lowercase
$allow _type = array (' jpg ', ' jpeg ', ' gif ', ' PNG ');//define the type of permission to upload
//Determine whether the file type is allowed to upload
if (!in_ Array ($type, $allow _type)) {
  //If not allowed, direct stop program to run return
  ;
}
Determine if the If
(!is_uploaded_file ($file [' tmp_name ']) is uploaded via HTTP POST {
  //if not via HTTP POST upload return
  ;
}
$upload _path = "d:/now/"; Upload the file's path
//start moving the 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 wrong situation did not deal with, however, the use of PHP to deal with the principle of uploading files is such, we can optimize on this basis, so that it more perfect.
second, using uploadify to achieve no refresh and with progress bar upload
first download the Uploadify plug-in, save it under a folder in your project, and then introduce three files

<link href= "Js/uploadify/uploadify.css" rel= "stylesheet" type= "Text/css"/> <script "type=" text/
JavaScript "src=" Js/uploadify/jquery.min.1.7.js "></script>
<script type=" Text/javascript "src=js/ Uploadify/jquery.uploadify.min.js "></script>

Then reference the following code

<script type= "Text/javascript" > $ (Function () {$ (". Uploadbutton"). each (function () {Bidtype
        = "Upload";
          $ (this). Uploadify ({swf: ' js/uploadify/uploadify.swf ', uploader: "handle.php",//handling uploaded PHP files or methods Multi:true,//whether to open a one-time upload multiple files queuesizelimit:20,//maximum allowable number of uploaded files Buttontext:bidtype,//Button text Character height:34,//button height width:82,//button width Auto:false,///select image after Move Upload method: ' Post ', filetypeexts: "*.JPG;*.PNG;*.GIF;*.JPEG;",//allowable file type Filetypedesc: " Please select picture file ",//File Description postdata:{}, FormData: {" Imgtype ":" Normal "," timestamp ":" ASDFSA "," token ":" 48f26 2516b3912a060d21ef6af564668 "},///Submit to server-side parameters onuploadsuccess:function (file, data, response) {//A file uploaded after successful response
          Processing var data = $.parsejson (data);
      }
        });
  })
    }); </script> <body> <span ID= "Commercial_upload" class= "Uploadbutton" ></span> </body>
 

The style is as follows

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

$file = $_files[' filedata '];//get the transmitted data
//Get the file name
$name = $file [' name '];
$type = Strtolower (substr ($name, Strrpos ($name, '. ') +1)); Get file types, and all are converted to lowercase
$allow _type = array (' jpg ', ' jpeg ', ' gif ', ' PNG ');//define the type of permission to upload
//Determine whether the file type is allowed to upload
if (!in_ Array ($type, $allow _type)) {
  //If not allowed, direct stop program to run return
  ;
}
Determine if the If
(!is_uploaded_file ($file [' tmp_name ']) is uploaded via HTTP POST {
  //if not via HTTP POST upload return
  ;
}
$upload _path = "d:/now/"; Upload the file's path
//start moving the 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 shows a different style, the second way is no refresh, and with the return value, can facilitate us to do other processing. Although commonly used, but will always be limited, use is not very flexible.
with the development of HTML5 more and more mature, we can use H5 some of the methods to implement the upload function, at this time the background PHP processing code is completely different from the above two ways, and the use of very flexible. Due to space restrictions, this article I do not introduce this way, interested can refer to the PHP+HTML5 to achieve no refresh picture upload Tutorial , I hope you will like.

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.