首页 > 编程知识 正文

php上传文件到指定目录,php实现文件上传

时间:2023-05-03 16:12:56 阅读:265294 作者:4313

效果图如下:

首先我们需要一个index.html页面上传文件: <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>上传文件</title></head><body> <!-- action为空表示提交到当前页面 enctype编码格式 --> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="img" value="file"> <button type="submit" name="button">上传文件</button> </form></body></html> 接下来用后台index.php文件接收: <?php//empty():检查一个变量是否为空 / $_FILES数组:接收上传的文件if(empty($_FILES)){ //提交的文件是否为空 //没有文件则显示提交页面 include 'index.html';}else{ //把文件上传到./dir目录 $up_root = './dir/'; //该目录是否存在 || 不存在创建该目录 is_dir($up_root) || mkdir($up_root); //遍历上传的文件 foreach($_FILES as $item){ //error = 0 表示文件正常可以上传 if($item['error'] === 0){ //读取文件信息 $content = file_get_contents($item['tmp_name'],'r'); //获取一个带前缀 基于当前时间微妙数唯一的ID $fid = uniqid(); //文件名和后缀以'.'分开 得到一个$suffix数组 $suffix = explode('.',$item['name']); //删除数组最后一位并返回 获得后缀名 $suffix = array_pop($suffix); $suffix && ($suffix = '.'.$suffix); //move_uploaded_file 将上传的文件移动到新位置 move_uploaded_file($item['tmp_name'],$up_root.$fid.$suffix); echo '文件'.$item['tmp_name'].'已上传到'.$up_root.'文件夹,文件名是:'.$fid.$suffix; }else{ echo "文件异常,上传失败"; }; };};

把代码放到服务器即可实现上传文件到服务器!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。