PHP to Upload and Verify Local Pictures

  • 2021-07-24 09:30:38
  • OfStack

Today, we study the FILES function of PHP under 1, Use PHP to upload local pictures, Verify that the file is a picture, And save the pictures in the specified directory to achieve the function of visiting pictures, which is still very fun. 1 There are two PHP pages, one for uploading and displaying pictures at the front desk, and the other for processing and saving pictures. The code has made very detailed comments, and the syntax of FILES function, error error type has been written out, and there are some matters needing attention.

FILES function

1. Original name of $_ FILES ['myFile'] ['name'] client file
2. The MIME type of the $_ FILES ['myFile'] ['type'] file requires browser support for this information, such as "image/gif"
3. $_ FILES ['myFile'] ['size'] The size of the uploaded file, in bytes
4. The temporary file name stored on the server after the file $_FILES ['myFile'] ['tmp_name'] is uploaded, 1 is generally the default of the system, and can be specified in upload_tmp_dir of php. ini

On the types of errors reported by error in FILES and its attention:

Filter error number

0: No error occurred, file upload succeeded
1: The uploaded file exceeds the limit of the upload_max_filesize option in php. ini
2: The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form
3: Only part of the file is uploaded
4: No files were uploaded
5: Upload file size is 0

Note:

1. After the file is uploaded, it is stored in the temporary directory by default. At this time, it must be deleted from the temporary directory or moved to other places. If not, it will be deleted. That is, no matter whether the upload is successful or not, the files in the temporary directory will definitely be deleted after the script is executed. Therefore, before deleting it, you should copy it to other locations with copy () function of PHP, and then you will complete the file uploading process.
2. Before PHP 4.1. 0, the array was named $HTTP_POST_FILES, and it is not an automatic global variable like _ FILES 1. HTTP_POST_FILES arrays are not supported by PHP 3.
3. When uploading a file with form, 1 must add the attribute content enctype= "multipart/form-data", otherwise an exception will be reported when obtaining file information with $_ FILES [filename].

Here's the code

index.php


 <?php
 header("Content-Type:text/html;charset=utf-8");
 ?>
 <title>PHP Upload and verify pictures </title>
 <style>
 table{text-align:center;}
 </style>
 <div align="center">
 <h1> Upload function </h1>
 <form action="index_ok.php" method="post" enctype="multipart/form-data">
   Upload pictures: <input type="file" name="pic">
  <input type="submit" name="sub" value=" Upload ">
 </form>
 <table width="500">
  <tr bgcolor="#cccccc">
  <th> Serial number </th>
  <th> Picture </th>
  <th> Adding time </th>
  <th> Operation </th>
  </tr>
  <?php
  //1  Open Directory 
  $dir=opendir("./uploads");
  //2  Traverse the directory and output the pictures inside 
  $i=0;
  while($f=readdir($dir)){
   $i++;
   // Read the files in the directory and simulate the database operation 
   if($f!="." && $f!=".."){
    $filename="./uploads/$f";
    echo "<tr>";
    echo "<td>{$i}</td>";
    echo "<td><img src='./uploads/{$f}' width='80' height='60'></td>";
    echo "<td>".date("Y-m-d",filectime($filename))."</td>";
    echo "<td><a href='./uploads/{$f}'> View </a></td>";
    echo "</tr>";
   }
  }
  //3  Close the directory 
  closedir($dir);
  ?>
 <tr bgcolor="#cccccc"><td colspan="4">&nbsp;</td></tr>
 </table>
 </div>

upload.php


 <?php
 header("Content-Type:text/html;charset=utf-8");
  if($_FILES['pic']['error']>0){
   echo $_FILES['pic']['error'];
   echo " Error uploading file! ";
   echo '<meta http-equiv="refresh" content="3;url=index.php">'; // Automatic jump back index Documents 
  }else{
   // Begin to get the information of uploaded files 
   $file=$_FILES['pic'];
   //var_dump($file); Print all the information in the file 
   //name: Upload file name 
   //type: Type of file uploaded 
   //tmp_name: Temporary files after successful upload 
   //size: Size of uploaded file 
   //error: Error message for uploading files 
   $uploaddir="./uploads/"; // Select the file directory to upload 
   //$uploadfile=$uploaddir.basename($file['name']);// Get the name of the uploaded file 
   // The name of the parse file 
   $fileinfo=pathinfo($file['name']);
 //  echo $fileinfo['extension'];  Gets the type of the file 
   do{
    $newfile=date("YmdHis").rand(1000,9999).".".$fileinfo['extension'];// Change the name of the file, get the 1 A new name 
   }while(file_exists($uploaddir.$newfile));

   // Type restrictions on uploaded files 
   if (!(($file['type'] == "image/gif")||($file['type'] == "image/jpeg")||($file['type'] == "image/pjpeg"))){
    die(" Wrong file type! ");
    echo '<meta http-equiv="refresh" content="3;url=index.php">';
   }
   // Size limit of uploaded files 
   if($file['size'] > 2*1024*1024){
    die(" Upload files exceeding 2MB ! ");
    echo '<meta http-equiv="refresh" content="3;url=index.php">';
   }
   // Start uploading files 
   if (is_uploaded_file($file['tmp_name'])) {
    if (move_uploaded_file($file['tmp_name'], $uploaddir.$newfile)) {
     echo " Upload successfully! ";
     echo '<meta http-equiv="refresh" content="3;url=index.php">';// Automatic jump back index Documents 
    } else {
     echo " Upload failed, please wait a moment! ";
     echo '<meta http-equiv="refresh" content="3;url=index.php">'; // Automatic jump back index Documents 
    }
   }

  }
 ?>

Related articles: