当前位置:网站首页>Implementation of simple upload function in PHP development
Implementation of simple upload function in PHP development
2022-07-28 05:05:00 【A dream against the sky】
The first 1 Chapter PHP Development of simple upload function
1.1 PHP Upload principle and implementation of file upload
file.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Upload files </title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please select the file to upload :<br>
<input type="file" name="myFile"><br>
<input type="submit" value=" Upload ">
</form>
</body>
</html>
upload.php:
<?php
header("content-type:text/html;charset=utf-8");
// File upload processing program
// $_FILES File upload variables
//echo "<pre>";
//var_dump($_FILES);
//exit();
//echo "</pre>";
// Handling upload files
$filename = $_FILES['myFile']['name'];
$type = $_FILES['myFile']['type'];
$tmp_name = $_FILES['myFile']['tmp_name'];
$size = $_FILES['myFile']['size'];
$error = $_FILES['myFile']['error'];
// Move the temporary files on the server to the specified location
move_uploaded_file($tmp_name, "upload/".$filename); // Folders should be created in advance
// Check according to our errors or report to the user directly
if ($error == 0) {
echo " Upload successful !!";
} else {
switch ($error) {
case 1:
echo " Exceeded the maximum upload file , Please upload 2M The following documents !";
break;
case 2:
echo " Too many uploaded files !";
break;
case 3:
echo " The file has not been uploaded !";
break;
case 4:
echo " No upload file selected !";
break;
case 5:
echo " The upload file is 0!";
break;
}
}
?>
1.2 PHP Realize the upload limit of file upload
file2.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Upload files ( Client limitations )</title>
</head>
<body>
<form action="upload2.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="101321">
Please select the file to upload :
<input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"><br>
<input type="submit" value=" Upload ">
</form>
</body>
</html>
upload2.php:
<?php
header('content-type:text/html;charset=utf-8');
// Receive the file , Temporary file information
$file_info = $_FILES['myFile']; // Dimension reduction operation
$file_name = $file_info['name'];
$tmp_name = $file_info['tmp_name'];
$size = $file_info['size'];
$type = $file_info['type'];
$error = $file_info['error'];
// Set limits on the server side
$maxsize = 10485760; // 10M,10*1024*1024
$allowExt = array('jpeg','jpg','png','gif'); // The types of files allowed to be uploaded ( Extension )
$ext = pathinfo($file_name, PATHINFO_EXTENSION); // Extract the extension of the uploaded file
// Target storage folder
$path = "uploads";
if (!file_exists($path)) {
// If the directory does not exist , Create
mkdir($path, 0777, true); // Create directory
chmod($path, 0777); // Modify file mode , Everyone has read 、 Write 、 Executive authority
}
// Get a unique file name , Prevent overwriting due to the same file name
$uniName = md5(uniqid(microtime(true),true)).".$ext"; // md5 encryption ,uniqid Produce a unique id,microtime Prefix
// Destination file address
$destination = $path."/".$uniName;
// When the file is uploaded successfully , Save to temporary folder , The server starts to judge
if ($error===0) {
if ($size > $maxsize) {
exit(" The upload file is too large !");
}
if (!in_array($ext, $allowExt)) {
exit(" Illegal file type !");
}
if (!is_uploaded_file($tmp_name)) {
exit(" Wrong upload method , Please use post The way !");
}
// Determine whether it is a real picture ( Prevent viruses disguised as pictures
if (!getimagesize($tmp_name)) {
// getimagesize by true Returns an array , Otherwise return to false
exit(" It's not really a picture type !");
}
// move_uploaded_file($tmp_name, "upload/".$file_name);
if (@move_uploaded_file($tmp_name, $destination)) {
// @ Error suppressor , Don't let users see the warning
echo " file ".$file_name." Upload successful !";
} else {
echo " file ".$file_name." Upload failed !";
}
} else {
switch ($error) {
case 1:
echo " The maximum number of uploaded files has been exceeded , Please upload 2M The following documents !";
break;
case 2:
echo " Too many uploaded files , You can only upload files at a time, and it must not exceed 20 individual !";
break;
case 3:
echo " The file was not completely uploaded , Please try again !";
break;
case 4:
echo " No upload file selected !";
break;
case 7:
echo " No temporary folders !";
break;
}
}
1.3 PHP Realize the package upload function of file upload
file3.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Upload files ( Client limitations )</title>
</head>
<body>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="101321">
Please select the file to upload :
<input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"><br>
<input type="submit" value=" Upload ">
</form>
</body>
</html>
file_upload.php:
<?php
function uploadFile ($file_info, $path, $maxsize, $allowExt) {
header('content-type:text/html;charset=utf-8');
// Receive the file , Temporary file information
$file_name = $file_info['name'];
$tmp_name = $file_info['tmp_name'];
$size = $file_info['size'];
$type = $file_info['type'];
$error = $file_info['error'];
// Set limits on the server side
$ext = pathinfo($file_name, PATHINFO_EXTENSION); // Extract the extension of the uploaded file
// Target storage folder
if (!file_exists($path)) {
// If the directory does not exist , Create
mkdir($path, 0777, true); // Create directory
chmod($path, 0777); // Modify file mode , Everyone has read 、 Write 、 Executive authority
}
// Get a unique file name , Prevent overwriting due to the same file name
$uniName = md5(uniqid(microtime(true),true)).".$ext"; // md5 encryption ,uniqid Produce a unique id,microtime Prefix
// Destination file address
$destination = $path."/".$uniName;
// When the file is uploaded successfully , Save to temporary folder , The server starts to judge
if ($error===0) {
if ($size > $maxsize) {
exit(" The upload file is too large !");
}
if (!in_array($ext, $allowExt)) {
exit(" Illegal file type !");
}
if (!is_uploaded_file($tmp_name)) {
exit(" Wrong upload method , Please use post The way !");
}
// Determine whether it is a real picture ( Prevent viruses disguised as pictures
if (!getimagesize($tmp_name)) {
// getimagesize by true Returns an array , Otherwise return to false
exit(" It's not really a picture type !");
}
// move_uploaded_file($tmp_name, "upload/".$file_name);
if (@move_uploaded_file($tmp_name, $destination)) {
// @ Error suppressor , Don't let users see the warning
echo " file ".$file_name." Upload successful !";
} else {
echo " file ".$file_name." Upload failed !";
}
} else {
switch ($error) {
case 1:
echo " The maximum number of uploaded files has been exceeded , Please upload 2M The following documents !";
break;
case 2:
echo " Too many uploaded files , You can only upload files at a time, and it must not exceed 20 individual !";
break;
case 3:
echo " The file was not completely uploaded , Please try again !";
break;
case 4:
echo " No upload file selected !";
break;
case 7:
echo " No temporary folders !";
break;
}
}
return $destination;
}
upload3.php:
<?php
header('content-type:text/html;charset=utf-8');
// Initialization of related variables
$file_info = $_FILES['myFile']; // Dimension reduction operation
$maxsize = 10485760; // 10M,10*1024*1024
$allowExt = array('jpeg','jpg','png','gif'); // The types of files allowed to be uploaded ( Extension )
$path = "uploads";
// Introduce file upload function
include_once 'file_upload.php';
uploadFile($file_info, $path, $maxsize, $allowExt);
?>
1.4 PHP Realize the uploading of many files
file4.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Multiple file upload </title>
</head>
<body>
<form action="upload4.php" method="post" enctype="multipart/form-data">
Please select the file to upload :<br>
<input type="file" name="myFile1"><br>
Please select the file to upload :<br>
<input type="file" name="myFile2"><br>
Please select the file to upload :<br>
<input type="file" name="myFile3"><br>
Please select the file to upload :<br>
<input type="file" name="myFile4"><br>
Please select the file to upload :<br>
<input type="file" name="myFile5"><br>
Please select the file to upload :<br>
<input type="file" name="myFile6"><br>
<input type="submit" value=" Upload ">
</form>
</body>
</html>
upload4.php:
<?php
header("content-type:text/html;charset=utf-8");
include_once 'file_upload.php';
// Initialization of related variables
$maxsize = 10485760; // 10M,10*1024*1024
$allowExt = array('jpeg','jpg','png','gif'); // The types of files allowed to be uploaded ( Extension )
$path = "uploads";
foreach ($_FILES as $file_info) {
$file[] = uploadFile($file_info, $path, $maxsize, $allowExt);
}
边栏推荐
- The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
- HashSet add
- 如何在 FastReport VCL 中通过 Outlook 发送和接收报告?
- flink思维导图
- [internal mental skill] - creation and destruction of function stack frame (C implementation)
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- 【ARXIV2204】Vision Transformers for Single Image Dehazing
- RT based_ Distributed wireless temperature monitoring system based on thread
- 基于MPLS构建虚拟专网的配置实验
- When initializing with pyqt5, super() and_ init _ () problems faced by the coordinated use of functions, as well as the corresponding learning and solutions
猜你喜欢

Observable time series data downsampling practice in Prometheus

【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution

C language classic 100 question exercise (1~21)

Improve the core quality of steam education among students

【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation

【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers

Win10 machine learning environment construction pycharm, anaconda, pytorch

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight

What should testers know about login security?

Summary and review of puppeter
随机推荐
POJ 3417 network (lca+ differential on tree)
为什么md5不可逆,却还可能被md5免费解密网站解密
How to successfully test php7.1 connecting to sqlserver2008r2
【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation
How to send and receive reports through outlook in FastReport VCL?
jsonp 单点登录 权限检验
HDU 1530 maximum clique
Array or object, date operation
如何在 FastReport VCL 中通过 Outlook 发送和接收报告?
Read the paper -- a CNN RNN framework for clip yield prediction
go-zero单体服务使用泛型简化注册Handler路由
[internal mental skill] - creation and destruction of function stack frame (C implementation)
Applet import project
【ARXIV2204】Vision Transformers for Single Image Dehazing
RT based_ Distributed wireless temperature monitoring system based on thread
Online sql to XML tool
[daily one] visual studio2015 installation in ancient times
How to simulate common web application operations when using testcafe
Redis type
Know etcd