PHP文件上传及下载源码,_PHP教程

WBOY
Freigeben: 2016-07-12 08:51:18
Original
881 Leute haben es durchsucht

PHP文件上传及下载源码,

  一、文件上传

  前台页面:

<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>
Nach dem Login kopieren

  后台处理:

<?<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 "
Nach dem Login kopieren
"; //可以在这里对文件大小进行限制 /*$filesize = $_FILES['myfile']['size']; if($filesize>2*1024*1024){ echo "文件过大,不能上传"; exit(); }*/ //可以对文件类型进行限制 /*$filetype = $_FILES['myfile']['type']; if($filetype!='image/jpg'&&$filetype!='application/pdf'){ echo "文件类型只能是jpg和pdf"; exit(); }*/ if (is_uploaded_file($_FILES['myfile']['tmp_name'])) { //把文件转存到你希望存放的目录 $uploaded = $_FILES['myfile']['tmp_name']; //每个用户动态创建一个文件夹 $userpath = $_SERVER['DOCUMENT_ROOT']."/up/".$name; //判断该用户是否已经有文件夹 if(!file_exists($userpath)){ mkdir($userpath); } //防止同一用户上传同名文件,可在文件中添加时间戳。 //$moveto = $userpath."/".time().$_FILES['myfile']['name']; //或者对文件名进行修改,但是需要使用字符串处理截得文件后缀名 $truename = $_FILES['myfile']['name']; $moveto = $userpath."/".time().substr($truename,strrpos($truename,".")); if(move_uploaded_file($uploaded,iconv("utf-8", "gb2312", $moveto))){ echo "上传文件".$_FILES['myfile']['name']."成功"; }else{ echo "上传文件".$_FILES['myfile']['name']."失败"; } }else{ echo "上传文件".$FILES['myfile']['name']."失败"; } ?>

  二、文件下载:

  单文件下载:

 <?<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>?>
Nach dem Login kopieren

  多个文件下载:

  DownList.php:

<a href="downprocess.php?filename=1.jpg">down</a><img  src="1.jpg"    style="max-width:90%" height="50px" alt="PHP文件上传及下载源码,_PHP教程" ><br><br>
<a href="downprocess.php?filename=2.jpg">down</a><img  src="2.jpg"    style="max-width:90%" height="50px" alt="PHP文件上传及下载源码,_PHP教程" ><br><br>
<a href="downprocess.php?filename=3.jpg">down</a><img  src="3.jpg"    style="max-width:90%" height="50px" alt="PHP文件上传及下载源码,_PHP教程" ><br><br>
<a href="downprocess.php?filename=4.jpg">down</a><img  src="4.jpg"    style="max-width:90%" height="50px" alt="PHP文件上传及下载源码,_PHP教程" ><br><br>
Nach dem Login kopieren

  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>?>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1131344.htmlTechArticlePHP文件上传及下载源码, 一、文件上传 前台页面: ! DOCTYPE html html head meta charset ="utf-8" title 文件上传 / title / head body div form method ="post" a...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage