How to implement file upload in PHP

qzuser
Release: 2023-03-18 20:50:01
forward
2681 people have browsed it

How does PHP implement file upload? This article will introduce you to the method of uploading files in PHP through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Here we use PHP's global array $_FILES to upload files:

  • ##$_FILES["file"]["name"] - The name of the uploaded file

  • $_FILES["file"]["type"] - The type of the uploaded file

  • $_FILES["file"]["size"] - The size of the uploaded file, in bytes

  • $_FILES[ "file"]["tmp_name"] - The name of the temporary copy of the file stored on the server

  • $_FILES["file"]["error"] - Error code caused by file upload

[Related video tutorial recommendation:

php tutorial]

HTML code:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>
Copy after login
PHP code:

if (!empty($_FILES)) {
 
    // 限制文件大小
    $file_size = $_FILES["file"]["size"];
    // 限制2M大小
    if ($file_size > 1024 * 1024 * 2) {
        echo '文件大小超过限制';
        exit;
    }
 
    // 限制文件上传类型
    $file_type = $_FILES["file"]["type"];
    $file_type_arr = ['image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif'];
    if (!in_array($file_type, $file_type_arr)) {
        echo '上传文件类型错误';
        exit;
    }
 
    // 文件上传到服务器临时文件夹之后的文件名
    $tem_name = $_FILES['file']['tmp_name'];
    // 取得文件后缀名
    $file_fix = explode('.', $_FILES['file']['name'])[1] ? explode('.', $_FILES['file']['name'])[1] : 'png';
    // 文件重命名,这里自动生成一个不重复的名字,方便使用
    $name = md5(uniqid(md5(microtime(true)), true)) . '.' . $file_fix;
    // 要存放文件的目录定义,这里按日期分开存储
    $file_dir = dirname(__FILE__) . '/upload/' . date('Ymd') . '/';
    // 检测要存放文件的目录是否存在,不存在则创建
    if (!is_dir($file_dir)) {
        mkdir($file_dir, 0755, true);
    }
    // 移动文件到指定目录下
    @ move_uploaded_file($tem_name, $file_dir . $name);
 
    echo '上传成功';
    exit;
} else {
    echo '文件上传失败';
    exit;
}
Copy after login
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

The above is the detailed content of How to implement file upload in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!