PHP file upload and download source code, _PHP tutorial
PHP file upload and download source code,
1. File upload
Front page:
<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>
Background processing:
<?<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 ""; //可以在这里对文件大小进行限制 /*$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']."失败"; } ?>
2. File download:
Single file download:
<?<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>?>
Multiple file downloads:
DownList.php:
<a href="downprocess.php?filename=1.jpg">down</a><img src="1.jpg" width="50px" height="50px"><br><br> <a href="downprocess.php?filename=2.jpg">down</a><img src="2.jpg" width="50px" height="50px"><br><br> <a href="downprocess.php?filename=3.jpg">down</a><img src="3.jpg" width="50px" height="50px"><br><br> <a href="downprocess.php?filename=4.jpg">down</a><img src="4.jpg" width="50px" height="50px"><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>?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.
