PHP文件上传代码用法详解
本文章是一篇适合于php入门者的文章告诉大如何编辑php文件上传代码,在编辑前我们需要了解几点,有及FILES全局变量的理解了,有需要学习php文件上传的朋友可参考本文章。
php文件上传代码编写过程
1. 先判断是否上传文件
2. 如果有再来判断上传中是否出错
3. 如果出错,则提示出错信息
4. 如查没出错,再判断文件类型
5. 如果类型符合条件,再判断指定目录中有没有存在该文件
6. 如果没有就把该文件移至指定目录
在php中上传文件必须知道的几个东西
$_FILES['myfile']['name'] 是指被上传文件的名称
$_FILES['myfile']['type'] 是指被上传文件的类型
$_FILES['myfile']['size'] 是指被上传文件的大小,单位为字节(B)
$_FILES['myfile']['tmp_name'] 是指被上传文件存在服务器中的临时副本文件名称,文件被移动到指定目录后临文件将被自动消毁。
$_FILES['myfile']["error"] 是指由文件上传中有可能出现的错误的状态码,关于各状态含义后在会说明。
先来看一下HTML部分。
代码如下 | 复制代码 |
? |
说明:
form标答的action="upload.php"是指点击这个form中的submit的时候,这个上传命令会被发送到这个叫 upload.php的页面去处理。method="post"是指以post方式去送,enctype="multipart/form-data"属性规定了在提交这个表单时要使用哪种内容类型,在表单需要二进制数据时,比如文件内容,请使用"multipart/form-data",如果要上传文件,这个属性是必要的。input中的type="file"时,规定了应该把输入作为文件来处理,并且在input后面会有一个浏览的按钮。
我们再来看一个PHP处理页面 upload.php
代码如下 | 复制代码 |
|
上面超级简单,我们现在来升级一下
1、upload.php
代码如下 | 复制代码 |
|
2、uploadProcess.php
代码如下 | 复制代码 |
//接收 ";<br> print_r($_FILES);<br> echo ""; */ //获取文件的大小 $file_size=$_FILES['myfile']['size']; if($file_size>2*1024*1024){ echo ""; exit(); } //获取文件类型 //判断上传是否OK //$move_to_file=$user_path."/".$_FILES['myfile']['name']; //echo $upload_file.$move_to_file; ?> |
注意:
我举个例子大家就知道,比如一个图片文件 pic.jpg,我们用 strrchr处理,strrchr(pic.jpg,'.'),它将返回.jpg,明白了吗?该函数返回指定字符在该字符串最后出现的位置后的字符。配合 substr() 我们就可以取到jpg,这样我们就得到了文件的后缀名,来判断上传文件是否符合指定格式。本程序把指定的格式放在一个数组中,实际使用时可根据需要添加。
接下来看产生随机数文件名部分, 我们看到mt_srand()这个函数,手册上叫他“播下一个更好的随机数发生器种子”,其实就是初始化一个随机数的函数,参数是(double)microtime() * 1000000,这里如果不这是参数就会自动设置个随机数,当然这不符合我们的需要,如此一来,随机数就具备一定的长度,保证了上传文件不重名

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.