PHP实现文件上传,下载(并且中文不乱码)

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);
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值