$src=$_FILES['n-img']['tmp_name']; $file=$_FILES['n-img']['name']; $a=explode('.',$file); $ext=array_pop($a); $rand=time().mt_rand().'.'.$ext; $dst="uploadimg/{$rand}"; if($_FILES['n-img']['error']===0){ if(move_uploaded_file($src,$dst)){ echo "头像上传成功"; }else{ echo "头像未上传成功"; } }
$file=$_FILES['myfile']; if(is_uploaded_file($file['tmp_name'])){ $rand=rand(1,1000); $floattime=microtime(); $str=substr($file['name'],-4,4); $path='uploads/'.($rand+$floattime).$str; if(move_uploaded_file($file['tmp_name'],$path)){ echo "上传成功"; } }
文件的下载
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>下载功能</title> </head> <body> <a href="1.php?filename=2.jpg">下载图片</a> </body> </html>
<?php $filename=$_GET['filename']; header('content-disposition:attachment;filename='.basename($filename));//函数返回路径中的文件名部分 header('content-length:'.filesize($filename));//返回指定文件的大小 readfile($filename);