search

THinkPHP中文件下载

 THinkPHP1.5中文件的下载 用到的系统类库文件是Http.class.php,位于ThinkPHP\Lib\ORG\Net目录下,类名Http,其中有静态方法static function download ($filename, $showname=”,$content=”,$expire=180);/     @param string $filename 下载文件名(完整路径加文件的保存名字)* @param string $showname 下载显示的文件名(想要显示的名字或者从数据库中读出的原来带中文的名字);* @param string $content  下载的内容(默认为空,此时下载的文件就是原文件)。* @param integer $expire  下载内容浏览器缓存时间 ,默认为空时为180秒。*/因为PHP保存文件名不支持中文,所以通常中文文件名保存到服务器上时换成成英文名或者生成随机名字。下载时可以利用此方法回复原文件名。应用举例:下载时显示文件原名/* 假设数据库里文件信息存储表为file(id,truename,savenane,user,size).文件存在于网站项目目录下的uploads文件夹里,本网站项目名为bm,其绝对路径为:H:\AppServ\www\bm\uploads\(  H:\AppServ\www\为文档根目录)此时该目录下有一文件123456789.doc,(savename),原文件名为“读后感.doc”,即truename,大小为2MB.那么要下载时服务器端得程序为:class FileAction extends Action{public function download(){$uploadpath=’H:\AppServ\www\bm\uploads\’;//设置文件上传路径,服务器上的绝对路径$id=$_GET['id'];//GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。if($id==”) //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。{$this->redirect(‘index’,'Index’,”,APP_NAME,”,1);}$file=D(‘File’);//利用与表file对应的数据模型类FileModel来建立数据对象。$result= $file->find($id);//根据id查询到文件信息if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面。或可做其他处理{$this->redirect(‘index’,'Index’,”,APP_NAME,”,1);}$savename=$file->savename;//文件保存名$showname=$file->truename;//文件原名$filename=$uploadpath.$savename;//完整文件名(路径加名字)import(‘ORG.Net.Http’);Http::download($filename,$showname);}}然后在该文件下载的HTML模板里要下载该文件的地方加一个下载链接,调用File模块的download方法即可。记得传参数id .如本例中:<table><tr><td>读后感.doc</td><td>sunmoon</td><td><a href=’__APP__/File/download/id/{$id}’>下载</a></td><!–其中{$id}是模板变量,代表要下载的文件在数据库中的保存id.–></tr></table> 注:IE浏览器的下载文件名编码只有gb2312才能显示,若是不然,要不就是文件名乱码,要不就是找不到文件而无法下载。针对此种情况,我对原来的download()方法进行了一些调整,经过测试发现IE、傲游、firefox均可正常下载。    /**     +----------------------     * 下载文件     * 可以指定下载显示的文件名,并自动发送相应的Header信息     * 如果指定了content参数,则下载该参数的内容     +----------------------     * @static     * @access public     +----------------------     * @param string $filename 下载文件名     * @param string $showname 下载显示的文件名     * @param string $content  下载的内容     * @param integer $expire  下载内容浏览器缓存时间     +----------------------     * @return void     +----------------------     * @throws ThinkExecption     +----------------------     */    static public function download ($filename, $showname='',$content='',$expire=180) {  if(file_exists($filename)){   $length = filesize($filename);  }elseif(is_file(UPLOAD_PATH.$filename)){   $filename = UPLOAD_PATH.$filename;   $length = filesize($filename);  }elseif($content != ''){   $length = strlen($content);   }else {   throw_exception($filename.L('下载文件不存在!'));  }  if(empty($showname)){   $showname = $filename;  }  $showname = basename($showname);  if(empty($filename)){   $type = mime_content_type($filename);  }else{   $type = "application/octet-stream";  }  //发送Http Header信息 开始下载  header("content-type:text/html; charset=utf-8");  header("Pragma: public");  header("Cache-control: max-age=".$expire);  //header('Cache-Control: no-store, no-cache, must-revalidate');  header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");  header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");  //下面一行就是改动的地方,即用iconv("UTF-8","GB2312//TRANSLIT",$showname)系统函数转换编码为gb2312  header("Content-Disposition: attachment; filename=". iconv("UTF-8","GB2312",$showname)); header("Content-Length: ".$length);  header("Content-type: ".$type);  header('Content-Encoding: none');  header("Content-Transfer-Encoding: binary" );  if($content == '' ) {   readfile($filename);  }  else { echo($content); }  exit();      }注:iconv为php系统函数库,但需要安装。若是服务器还没有这个模块安装,则需将iconv.dll下载下来后复制到windows/system32/下面,同时在php安装文件夹得ext文件夹里也复制一份。然后在php.ini文件中将extension=php_iconv.dll前的”;”去掉,没有的话就加上extension=php_iconv.dll。然后重启服务器即可。

?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
鸿蒙原生应用随机诗词鸿蒙原生应用随机诗词Feb 19, 2024 pm 01:36 PM

想了解更多关于开源的内容,请访问:51CTO鸿蒙开发者社区https://ost.51cto.com运行环境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、创建应用点击File->newFile->CreateProgect。选择模版:【OpenHarmony】EmptyAbility:填写项目名,shici,应用包名com.nut.shici,应用存储位置XXX(不要有中文,特殊字符,空格)。CompileSDK10,Model:Stage。Device

使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

使用java的File.renameTo()函数重命名文件使用java的File.renameTo()函数重命名文件Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函数重命名文件在Java编程中,我们经常需要对文件进行重命名的操作。Java提供了File类来处理文件操作,其中的renameTo()函数可以方便地重命名文件。本文将介绍如何使用Java的File.renameTo()函数来重命名文件,并提供相应的代码示例。File.renameTo()函数是File类的一个方法,

linux的header是什么意思linux的header是什么意思Jul 18, 2023 pm 03:34 PM

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

SpringBoot怎么通过Feign调用传递Header中参数SpringBoot怎么通过Feign调用传递Header中参数May 16, 2023 pm 08:38 PM

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

使用java的File.getParentFile()函数获取文件的父目录使用java的File.getParentFile()函数获取文件的父目录Jul 27, 2023 am 11:45 AM

使用java的File.getParentFile()函数获取文件的父目录在Java编程中,我们经常需要操作文件和文件夹。当我们需要获取文件的父目录时,可以使用Java提供的File.getParentFile()函数来完成。本文将介绍如何使用这个函数并提供代码示例。Java中的File类是用于操作文件和文件夹的主要类。它提供了许多方法来获取和操作文件的属性

使用java的File.getParent()函数获取文件的父路径使用java的File.getParent()函数获取文件的父路径Jul 24, 2023 pm 01:40 PM

使用java的File.getParent()函数获取文件的父路径在Java编程中,我们经常需要操作文件和文件夹。有时候,我们需要获取一个文件的父路径,也就是该文件所在文件夹的路径。Java的File类提供了getParent()方法用于获取文件或文件夹的父路径。File类是Java对文件和文件夹的抽象表示,它提供了一系列操作文件和文件夹的方法。其中,get

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!