当前位置:网站首页>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);
}
边栏推荐
- 如何在 FastReport VCL 中通过 Outlook 发送和接收报告?
- Data security is gradually implemented, and we must pay close attention to the source of leakage
- 面试了一位38岁程序员,听说要加班就拒绝了
- Youxuan database participated in the compilation of the Research Report on database development (2022) of the China Academy of communications and communications
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- HDU 1914 the stable marriage problem
- Comprehensively analyze the differences between steam and maker Education
- Read the paper -- a CNN RNN framework for clip yield prediction
- HDU 3078 network (lca+ sort)
- Tips for using swiper (1)
猜你喜欢

UI automation test farewell from now on, manual download browser driver, recommended collection

Check box error
![[function document] torch Histc and paddle Histogram and numpy.histogram](/img/ee/ea918f79dc659369fde5394b333226.png)
[function document] torch Histc and paddle Histogram and numpy.histogram

Reading notes of SMT practical guide 1

MySQL(5)

Visual studio 2019 new OpenGL project does not need to reconfigure the environment

C语言ATM自动取款机系统项目的设计与开发

Dcgan:deep volume general adaptive networks -- paper analysis

Redux basic syntax

What SaaS architecture design do you need to know?
随机推荐
HDU 1435 stable match
Visual studio 2019 new OpenGL project does not need to reconfigure the environment
Redux basic syntax
FreeRTOS个人笔记-任务通知
HDU 3078 network (lca+ sort)
动态sql和分页
How to send and receive reports through outlook in FastReport VCL?
[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme
Redis type
What is the core value of testing?
Reading notes of SMT practical guide 1
多御安全浏览器将改进安全模式,让用户浏览更安全
Online sql to XML tool
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
Leetcode 15. sum of three numbers
HDU 3585 maximum shortest distance
Transformer -- Analysis and application of attention model
Do you know several assertion methods commonly used by JMeter?