Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Single file upload

Shulou Source: shulou.com Published: 2022-06-01 10:32:01 09月21日 Update

Create a new 1107.php file and write the following code in the file:

File upload

Select a file:

Create a new t2.php file and write the following code in the file:

$allowtype=array ("gif", "png", "jpg"); / / sets the types of files allowed to be uploaded

$size=1000000; / / sets the file size allowed to upload

$path= ". / uploads"; / / set the path to save the file after uploading

/ / determine whether the file can be successfully uploaded to the server. If $_ FILES ["myfile"] ["error"] is 0, the file can be uploaded successfully.

If ($_ FILES ["myfile"] ["error"] > 0) {

Echo "File upload error"

Switch ($_ FILES ["myfile"] ["error"]) {

Case 1:die ("upload file size exceeds the agreed value in the PHP configuration file"); break

Case 2:die ("upload file size exceeds the agreed value in the form"); break

Case 3:die ("the file is only partially uploaded"); break

Case 4:die ("No files uploaded"); break

Default: die (unknown error); break

}

}

/ / determine whether the uploaded file is an allowed type by the suffix name of the file.

$hz=array_pop (explode (".", $_ FILES ["myfile"] ["name"]))

If (! in_array ($hz,$allowtype)) {

Die ("this file suffix is {$hz}, not the allowed file type!")

}

/ / determine whether the uploaded file is the allowed size

If ($_ FILES ["myfile"] ["name"] > $size) {

Die ("exceeds the allowed {$size} byte size")

}

/ / for the sake of system security and not to overwrite the file with the same name, use the system definition after uploading the file name

$filename=date ("YmdHis"). Rand (100999). ". $hz

/ / determine whether the file is uploaded

If (is_uploaded_file ($_ FILES ["myfile"] ["tmp_name"]) {

If (! move_uploaded_file ($_ FILES ["myfile"] ["tmp_name"], $path. "/". $filename) {

Die ("problem: cannot move files to the specified directory")

}

} else {

Die ("problem: uploading file {$_ FILES [" myfile "] [" name "]} is not a legal file!)

}

/ / output if the file is uploaded successfully

Echo "File {$upfile} uploaded successfully and saved in directory {$path} with a file size of {$_ FILES [" myfile "] [" size "]} bytes"

To execute the above example, you need to create a uploads directory (that is, a folder) in the current directory!

The die () function outputs a message and exits the current script

This function is an alias for the exit () function.

The syntax die (status) parameter describes the status required. Specifies the message or status number written before exiting the script. The status number is not written to the output. Description

If status is a string, the function outputs the string before exiting.

If status is an integer, this value is used as the exit state. The value of the exit status is between 0 and 254. The exit status of 255 is reserved by PHP and will not be used. Status 0 is used to successfully terminate the program.

Tips and comments: if the version number of PHP is greater than or equal to 4.2.0, this parameter will not be output if status is an integer.

Tags: Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Shulou Information NVidia Shulou Technology