1. 程式人生 > >php 上傳文件

php 上傳文件

出了 bsp echo mic index.php efault error col turn

php 上傳文件


html表單,需要記住form的enctype屬性:

<form action=‘index.php‘ method="POST" enctype="multipart/form-data">
<input type=‘file‘ name=‘file‘ />
<input type=‘submit‘ value=‘提交‘ />
</form>

php代碼:

function uploadFile($fileInfo,$path=‘./upload/‘){
    //判斷錯誤號
    if($fileInfo[‘error‘] == UPLOAD_ERR_OK){
        
//文件是否是通過HTTP POST上傳的 if(is_uploaded_file($fileInfo[‘tmp_name‘])){ $ext = strtolower(pathinfo($fileInfo[‘name‘],PATHINFO_EXTENSION)); $allow_Ext = array(‘jpg‘,‘jpeg‘,‘png‘); if(in_array($ext, $allow_Ext)){ $length = 6; $newFileName
= substr(md5(uniqid(microtime(true),true)),0,$length); $savePath = $path . $newFileName .‘.‘.$ext; if(move_uploaded_file($fileInfo[‘tmp_name‘], $savePath)){ return ‘success‘; }else{ return ‘file upload fail‘; } }
else{ return ‘file type not allow‘; } }else{ return ‘文件不是通過HTTP POST上傳的‘; } }else{ switch($fileInfo[‘error‘]){ case 1: $errMsg = ‘超出了配置文件的大小‘;//PHP.ini的upload_max_filesize的值 break; case 2: $errMsg = ‘超過了表單允許接收數據的大小‘;//表單中MAX_FILE_SIZE的值 break; case 3: $errMsg = ‘文件部分被上傳‘; break; case 4: $errMsg = ‘沒有文件被上傳‘; break; case 6: $errMsg = ‘找不到臨時文件夾‘; break; case 7: $errMsg = ‘文件寫入失敗‘; break; default: $errMsg = "Unknown upload error"; break; } return $errMsg; } } //var_dump($_FILES); if(isset($_FILES[‘file‘])){ echo uploadFile($_FILES[‘file‘]); }

php 上傳文件