php实现简单文件下载的方法

时间:2025-10-08 02:10:37 php语言 我要投稿

php实现简单文件下载的方法

  介绍的php文件下载代码,只是简单实现了一张图片的下载功能,还不完善,最好是封装到一个类里,或是采用函数调用。感兴趣的朋友可以在此基础上加以完善!

  php文件下载代码如下:

  <?php

  $file_name = "2.jpg";/pic/p>

  define("SPATH","/php/image/");/pic/p>

  $file_sub_path = $_SERVER['DOCUMENT_ROOT'];/pic/p>

  $file_path = $file_sub_path.SPATH.$file_name;/pic/p>

  /pic/p>

  if(!file_exists($file_path)){

  echo "该文件不存在";

  return;

  }

  $fp = fopen($file_path,"r");/pic/p>

  $file_size = filesize($file_path);/pic/p>

  /*

  *下载文件需要用到的header

  */

  header("Content-type:application/octet-stream");

  header("Accept-Ranges:bytes");

  header("Accept-Length:".$file_size);

  header("Content-Disposition:attachment;filename=".$file_name);

  $buffer=1024;

  $file_count=0;

  /pic/p>

  while(!feof($fp) && $file_count<$file_size){

  $file_con = fread($fp,$buffer);

  $file_count += $buffer;

  echo $file_con;/pic/p>

  }

  fclose($fp);

  ?>

【php实现简单文件下载的方法】相关文章:

php实现编辑和保存文件的方法02-27

PHP中读取大文件实现方法10-09

PHP中读取大文件实现方法详解09-04

PHP下载保存文件保存到本地的方法02-16

php readfile下载大文件失败的解决方法01-20

PHP实现文件上传和多文件上传01-19

php实现通过ftp上传文件06-28

PHP文件锁与进程锁的实现12-12

jQuery Mobile + PHP实现文件上传03-20

  • 相关推荐