一、文件上传
前台页面:
<span><!</span><span>DOCTYPE html</span><span>></span> <span><</span><span>html</span><span>></span> <span><</span><span>head</span><span>></span> <span><</span><span>meta </span><span>charset</span><span>="utf-8"</span><span>></span> <span><</span><span>title</span><span>></span>文件上传<span></</span><span>title</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body</span><span>></span> <span><</span><span>div</span><span>></span> <span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="upload.php"</span><span> enctype</span><span>="multipart/form-data"</span> <span>></span><span> 用户名</span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="username"</span><span>></</span><span>input</span><span>></span> <span><</span><span>br</span><span>></span><span> 文件简单介绍</span><span><</span><span>br</span><span>><</span><span>textarea </span><span>name</span><span>="fileintro"</span><span> rows</span><span>="8"</span><span> cols</span><span>="50"</span><span>></</span><span>textarea</span><span>></span> <span><</span><span>br</span><span>></span>选择要上传的文件<span><</span><span>br</span><span>><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="myfile"</span><span>></</span><span>input</span><span>><</span><span>br</span><span>></span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="上传文件"</span><span>></</span><span>input</span><span>></span> <span></</span><span>form</span><span>></span> <span></</span><span>div</span><span>></span> <span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
后台处理:
<?<span>php </span><span>//</span><span>接收提交页面传送的相关信息</span> <span>$name</span> = <span>$_POST</span>['username'<span>]; </span><span>$intro</span> = <span>$_POST</span>['fileintro'<span>]; </span><span>//</span><span>$_FILES中存放着文件的相关信息 // echo "<pre class="brush:php;toolbar:false">"; // print_r($_FILES); // echo "
二、文件下载:
单文件下载:
<?<span>php </span><span>function</span> down_file(<span>$file_name</span>,<span>$file_path</span><span>){ </span><span>//</span><span>$file_name = iconv("uft-8","gb2312",$file_name); //如果文件名是中文,需要对中文名称转码gb2312 //要下载的文件读取到服务器的内存中 //服务器返回文件数据给浏览器 //浏览器将文件写入用户指定的位置 //1.判断文件是否存在</span> <span>if</span>(!<span>file_exists</span>(<span>$file_name</span><span>)){ </span><span>echo</span> "111"<span>; </span><span>return</span><span> ; } </span><span>$fp</span> = <span>fopen</span>(<span>$file_name</span>,"r"<span>); </span><span>$file_size</span> = <span>filesize</span>(<span>$file_name</span><span>); </span><span>//</span><span>获取文件大小 //可通过file_size限制浏览器下载文件大小。 //返回的文件</span> <span>header</span>("Content-type:application/octet-stream"<span>); </span><span>//</span><span>按字节大小返回</span> <span>header</span>("Accept-Ranges:bytes"<span>); </span><span>//</span><span>返回文件大小</span> <span>header</span>("Accept-Length:<span>$file_size</span>"<span>); </span><span>//</span><span>客户端弹出对话框,对应的文件名</span> <span>header</span>("Content-Disposition:attachment;filename=".<span>$file_name</span><span>); </span><span>$buffer</span> = 1024<span>; </span><span>//</span><span>定义缓冲区 //为了下载的安全,最好使用文件字节读取计数器</span> <span>$file_count</span> = 0<span>; </span><span>//</span><span>feof用于判断文件是否读取到文档尾</span> <span>while</span>(!<span>feof</span>(<span>$fp</span>) && (<span>$file_size</span>-<span>$file_count</span>>0<span>)){ </span><span>$file_data</span> = <span>fread</span>(<span>$fp</span>,<span>$buffer</span><span>); </span><span>//</span><span>统计读了多少个字节</span> <span>$file_count</span>+<span>$buffer</span><span>; </span><span>echo</span> <span>$file_data</span><span>; </span><span>//</span><span>把部分数据会送给浏览器</span> <span> } </span><span>fclose</span>(<span>$fp</span><span>); </span><span>//</span><span>关闭文件</span> <span> } </span>?>
多个文件下载:
DownList.php:
<a href="downprocess.php?filename=1.jpg">down</a><img src="1.jpg" style="max-width:90%" height="50px" alt="PHP文件上传及下载源码," ><br><br> <a href="downprocess.php?filename=2.jpg">down</a><img src="2.jpg" style="max-width:90%" height="50px" alt="PHP文件上传及下载源码," ><br><br> <a href="downprocess.php?filename=3.jpg">down</a><img src="3.jpg" style="max-width:90%" height="50px" alt="PHP文件上传及下载源码," ><br><br> <a href="downprocess.php?filename=4.jpg">down</a><img src="4.jpg" style="max-width:90%" height="50px" alt="PHP文件上传及下载源码," ><br><br>
downprocess.php:
<?<span>php </span><span>$filename</span> = <span>$_REQUEST</span>['filename'<span>]; </span><span>function</span> down_file(<span>$file_name</span><span>){ </span><span>if</span>(!<span>file_exists</span>(<span>$file_name</span><span>)){ </span><span>echo</span> "111"<span>; </span><span>return</span><span> ; } </span><span>$fp</span> = <span>fopen</span>(<span>$file_name</span>,"r"<span>); </span><span>$file_size</span> = <span>filesize</span>(<span>$file_name</span><span>); </span><span>header</span>("Content-type:application/octet-stream"<span>); </span><span>//</span><span>按字节大小返回</span> <span>header</span>("Accept-Ranges:bytes"<span>); </span><span>//</span><span>返回文件大小</span> <span>header</span>("Accept-Length:<span>$file_size</span>"<span>); </span><span>//</span><span>客户端弹出对话框,对应的文件名</span> <span>header</span>("Content-Disposition:attachment;filename=".<span>$file_name</span><span>); </span><span>$buffer</span> = 1024<span>; </span><span>//</span><span>定义缓冲区</span> <span>$file_count</span> = 0<span>; </span><span>//</span><span>feof用于判断文件是否读取到文档尾</span> <span>while</span>(!<span>feof</span>(<span>$fp</span>) && (<span>$file_size</span>-<span>$file_count</span>>0<span>)){ </span><span>$file_data</span> = <span>fread</span>(<span>$fp</span>,<span>$buffer</span><span>); </span><span>//</span><span>统计读了多少个字节</span> <span>$file_count</span>+<span>$buffer</span><span>; </span><span>echo</span> <span>$file_data</span><span>; </span><span>//</span><span>把部分数据会送给浏览器</span> <span> } </span><span>fclose</span>(<span>$fp</span><span>); } down_file(</span><span>$filename</span><span>); </span>?>