Home > Backend Development > PHP Tutorial > PHP file upload (implemented using global array $_FILES)

PHP file upload (implemented using global array $_FILES)

little bottle
Release: 2023-04-06 08:08:01
forward
3512 people have browsed it

This article mainly talks about using the global array $_FILES in PHP to implement file upload. It has certain reference value. Interested friends can learn about it.

The PHP global array $_FILES is used here to implement file upload:

  • $_FILES["file"]["name"] - Uploaded file The name of
  • $_FILES["file"]["type"] - The type of file being uploaded
  • $_FILES["file"][" size"] - Size of the file being uploaded, in bytes
  • $_FILES["file"]["tmp_name"] - Temporary copy of the file stored on the server Name
  • $_FILES["file"]["error"] - Error code caused by file upload

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 &#39;文件大小超过限制&#39;;        exit;
    } 
    // 限制文件上传类型
    $file_type = $_FILES["file"]["type"];    $file_type_arr = [&#39;image/jpg&#39;, &#39;image/jpeg&#39;, &#39;image/png&#39;, &#39;image/pjpeg&#39;, &#39;image/gif&#39;];    if (!in_array($file_type, $file_type_arr)) {        echo &#39;上传文件类型错误&#39;;        exit;
    } 
    // 文件上传到服务器临时文件夹之后的文件名
    $tem_name = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];    // 取得文件后缀名
    $file_fix = explode(&#39;.&#39;, $_FILES[&#39;file&#39;][&#39;name&#39;])[1] ? explode(&#39;.&#39;, $_FILES[&#39;file&#39;][&#39;name&#39;])[1] : &#39;png&#39;;    // 文件重命名,这里自动生成一个不重复的名字,方便使用
    $name = md5(uniqid(md5(microtime(true)), true)) . &#39;.&#39; . $file_fix;    // 要存放文件的目录定义,这里按日期分开存储
    $file_dir = dirname(__FILE__) . &#39;/upload/&#39; . date(&#39;Ymd&#39;) . &#39;/&#39;;    // 检测要存放文件的目录是否存在,不存在则创建
    if (!is_dir($file_dir)) {        mkdir($file_dir, 0755, true);
    }    // 移动文件到指定目录下
    @ move_uploaded_file($tem_name, $file_dir . $name); 
    echo &#39;上传成功&#39;;    exit;
} else {    echo &#39;文件上传失败&#39;;    exit;
}
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of PHP file upload (implemented using global array $_FILES). 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