PHP Introductory Tutorial Upload file example detailed _php tips

Source: Internet
Author: User

This article illustrates the way PHP uploads files. Share to everyone for your reference, specific as follows:

demo1.php

<form enctype= "Multipart/form-data" action= "demo2.php" method= "POST" >
  <input type= "hidden" name= "MAX_" File_size "value=" 2000000/>
  upload file: <input type= "file" name= "UserFile"/> <input type=
  "Submit" value= "Upload"/>
</form>

demo2.php

<?php//Accept upload file//$_files; exists, but the null value//[userfile][name] Indicates the file type of the uploaded filename//[userfile][type]: For example, the file type of JPG is: Image/jpeg//[userfile][tmp_name] represents the uploaded
  The location where the file is temporarily stored C:\WINDOWS\temp\php28.tmp//[userfile][error] represents the error type, indicating that there are no errors.
  [Userfile][size] Indicates the size of the uploaded file Print_r ($_files);
  Echo ' <br/> '; Is_uploaded_file--To determine if a file is uploaded via an HTTP post or uploaded via an HTTP post, the file is stored in a temporary folder $fileMimes = Array (' Image/jpeg ', ' image/p
  JPEG ', ' image/gif ', ' image/png ', ' image/x-png '); Determines whether the type is an if (Is_array ($fileMimes)) {if (!in_array ($_files[' userfile '] [' type '], $fileMimes)) {echo &LT;SCR
      Ipt>alert (' This site only allows jpg,png,gif picture '); History.back ();</script> ";
    Exit }//Create a constant define (' URL ', DirName (__file__). '
  \uploads ');
  Echo URL;
  To determine if the directory exists if (!is_dir (URL)) {mkdir (url,0777);//maximum permission 0777, meaning if this directory is not available, then create the define (' Max_size ', 2000000); if ($_files[' userfile '] [' size '] > max_size) {echo ' <script>alert (' upload shall not exceed 2 M '); History.back (); </script > ";
  Exit //There are two questions to verify//the second issue, allowing only JPG files//if ($_files[' userfile '] [' type ']!= ' image/jpeg ' && $_files[' userfile ' [' Typ ']  E ']!= ' image/pjpeg ') {//echo "<script>alert (' site only allows JPG Pictures '); History.back ();</script>";//exit;////    Switch ($_files[' userfile ' [' type ']) {//Case ' image/jpeg '://Firefox/break;//Case ' image/pjpeg '://break;//
Case ' image/gif '://break;
Case ' image/png '://Firefox/break;
Case ' image/x-png '://ie//break;
Default:echo "<script>alert (' This site only allows jpg,png,gif Pictures '); History.back ();</script>";
Exit; //First question, if upload error, how to do if ($_files[' userfile '] [' ERROR ']>0] {switch ($_files[' userfile '] [' ERROR ']) {Case 1
      : Echo "<script>alert (' upload file exceeds the agreed value 1 '); History.back ();</script>";
      Break
      Case 2:echo "<script>alert (' upload file exceeds the agreed value of 2 '); History.back ();</script>";
      Break
   Case 3:echo "<script>alert (' partially uploaded '); History.back ();</script>";   Break
      Case 4:echo "<script>alert (' not uploaded '); History.back ();</script>";
    Break
  } exit; } if (Is_uploaded_file ($_files[' userfile ' [' tmp_name '])) {//Just moved//move_uploaded_file here--move uploaded files to the new location//First parameters, write the address of the temporary file,//The second argument, the second parameter to write the address you want to exist//to determine if the directory exists////If you want to block out the warning, add the @ if!move_uploaded_file ($_files[' Us Erfile ' [' tmp_name '],url. '
      /'. $_files[' userfile ' [' name ']] {//If the move fails, echo ' Move failed ';
    Exit
    }}else{echo "<script>alert (' temporary folder cannot find uploaded files '); History.back ();</script>";
  Exit ///All through upload succeeded//must pass a value to demo3.php//file uploaded address echo "<script>alert (' File upload succeeded '); location.href= ' demo3.php?url= '. $_f iles[' UserFile ' [' name ']. ';

 </script> ";?>

demo3.php

<?php
  $url = $_get[' url '];
  echo "

More about PHP Interested readers can view the site topics: "Summary of PHP file operations," "PHP Array Operation skills Encyclopedia", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.