php文件上傳的記錄
阿新 • • 發佈:2018-06-04
utf-8 sub clu move pat meta .html true creat
文件上傳案例
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上傳簡述</title> </head> <body> <form action="a.php" method="post" enctype="multipart/form-data"> <p> <input type="file" name="pic"> </p> <p> <input type="submit" name="submit" value="提交"> </p> </form> </body> </html>
處理上傳文件a.php
<?php include(‘./func.php‘); if(is_file($_FILES[‘pic‘][‘tmp_name‘]) && $_FILES[‘pic‘][‘error‘] == 0){ $des = createDir().‘/‘.randStr().getExt($_FILES[‘pic‘][‘name‘]); $data = move_uploaded_file($_FILES[‘pic‘][‘tmp_name‘],$des); }else{ echo ‘上傳文件為空‘; } ?>
涉及的自定義函數
<?php /** * 按日期創建存儲目錄 */ function createDir(){ $path = ‘./upload/‘.date(‘Y/md‘); if(is_dir($path) || mkdir($path,0777,true)){ return $path; }else{ return false; } } /** * 生成隨機字符串 */ function randStr($length=6){ $str = str_shuffle(‘ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789‘);$str = substr($str,0,$length); return $str; } /** * 獲取文件後綴 * @param str $name 文件名 */ function getExt($name){ return strrchr($name,‘.‘); } ?>
相關介紹:
刪除指定文件 unlink(‘index.php‘);
is_file() is_dir() file_exists() 的區別
is_file() 判斷是否有該文件
is_dir() 判斷是否存在該目錄
file_exists 判斷是否存在該文件和該目錄
php文件上傳的記錄