一個簡單的PHP文件上傳示例程序
來源:程序員人生 發布時間:2014-06-20 03:14:36 閱讀次數:3365次
本文章來給各位初學者來介紹一個簡單的PHP文件上傳示例程序代碼,有需要學習的朋友可進入參考參考.
實例代碼如下:
- <?php
-
- function alert($msg){
- return '<script type="text/javascript">alert("'.$msg.'");window.history.back(-1);</script>';
- }
-
-
- $allowType = array('image/jpeg','image/gif','image/jpg');
-
-
- $filePath = './uploadFileDir/';
-
-
- $file = $_FILES['file'];
-
-
- if( $file['error'] !== 0 ){
- exit(alert('文件上傳錯誤'));
- }
-
-
- if( $file['size'] > 102400 ){
- exit(alert('文件過大'));
- }
-
-
- if( !in_array(mime_content_type($file['tmp_name']),$allowType) ){
- exit(alert('文件類型錯誤'));
- }
-
-
- if( !file_exists($filePath) && !mkdir($filePath,0777,true) ){
- exit(alert('創建目錄錯誤'));
- }
-
-
- $filename = time().'_'.$file['name'];
-
-
- if( !copy($file['tmp_name'],$filePath.$filename) ){
- exit(alert('上傳文件出錯,請稍候重試'));
- }
-
-
- unlink($file['tmp_name']);
-
-
- echo alert('恭喜,上傳文件['.$filename.']成功!');
- ?>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈