PHP 文件的上传与下载

来源:互联网 发布:野人升级数据 编辑:程序博客网 时间:2024/05/16 09:56

 (一) 文件的下载

$file='drupalmo.srt';if (file_exists($file)) {header('Content-Description: File Transfer');header('Content-Type: application/srt');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);exit;}

其中 Content-type可以为如下的格式,要注意:文件名和内容要保持关联,也就是如果内容是pdf,那么文件的扩展名也需要时pdf的,否则无法下载。application/pdf指用pdf程序打开。

         case "pdf": $ctype="application/pdf"; break;          case "exe": $ctype="application/octet-stream"; break;          case "zip": $ctype="application/zip"; break;          case "doc": $ctype="application/msword"; break;          case "xls": $ctype="application/vnd.ms-excel"; break;          case "ppt": $ctype="application/vnd.ms-powerpoint"; break;          case "gif": $ctype="image/gif"; break;          case "png": $ctype="image/png"; break;          case "jpeg":          case "jpg": $ctype="image/jpg"; break;          default: $ctype="application/force-download";