php檔案上傳之單檔案上傳

來源:互聯網
上載者:User
為了簡單一些,php檔案跟form表單寫在了一個檔案裡.

php單檔案上傳---->

 1  2  3  4      5  6  7 12 13 14151617 php18if(!empty($_FILES)){19header('content-type:text/html;charset=utf-8');20$fileInfo=$_FILES['myfile'];21print_r($_FILES);22//如果上傳出錯則退出並列印錯誤資訊23if($fileInfo['error']>0){24switch($fileInfo['error']){25case 1:26$msg_error='上傳檔案超過了php設定檔中UPLOAD_MAX_FILESIZE選項的值';27break;28case 2:29$msg_error='超過了表單MAX_FILE_SIZE限制的大小';30break;31case 3:32$msg_error='檔案部分上傳';33break;34case 4:35$msg_error='沒有檔案上傳';36break;37case 6:38$msg_error='沒有找到臨時目錄';39break;40case 7:41case 8:42$msg_error='系統錯誤';43break;44            }45exit($msg_error);46        }47$filename=$fileInfo['name'];48//擷取檔案的副檔名49$ext=strtolower(substr($filename,strrpos($filename,'.')+1));50//定義可允許上傳的副檔名51$allowExt=array('txt','html','png','gif','jpeg');52//檢測上傳檔案的類型53if(!in_array($ext,$allowExt)){54exit('上傳檔案類型錯誤');55        }565758//檢測檔案的大小59$maxSize=2097152;60if($fileInfo['size']>$maxSize){61exit('上傳檔案過大');62        }6364//檢測是否為HTTP POST方式上傳上來的65if(!is_uploaded_file($fileInfo['tmp_name'])){66exit('檔案不是通過HTTP POST方式提交上來的');67        }6869//確保檔案名稱字唯一,防止同名檔案被覆蓋70$uniqName=md5(uniqid(microtime(true),true)).'.'.$ext;7172//定義儲存在哪個檔案夾下,如果沒有該檔案夾則建立73$path='uploads';74if(!file_exists($path)){75mkdir($path,0777,true);76chmod($path,0777);77        }78$destination=$path.'/'.$uniqName;7980//移動檔案至要儲存的目錄81if(! @move_uploaded_file($fileInfo['tmp_name'],$destination)){82exit('檔案上傳失敗');83        }8485echo '上傳成功';8687    }88 ?>

以上就介紹了php檔案上傳之單檔案上傳,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.