1.文件上传实现
(1)fileUpload.html文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2621114">
<input type="file" required name="upload_file">
<input type="submit" value="提交">
</form>
</body>
</html>
(2)upload.php文件
<?php
if (is_uploaded_file($_FILES['upload_file']['tmp_name'])){
$upfile = $_FILES['upload_file'];
//print_r($upfile);
$name = $upfile['name'];//获取文件名
$type = $upfile['type'];//文件类型
$size = $upfile['size'];//文件大小
$tmp_name = $upfile['tmp_name'];//服务器存储的临时名称
$error = $upfile['error'];//错误信息
/* switch ($type){
case 'image/png':
$ok=1;
break;
case 'image/jpg':
$ok=1;
break;
case 'image/jif':
$ok=1;
break;
case 'image/jpeg':
$ok=1;
break;
$ok &&
}*/
if ( $error == 0){//保证文件路径存在(这里不会新建);
move_uploaded_file($tmp_name,'./uploads/'.iconv("UTF-8", "gbk",$name));
echo '文件上传成功';
}else{
echo '文件上传失败';
}
}
2.文件下载实现
(1)fileDownLoad.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<a href="download.php?file=现金2223.txt">下载</a>
</body>
</html>
(2)download.php文件
<?php
//download.php
//$upfile ="uploads\\"." ".$_GET['file'];
//echo $upfile ;
//print_r($upfile);
//页面加载的时候就调用
downloadFile("uploads\\"."".$_GET['file'],"something.txt");
//$filePath是服务器的文件地址
//$saveAsFileName是用户指定的下载后的文件名
function downloadFile($filePath,$saveAsFileName){
// 清空缓冲区并关闭输出缓冲
ob_end_clean();
//r: 以只读方式打开,b: 强制使用二进制模式
$fileHandle=fopen($filePath,"rb");
if($fileHandle===false){
echo "没有发现文件: $filePath\n";
exit;
}
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: ".filesize($filePath));
Header("Content-Disposition: attachment; filename=\"$saveAsFileName\"");
while(!feof($fileHandle)) {
//从文件指针 handle 读取最多 length 个字节
echo fread($fileHandle, 32768);
}
fclose($fileHandle);
}
?>