php+ajaxを使ってプログレスバー付きで画像をアップロードする例を詳しく解説

墨辰丷
リリース: 2023-03-28 22:56:01
オリジナル
1574 人が閲覧しました

この記事では、主に php+ajax を使用してプログレスバー付きの画像をアップロードする機能を紹介し、更新せずに php ファイル転送と ajax 送信の関連操作テクニックを紹介します。また、読者がダウンロードして参照できるように、デモのソースコードも付属しています。必要な方は参考にしてください

ランニング効果図は次のとおりです:

コードは次のとおりです:

<?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;);
}
ログイン後にコピー

概要: 上記がこの記事の全内容です。みんなの勉強に役立ちます。

関連する推奨事項:

PHPによって実装されたカスタム配列ソート関数とソートクラス

PHPはWebページ内のすべての固定シードリンクをバッチで取得するメソッドを実装します

PHPは2次元配列を実装します特定の列による並べ替え方法_phptips

以上がphp+ajaxを使ってプログレスバー付きで画像をアップロードする例を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!