Home >Backend Development >PHP Tutorial >PHP file upload code usage_PHP tutorial
PHP file upload code writing process
1. First determine whether to upload the file
2. If there is any error, please check again to see if there is an error in uploading
3. If an error occurs, an error message will be displayed
4. If there are no errors, then determine the file type
5. If the type meets the conditions, then determine whether the file exists in the specified directory
6. If not, move the file to the specified directory
Several things you must know when uploading files in php
$_FILES['myfile']['name'] refers to the name of the uploaded file
$_FILES['myfile']['type'] refers to the type of file being uploaded
$_FILES['myfile']['size'] refers to the size of the uploaded file, in bytes (B)
$_FILES['myfile']['tmp_name'] refers to the name of the temporary copy file of the uploaded file stored in the server. After the file is moved to the specified directory, the temporary file will be automatically destroyed.
$_FILES['myfile']["error"] refers to the status code of errors that may occur during file upload. The meaning of each status will be explained later.
Let’s take a look at the HTML part first.
The code is as follows | Copy code | ||||
?
|
Description:
The action="upload.php" in the form tag means that when you click submit in this form, the upload command will be sent to the page called upload.php for processing. method="post" refers to sending in post mode. The enctype="multipart/form-data" attribute specifies which content type to use when submitting this form. When the form requires binary data, such as file content, please use "multipart/form-data", this attribute is necessary if you want to upload files. Type="file" in input specifies that the input should be processed as a file, and there will be a browse button behind the input.
Let’s look at a PHP processing page upload.php
The code is as follows | Copy code | ||||
|
上面超级简单,我们现在来升级一下
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; ?> |
//接收
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
//echo $username.$fileintro;
//获取文件信息
/* echo "
";<br> print_r($_FILES);<br> echo "";
//获取文件类型
$file_type=$_FILES['myfile']['type'];
if($file_type!="image/jpeg" && $file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
//判断上传是否OK
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
//得到上传的文件 转存到你希望的目录
$upload_file=$_FILES['myfile']['tmp_name'];
//防止图片覆盖问题,为每个用户建立一个文件夹
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strripos($file_true_name,"."));
//echo $upload_file.$move_to_file;
//中文要转码
if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
echo $_FILES['myfile']['name']."上传成功";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
?>
Note:
Let me give you an example and everyone knows it. For example, a picture file pic.jpg, we use strrchr to process it, strrchr(pic.jpg,'.'), it will return .jpg, do you understand? This function returns the specified character The character after the last occurrence of the string. With substr(), we can get jpg, so that we can get the file extension to determine whether the uploaded file conforms to the specified format. This program puts the specified format in an array, which can be added as needed during actual use.
Next, look at the file name that generates random numbers. We see the function mt_srand(). The manual calls it "sowing a better random number generator seed". In fact, it is a function that initializes a random number. The parameters It is (double)microtime() * 1000000. If this is not a parameter, a random number will be automatically set. Of course, this does not meet our needs. In this way, the random number will have a certain length, ensuring that the uploaded file is not duplicated. First name