pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='iOyeB'><tr id='iOyeB'><dt id='iOyeB'><q id='iOyeB'><span id='iOyeB'><b id='iOyeB'><form id='iOyeB'><ins id='iOyeB'></ins><ul id='iOyeB'></ul><sub id='iOyeB'></sub></form><legend id='iOyeB'></legend><bdo id='iOyeB'><pre id='iOyeB'><center id='iOyeB'></center></pre></bdo></b><th id='iOyeB'></th></span></q></dt></tr></i><div class="p7wgjw3" id='iOyeB'><tfoot id='iOyeB'></tfoot><dl id='iOyeB'><fieldset id='iOyeB'></fieldset></dl></div>
  • <tfoot id='iOyeB'></tfoot>
    1. <small id='iOyeB'></small><noframes id='iOyeB'>

        <legend id='iOyeB'><style id='iOyeB'><dir id='iOyeB'><q id='iOyeB'></q></dir></style></legend>
        • <bdo id='iOyeB'></bdo><ul id='iOyeB'></ul>

        PHP 驗證文件上傳

        PHP Validating the File Upload(PHP 驗證文件上傳)
      1. <legend id='xllJr'><style id='xllJr'><dir id='xllJr'><q id='xllJr'></q></dir></style></legend>
        • <bdo id='xllJr'></bdo><ul id='xllJr'></ul>

              <tbody id='xllJr'></tbody>

            <small id='xllJr'></small><noframes id='xllJr'>

                <tfoot id='xllJr'></tfoot>

                <i id='xllJr'><tr id='xllJr'><dt id='xllJr'><q id='xllJr'><span id='xllJr'><b id='xllJr'><form id='xllJr'><ins id='xllJr'></ins><ul id='xllJr'></ul><sub id='xllJr'></sub></form><legend id='xllJr'></legend><bdo id='xllJr'><pre id='xllJr'><center id='xllJr'></center></pre></bdo></b><th id='xllJr'></th></span></q></dt></tr></i><div class="rcupsbg" id='xllJr'><tfoot id='xllJr'></tfoot><dl id='xllJr'><fieldset id='xllJr'></fieldset></dl></div>

                  本文介紹了PHP 驗證文件上傳的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我是一名 PHP 初學者,目前正在學習驗證文件上傳"部分.

                  I'm a PHP beginner and currently learning the "Validating the File Upload" part.

                  我做了一個包含以下代碼的 test.php 頁面:

                  I made a test.php page containing following code:

                  var_dump(@$_FILES['file']['type']);
                  

                  首先,我上傳了一張圖片img.gif"并返回:

                  First, I uploaded an image "img.gif" and it returned:

                  string 'image/gif' (length=9)
                  

                  然后,我將圖像的擴展名更改為.jpg"并返回:

                  Then, I changed the image's extension to ".jpg" and it returned:

                  string 'image/jpeg' (length=10)
                  

                  所以我意識到 $_FILES["file"]["type"] 只返回上傳的文件擴展名,但實際上并沒有檢查它是什么文件.

                  So I realized $_FILES["file"]["type"] only return the uploaded file extension but didn't actually check what file is it.

                  在這個頁面,http://www.w3schools.com/php/php_file_upload.asp,有是代碼:

                  In this page, http://www.w3schools.com/php/php_file_upload.asp, there is a code:

                  $allowedExts = array("gif", "jpeg", "jpg", "png");
                  $extension = end(explode(".", $_FILES["file"]["name"]));
                  if ((($_FILES["file"]["type"] == "image/gif")
                  || ($_FILES["file"]["type"] == "image/jpeg")
                  || ($_FILES["file"]["type"] == "image/jpg")
                  || ($_FILES["file"]["type"] == "image/png"))
                  && ($_FILES["file"]["size"] < 20000)
                  && in_array($extension, $allowedExts))
                  

                  我想知道為什么上面的代碼會檢查文件擴展名兩次?我從上面的代碼中刪除了一些,這是我的新代碼:

                  I'm wondering why above codes check file extension twice? I deleted some from above codes and this is my new code:

                  $allowedExts = array("gif", "jpeg", "jpg", "png");
                  $extension = end(explode(".", $_FILES["file"]["name"]));
                  if (($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts))
                  

                  我的代碼正確嗎?或者你有什么更好的方法來驗證上傳的文件是圖片嗎?

                  Is my code correct? Or do you have any better ways to validate the upload file is a image?

                  謝謝!

                  推薦答案

                  您應該將文件的 tmp_name* 傳遞給 getimagesize,它會給你圖片的大小和類型(如果是圖片).如果傳遞的參數是文件而不是圖像,則返回 false,這將允許您進行驗證.

                  You should pass the tmp_name of the file* to getimagesize, it will give you the size and type of the image (if it is an image). If the passed argument is a file but not an image it returns false, that will allow you to validate.

                  圖像驗證唯一可靠的方法是使用 GD 或 Imagick 制作它的副本 - getimagesize 很容易被黑.

                  The only reliable method of image validation is to make a copy of it using GD or Imagick - getimagesize can be easily hacked.

                  *:我的意思是上傳后創建的臨時文件.

                  *: I mean, the temporal file created after upload.

                  例如:

                  if ($_SERVER['REQUEST_METHOD'] === 'POST')
                  {
                      $file = $_FILES['file']['tmp_name'];
                      if (file_exists($file))
                      {
                          $imagesizedata = getimagesize($file);
                          if ($imagesizedata === FALSE)
                          {
                              //not image
                          }
                          else
                          {
                              //image
                              //use $imagesizedata to get extra info
                          }
                      }
                      else
                      {
                          //not file
                      }
                  }
                  

                  此代碼使用 file_exists 只是為了通用.如果沒有上傳文件,您將獲得 $_FILES['file']['size'] = 0, $_FILES['file']['tmp_name'] = ''$_FILES['file']['error'] = 4.另請參閱is_readable.有關錯誤值,請參閱 文件上傳錯誤解釋,位于 php.net.

                  This code uses file_exists just to be general. In case no file were uploaded you would get $_FILES['file']['size'] = 0, $_FILES['file']['tmp_name'] = '' and $_FILES['file']['error'] = 4. See also is_readable. For the error values see file upload errors explained at php.net.

                  這篇關于PHP 驗證文件上傳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                  <small id='2l8hg'></small><noframes id='2l8hg'>

                  <i id='2l8hg'><tr id='2l8hg'><dt id='2l8hg'><q id='2l8hg'><span id='2l8hg'><b id='2l8hg'><form id='2l8hg'><ins id='2l8hg'></ins><ul id='2l8hg'></ul><sub id='2l8hg'></sub></form><legend id='2l8hg'></legend><bdo id='2l8hg'><pre id='2l8hg'><center id='2l8hg'></center></pre></bdo></b><th id='2l8hg'></th></span></q></dt></tr></i><div class="tmuwnx3" id='2l8hg'><tfoot id='2l8hg'></tfoot><dl id='2l8hg'><fieldset id='2l8hg'></fieldset></dl></div>

                        <tfoot id='2l8hg'></tfoot>
                      1. <legend id='2l8hg'><style id='2l8hg'><dir id='2l8hg'><q id='2l8hg'></q></dir></style></legend>
                          <bdo id='2l8hg'></bdo><ul id='2l8hg'></ul>
                            <tbody id='2l8hg'></tbody>
                          1. 主站蜘蛛池模板: Win10系统下载_32位/64位系统/专业版/纯净版下载 | 天然鹅卵石滤料厂家-锰砂滤料-石英砂滤料-巩义东枫净水 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 智能家居全屋智能系统多少钱一套-小米全套价格、装修方案 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 周易算网-八字测算网 - 周易算网-宝宝起名取名测名字周易八字测算网 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 云南丰泰挖掘机修理厂-挖掘机维修,翻新,再制造的大型企业-云南丰泰工程机械维修有限公司 | 北京遮阳网-防尘盖土网-盖土草坪-迷彩网-防尘网生产厂家-京兴科技 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 宜兴紫砂壶知识分享 - 宜兴壶人| 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 周口风机|周风风机|河南省周口通用风机厂 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 深圳市人通智能科技有限公司| 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 伸缩节_伸缩器_传力接头_伸缩接头_巩义市联通管道厂 | 智慧养老_居家养老_社区养老_杰佳通| 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 灰板纸、灰底白、硬纸板等纸品生产商-金泊纸业 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 |