亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Steps to upload php files

Steps of file upload

In order to learn PHP better, we have summarized the extremely complex PHP file upload into 6 steps.

In actual use, you can successfully complete PHP file upload by following these 6 steps:

1. Determine whether there is an error code


Detailed explanation of the error code returned by the system:

Error code Explanation
0 is correct and you can continue with subsequent operations of file upload.
1Exceeds the maximum limit of uploaded files, upload_max_filesize = 2M is set in php.ini, and the default is 2M. It can be modified according to the actual needs of the project
2If the specified file size is exceeded, specify the size limit of the uploaded file according to the business needs of the project
3Only some files were uploaded
4The files were not uploaded
6The temporary folder cannot be found. The directory may not exist or has no permissions.
7File writing failed. The disk may be full or there may be no permissions


Note: There is no 5 in the error code.

2. Customize the judgment whether the file size exceeds the range

When developing the upload function. As developers, we, in addition to the maximum upload value specified in php.ini.

We usually also set a value, which is the upload size limit specified by the business.

For example:
Sina Weibo or QQ Zone only allows a single avatar picture of 2M. When uploading albums, you can upload more than 2M.

So, its system supports larger file uploads.

The judgment file size here is used to limit the uploaded file size we want to specify in actual business.

3. Determine whether the suffix name and mime type match

There are also bad people in the online world. They will insert viruses into pictures, upload viruses in attachments, and they will insert viruses or pornographic pictures into web pages.

We need to judge the suffix and mime type of the uploaded file.

MIME (Multipurpose Internet Mail Extensions) is a multipurpose Internet mail extension type. It is a type of method that sets a file with a certain extension to be opened by an application. When the file with the extension is accessed, the browser will automatically use the specified application to open it. It is mostly used to specify some client-defined file names and some media file opening methods.

When determining the suffix and MIME type, we will use a PHP function in_array(), which passes in two parameters.
The first parameter is the value to be judged;
The second parameter is the range array.

We use this function to determine whether the file extension and mime type are within the allowed range.

4. Generate file name

Our file was uploaded successfully, but it will not save its original name.
Because some people who have sensitive keywords in their original names will violate the relevant laws and regulations of our country.

We can use date(), mt_rand() or unique() to generate random file names.

5. Determine whether the file is uploaded

When the file is uploaded successfully, the system will upload the uploaded temporary file to the system's temporary directory. Create a temporary file.

At the same time, a temporary file name will be generated. What we need to do is move the temporary files to the specified directory on the system.

It is unscientific not to move blindly before moving, or to move wrongly. Before moving, we need to use relevant functions to determine whether the uploaded file is a temporary file.

is_uploaded_file() passes in a parameter (the cache file name in $_FILES) to determine whether the passed in name is an uploaded file.

6. Move temporary files to the specified location

Temporary files are real temporary files, we need to move them to our website directory.

Let others access the data in our website directory.

We use: move_uploaded_file().
This function moves the uploaded file to the specified location and names it.
Pass in two parameters:
The first parameter is the uploaded file that specifies the move;
The second parameter is the string concatenating the specified folder and name.


Continuing Learning
||
<form action="" enctype="multipart/form-data" method="post" name="uploadfile">上傳文件:<input type="file" name="upfile" /><br> <input type="submit" value="上傳" /></form> <?php //print_r($_FILES["upfile"]); if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ $upfile=$_FILES["upfile"]; //獲取數(shù)組里面的值 $name=$upfile["name"];//上傳文件的文件名 $type=$upfile["type"];//上傳文件的類型 $size=$upfile["size"];//上傳文件的大小 $tmp_name=$upfile["tmp_name"];//上傳文件的臨時(shí)存放路徑 //判斷是否為圖片 switch ($type){ case 'image/pjpeg':$okType=true; break; case 'image/jpeg':$okType=true; break; case 'image/gif':$okType=true; break; case 'image/png':$okType=true; break; } if($okType){ /** * 0:文件上傳成功<br/> * 1:超過了文件大小,在php.ini文件中設(shè)置<br/> * 2:超過了文件的大小MAX_FILE_SIZE選項(xiàng)指定的值<br/> * 3:文件只有部分被上傳<br/> * 4:沒有文件被上傳<br/> * 5:上傳文件大小為0 */ $error=$upfile["error"];//上傳后系統(tǒng)返回的值 echo "================<br/>"; echo "上傳文件名稱是:".$name."<br/>"; echo "上傳文件類型是:".$type."<br/>"; echo "上傳文件大小是:".$size."<br/>"; echo "上傳后系統(tǒng)返回的值是:".$error."<br/>"; echo "上傳文件的臨時(shí)存放路徑是:".$tmp_name."<br/>"; echo "開始移動(dòng)上傳文件<br/>"; //把上傳的臨時(shí)文件移動(dòng)到up目錄下面 move_uploaded_file($tmp_name,'up/'.$name); $destination="up/".$name; echo "================<br/>"; echo "上傳信息:<br/>"; if($error==0){ echo "文件上傳成功啦!"; echo "<br>圖片預(yù)覽:<br>"; echo "<img src=".$destination.">"; //echo " alt=\"圖片預(yù)覽:\r文件名:".$destination."\r上傳時(shí)間:\">"; }elseif ($error==1){ echo "超過了文件大小,在php.ini文件中設(shè)置"; }elseif ($error==2){ echo "超過了文件的大小MAX_FILE_SIZE選項(xiàng)指定的值"; }elseif ($error==3){ echo "文件只有部分被上傳"; }elseif ($error==4){ echo "沒有文件被上傳"; }else{ echo "上傳文件大小為0"; } }else{ echo "請(qǐng)上傳jpg,gif,png等格式的圖片!"; } } ?>
submitReset Code