一個簡單的PHP文件上傳示例程序
來源:程序員人生 發(fā)布時間:2014-06-20 03:14:36 閱讀次數(shù):3360次
本文章來給各位初學(xué)者來介紹一個簡單的PHP文件上傳示例程序代碼,有需要學(xué)習(xí)的朋友可進入?yún)⒖紖⒖?
實例代碼如下:
- <?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('創(chuàng)建目錄錯誤'));
- }
-
-
- $filename = time().'_'.$file['name'];
-
-
- if( !copy($file['tmp_name'],$filePath.$filename) ){
- exit(alert('上傳文件出錯,請稍候重試'));
- }
-
-
- unlink($file['tmp_name']);
-
-
- echo alert('恭喜,上傳文件['.$filename.']成功!');
- ?>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈