Maison > Article > développement back-end > 实战PHP上传文件代码演示_PHP教程
PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下PHP上传文件代码,和大家讨论讨论。PHP本身是一种简单而强大的语言。PHP语言拥有核心特性如强大的字符串和数组处理能力,同时极大的改进了对面向对象编程的支持(PHP5以上版本)。通过使用标准的和可选的扩展模块,PHP应用程序可以连接MySQL或Oracle等十几种数据库、绘图、创建PDF文件和创建解析XML文件。你也可以使用C语言来写自己的PHP扩展模块。
PHP上传文件代码
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span><li class=""> <span></span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>上载文件表单</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span></span> </li> <li class="alt"> <span></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span> </li> <li class=""> <span></span><span class="tag"><span class="tag-name">form</span><span> </span><span class="attribute">enctype</span><span>=</span><span class="attribute-value">"multipart/form-data"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">""</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span class="tag">></span><span> </span></span> </li> <li class="alt"> <span>请选择文件: </span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span> </span></span> </li> <li class=""> <span></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"upload_file"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"file"</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span> </span></span><li class="alt"> <span></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"上传文件"</span><span class="tag">></span><span> </span></span> </li> <li class=""> <span></span><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span> </span> </li></span> </li> <li class="alt"> <span></span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span> </li></span><li class=""> <span></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span> </li></span></li> <li class="alt"> <span></span><span class="tag"></span><span> </span> </li> <li class=""> <span>$</span><span class="attribute">upload_file</span><span>=$_FILES['upload_file']['tmp_name']; </span> </li> <li class="alt"> <span>$</span><span class="attribute">upload_file_name</span><span>=$_FILES['upload_file']['name']; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>if($upload_file){ </span></li> <li class=""> <span>$</span><span class="attribute">file_size_max</span><span> = </span><span class="attribute-value">1000</span><span>*1000;// 1M限制文件上传最大容量(bytes) </span> </li> <li class="alt"> <span>$</span><span class="attribute">store_dir</span><span> = </span><span class="attribute-value">"d:/"</span><span>;// 上传文件的储存位置 </span> </li> <li class=""> <span>$</span><span class="attribute">accept_overwrite</span><span> = </span><span class="attribute-value">1</span><span>;//是否允许覆盖相同文件 </span> </li> <li class="alt"><span>// 检查文件大小 </span></li> <li class=""> <span>if ($upload_file_size </span><span class="tag">></span><span> $file_size_max) { </span> </li> <li class="alt"><span>echo "对不起,你的文件容量大于规定"; </span></li> <li class=""><span>exit; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>// 检查读写文件 </span></li> <li class=""><span>if (file_exists($store_dir . $upload_file_name) && !$accept_overwrite) { </span></li> <li class="alt"><span>Echo "存在相同文件名的文件"; </span></li> <li class=""><span>exit; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//复制文件到指定目录 </span></li> <li class=""><span>if (!move_uploaded_file($upload_file,$store_dir.$upload_file_name)) { </span></li> <li class="alt"><span>echo "复制文件失败"; </span></li> <li class=""><span>exit; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"> <span>Echo "</span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>你上传了文件:"; </span></span> </li> <li class=""><span>echo$_FILES['upload_file']['name']; </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span> </li> <li class=""><span>//客户端机器文件的原名称。 </span></li> <li class="alt"><span> </span></li> <li class=""><span>Echo "文件的 MIME 类型为:"; </span></li> <li class="alt"><span>echo $_FILES['upload_file']['type']; </span></li> <li class=""><span>//文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。 </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span> </li> <li class=""><span> </span></li> <li class="alt"><span>Echo "上传文件大小:"; </span></li> <li class=""><span>echo $_FILES['upload_file']['size']; </span></li> <li class="alt"><span>//已上传文件的大小,单位为字节。 </span></li> <li class=""> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span> </li> <li class="alt"><span> </span></li> <li class=""><span>Echo "文件上传后被临时储存为:"; </span></li> <li class="alt"><span>echo $_FILES['upload_file']['tmp_name']; </span></li> <li class=""><span>//文件被上传后在服务端储存的临时文件名。 </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span> </li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute">Erroe</span><span>=$_FILES['upload_file']['error']; </span> </li> <li class="alt"><span>switch($Erroe){ </span></li> <li class=""><span>case 0: </span></li> <li class="alt"><span>Echo "上传成功"; break; </span></li> <li class=""><span>case 1: </span></li> <li class="alt"><span>Echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值."; break; </span></li> <li class=""><span>case 2: </span></li> <li class="alt"><span>Echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。";break; </span></li> <li class=""><span>case 3: </span></li> <li class="alt"><span>Echo "文件只有部分被上传";break; </span></li> <li class=""><span>case 4: </span></li> <li class="alt"><span>Echo "没有文件被上传";break; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag">?></span><span> </span> </li> </ol>
以上就是详细的PHP上传文件代码,希望对大家有帮助。