① php实现下载txt文件,header到底怎么写,网上的所有方法都试过了,有没有实现的,我用的360浏览器
$str = 'txt内容';$filename = '文档名称';header("Content-type: text/plain"); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=".$filename); header("Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header("Pragma: no-cache" ); header("Expires: 0" ); exit($str);
② php利用表头来下载文件,利用header()类下载文件
手册上不都有吗:
<?php$file='monkey.gif';if(file_exists($file)){header('Content-Description:FileTransfer');header('Content-Type:application/octet-stream');header('Content-Disposition:attachment;filename='.basename($file));header('Content-Transfer-Encoding:binary');header('Expires:0');header('Cache-Control:must-revalidate');header('Pragma:public');header('Content-Length:'.filesize($file));ob_clean();flush();readfile($file);exit;}?>
③ php header("Content-Length: 下载获取文件长度的问题
可能是路径问题吧,$_SERVER['DOCUMENT_ROOT']这是获得根目录,跟你的文件名连在一起试试
④ php下载函数,在ie上不能下载大于2G的文件,当文件大于2G的时候,下载下的压缩文件是0KB的,求大神指点。
/** * 发送文件 * * @author: legend([email protected]) * @link: http://www.ugia.cn/?p=109 * @description: send file to client * @version: 1.0 * * @param string $fileName 文件名称或路径 * @param string $fancyName 自定义的文件名,为空则使用filename * @param boolean $forceDownload 是否强制下载 * @param integer $speedLimit 速度限制,单位为字节,0为不限制,不支持windows服务器 * @param string $$contentType 文件类型,默认为application/octet-stream * * @return boolean */function sendFile($fileName, $fancyName = '', $forceDownload = true, $speedLimit = 0, $contentType = ''){ if (!is_readable($fileName)) { header("HTTP/1.1 404 Not Found"); return false; } $fileStat = stat($fileName); $lastModified = $fileStat['mtime']; $md5 = md5($fileStat['mtime'] .'='. $fileStat['ino'] .'='. $fileStat['size']); $etag = '"' . $md5 . '-' . crc32($md5) . '"'; header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT'); header("ETag: $etag"); if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified) { header("HTTP/1.1 304 Not Modified"); return true; } if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $lastModified) { header("HTTP/1.1 304 Not Modified"); return true; } if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { header("HTTP/1.1 304 Not Modified"); return true; } if ($fancyName == '') { $fancyName = basename($fileName); } if ($contentType == '') { $contentType = 'application/octet-stream'; } $fileSize = $fileStat['size']; $contentLength = $fileSize; $isPartial = false; if (isset($_SERVER['HTTP_RANGE'])) { if (preg_match('/^bytes=(d*)-(d*)$/', $_SERVER['HTTP_RANGE'], $matches)) { $startPos = $matches[1]; $endPos = $matches[2]; if ($startPos == '' && $endPos == '') { return false; } if ($startPos == '') { $startPos = $fileSize – $endPos; $endPos = $fileSize – 1; } else if ($endPos == '') { $endPos = $fileSize – 1; } $startPos = $startPos < 0 ? 0 : $startPos; $endPos = $endPos > $fileSize – 1 ? $fileSize – 1 : $endPos; $length = $endPos – $startPos + 1; if ($length < 0) { return false; } $contentLength = $length; $isPartial = true; } } // send headers if ($isPartial) { header('HTTP/1.1 206 Partial Content'); header("Content-Range: bytes $startPos-$endPos/$fileSize"); } else { header("HTTP/1.1 200 OK"); $startPos = 0; $endPos = $contentLength – 1; } header('Pragma: cache'); header('Cache-Control: public, must-revalidate, max-age=0'); header('Accept-Ranges: bytes'); header('Content-type: ' . $contentType); header('Content-Length: ' . $contentLength); if ($forceDownload) { header('Content-Disposition: attachment; filename="' . rawurlencode($fancyName). '"'); } header("Content-Transfer-Encoding: binary"); $bufferSize = 2048; if ($speedLimit != 0) { $packetTime = floor($bufferSize * 1000000 / $speedLimit); } $bytesSent = 0; $fp = fopen($fileName, "rb"); fseek($fp, $startPos); //fpassthru($fp); while ($bytesSent < $contentLength && !feof($fp) && connection_status() == 0 ) { if ($speedLimit != 0) { list($usec, $sec) = explode(" ", microtime()); $outputTimeStart = ((float)$usec + (float)$sec); } $readBufferSize = $contentLength – $bytesSent < $bufferSize ? $contentLength – $bytesSent : $bufferSize; $buffer = fread($fp, $readBufferSize); echo $buffer; ob_flush(); flush(); $bytesSent += $readBufferSize; if ($speedLimit != 0) { list($usec, $sec) = explode(" ", microtime()); $outputTimeEnd = ((float)$usec + (float)$sec); $useTime = ((float) $outputTimeEnd – (float) $outputTimeStart) * 1000000; $sleepTime = round($packetTime – $useTime); if ($sleepTime > 0) { usleep($sleepTime); } } } return true;}
⑤ 用php header() 实现文件下载,出现问题
$file=fopen('文件地址',"r");$size=filesize('文件地址');header("Content-Type:application/octet-stream");header("Accept-Ranges:bytes");header("Accept-Length:".$size);header("Content-Disposition:attachment;filename=文件名称");echofread($file,$size);fclose($file);
⑥ php header方法写文件下载,我用图片测试,下载后文件比原来大,为什么呢,
$buf出了问题.应该先剩余大小 = 文件大小在while循环中. 条件为 剩余大小>0剩余大小 = 剩余大小 – 1024$buf = 剩余大小>1024 ? 1024 : 剩余大小.————文件在不同文件系统格式中大小也不相同.
⑦ PHP如何用header设置文件下载头!感谢~
$path="http://*****/wlyz/1.txt";header("Content-Type:application/force-download");header("Content-Disposition:attachment;filename=".basename($path));readfile($path);
这段代码可以新建一个PHP文件,放入以上代码,然后用一个<a>标签链接到这版个php文件,带上参权数,传入需要下载的文件的路径。
⑧ php header 下载zip文件的问题
<?phpinclude("class.php");date_default_timezone_set("PRC");//设置时区为中国$filetype_array = array("as"=>"Flash ActionScript File","bmp"=>"BMP图像","doc"=>"Microsoft Word文档","rar"=>"WinRAR 压缩文档","ppt"=>"Microsoft PowerPoint 演示文稿","txt"=>"文本文档","xls"=>"Microsoft Excel 工作表","gif"=>"GIF图像","jpg"=>"JPEG图像","jpeg"=>"JPEG图像","png"=>"PNG图像","html"=>"HTML Document","htm"=>"HTML Document","mp3"=>"MP3 格式声音","chm"=>"已编译的HTML帮助文件","fla"=>"Flash Document","mdb"=>"Microsoft Access 应用程序","pdf"=>"PDF 文件","swf"=>"Flash Movie","xml"=>"XML 文档","zip"=>"WinRAR ZIP 压缩文件","asf"=>"Windows 音频/视频文件","wmv"=>"Windows Media 音频/视频文件","avi"=>"视频剪辑","mpeg"=>"电影剪辑","exe"=>"应用程序","gz"=>"WinRAR 压缩文件","tar"=>"WinRAR 压缩文件","tiff"=>"TIFF 文件");//以上为判断函数$="./images";$nba=opendir($);$i=1;echo "<table border=1px color:red>";echo "<tr><td>I D</td><td>文件名称</td><td>大小</td><td>最后修改时间</td><td>类型</td><td>下载</td></tr>";while ($wnba=readdir($nba)){ if($wnba != "." && $wnba != "..") { $path=$."/".$wnba; $dir=new dir($path); $str[]= "<tr>". "<td>".$i++."</td>". "<td>".$wnba."</td>". "<td>".$dir->getsize()."</td>". "<td>".$dir->gettime()."</td>". "<td>".$dir->gettype()."</td>". "<td><a href='$path'><img src=".$dir->getIcon()."></a></td>". "</tr>\n"; } $lines=15;//每页15行 if( " "==$cur_page) $cur_page=1;//当前页 $page=ceil(count($str)/$lines);//页数 echo $page; for($i=($cur_page-1)*$lines ; $i <$cur_page*$lines ; $i++){//分段读出数组 echo "$str[$i] "; } }echo "</table>";closedir($nba); ?><table width=100%> <tr> <td align=center> <? for ($i=1;$i <=$page;$i++){ $i==$cur_page ? $a= " <b> ".$i. " </b> " : $a= " <a href= ".wjclass1.php. "?cur_page=$i> ".$i. " </a> "; echo "$a "; } ?> </td> </tr> </table> </td> </tr> </table>
⑨ PHP-大数据量怎么处理优化
大数据的话可以进行以下操作:
减少对数据库的读取,也就是减少调用数据库,
进行数据缓存,
利用数据库的自身优化技术,如索引等
精确查询条件,有利于提高查找速度
⑩ php用 header 下载给文件重命名的时候 长度限制怎么设置啊
publicfunctiondownloads(){$file="./Ludian_Disaster_Relief_Map_Operation_Guide.pdf";if(file_exists($file)){header('Content-Description:FileTransfer');header('Content-Type:application/octet-stream');header('Content-Disposition:attachment;filename='.basename($file));header('Content-Transfer-Encoding:binary');header('Expires:0');header('Cache-Control:must-revalidate,post-check=0,pre-check=0');header('Pragma:public');header('Content-Length:'.filesize($file));ob_clean();flush();readfile($file);}else{$this->error("文件不存在");}}