Home > Backend Development > PHP Tutorial > Detailed explanation of the example of uploading pictures with progress bar using php+ajax

Detailed explanation of the example of uploading pictures with progress bar using php+ajax

墨辰丷
Release: 2023-03-28 22:56:01
Original
1628 people have browsed it

This article mainly introduces the php ajax function of uploading pictures with a progress bar, involving php file transfer and ajax non-refresh submission related operation skills, and comes with demo source code for readers to download and reference, friends in need can refer to it

The running effect diagram is as follows:

The code is as follows:

<?php
if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
  ############ Edit settings ##############
  $UploadDirectory  = &#39;F:/Websites/file_upload/uploads/&#39;; //specify upload directory ends with / (slash)
  ##########################################
  /*
  Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
  Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
  and set them adequately, also check "post_max_size".
  */
  //check if this is an ajax request
  if (!isset($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;])){
    die();
  }
  //Is file size is less than allowed size.
  if ($_FILES["FileInput"]["size"] > 5242880) {
    die("File size is too big!");
  }
  //allowed file type Server side check
  switch(strtolower($_FILES[&#39;FileInput&#39;][&#39;type&#39;]))
    {
      //allowed file types
      case &#39;image/png&#39;:
      case &#39;image/gif&#39;:
      case &#39;image/jpeg&#39;:
      case &#39;image/pjpeg&#39;:
      case &#39;text/plain&#39;:
      case &#39;text/html&#39;: //html file
      case &#39;application/x-zip-compressed&#39;:
      case &#39;application/pdf&#39;:
      case &#39;application/msword&#39;:
      case &#39;application/vnd.ms-excel&#39;:
      case &#39;video/mp4&#39;:
        break;
      default:
        die(&#39;Unsupported File!&#39;); //output error
  }
  $File_Name     = strtolower($_FILES[&#39;FileInput&#39;][&#39;name&#39;]);
  $File_Ext      = substr($File_Name, strrpos($File_Name, &#39;.&#39;)); //get file extention
  $Random_Number   = rand(0, 9999999999); //Random number to be added to name.
  $NewFileName    = $Random_Number.$File_Ext; //new file name
  if(move_uploaded_file($_FILES[&#39;FileInput&#39;][&#39;tmp_name&#39;], $UploadDirectory.$NewFileName ))
    {
    die(&#39;Success! File Uploaded.&#39;);
  }else{
    die(&#39;error uploading File!&#39;);
  }
}
else
{
  die(&#39;Something wrong with upload! Is "upload_max_filesize" set correctly?&#39;);
}
Copy after login

Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.

Related recommendations:

Custom array sorting function and sorting class implemented in PHP

PHP implements batch acquisition of web pages All methods of fixed seed links

PHP method to implement two-dimensional array sorting by a certain column_phpTips

The above is the detailed content of Detailed explanation of the example of uploading pictures with progress bar using php+ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template