文件上传案例
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文件上传的记录
原文地址:https://www.cnblogs.com/syx0610/p/9133277.html