php image upload ($ _FILES)

Foreword

Before you start uploading the picture, we first look at the required global array $_FILESAPI:

grammar:

$_FILES["file"]["type"]

Parameter Description:

  1. file: front-end form input name.
  2. type: can be "name", "type", "size", "tmp_name" or "error".

type:

  • $ _FILES [ "file"] [ "name"] - is the name of the uploaded file
  • $ _FILES [ "file"] [ "type"] - is the type of file uploaded
  • $ _FILES [ "file"] [ "size"] - the upload file size, in bytes
  • $ _FILES [ "file"] [ "tmp_name"] - the name of the temporary copy is stored in the file server
  • $ _FILES [ "file"] [ "error"] - the error code resulting from the file upload

test

We first test whether to upload pictures from the front end of the back-end properties will receive and print what image properties. Here I was in a local test, so a direct front-end back-end write the php file.

<!-- 前端 -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
        <input type="file" name="img">
        <input type="submit" name="submit">
</form>

<?php
# 错误判断
if($_FILES['img']['error'] > 0){
    echo '失败:' . $_FILES['img']['error'];
}

# 执行处理
else{
    show();
}

# 处理函数
function show(){
    echo '文件名称:' . $_FILES['img']['name'] . '<br/>';
    echo '文件类型:' . $_FILES['img']['type'] . '<br/>';
    echo '文件大小(MB):' . $_FILES['img']['size'] / 1024 . '<br/>';//默认字节B
    echo '服务器临时副本名称:' . $_FILES['img']['tmp_name'] . '<br/>';
}
?>

Here Insert Picture Description
Of course, these data have not been treated with the determination, to look bad.

Upload limit

We might observe that some sites the user is required to limit the file, for example: images must be .jpg and must be less than 1MB, and so demand, then the php how to complete the upload limit it? There are many ways!

For example: Users can only upload .gif / .jpeg file, the file size must be less than 20 KB:

<!-- 前端 -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
        <input type="file" name="img">
        <input type="submit" name="submit">
</form>

<?php
# 后面频繁使用最好封装一下
$type = $_FILES['img']['type'];
$size = $_FILES['img']['size'];
$name = $_FILES['img']['name'];

# 上传限制判断
if(
    ($type == 'image/gif') //是不是git格式
    || ($type == 'image/jpeg') //是不是jpg格式
    || ($type == 'image/jpeg') //兼容IE(判断jpg格式)
    && ($size < 20000) //获取到的size是字节(B)
)

# 通过限制
{
    echo '恭喜您,图像 ' . $name . '可以使用!';
}

# 没通过
else{
    echo '很遗憾,图像 ' . $name . '不符合限制!';
}
?>

Here Insert Picture Description
Note: For IE, identified jpg file type must be pjpeg, for FireFox, must be jpeg.

Save the file to be uploaded

Remember us in 标题——测试a temporary copy of it in the name of the server output?

This temporary copy files will disappear at the end of the script, that is close or refresh the page after cleaning, so to save the file to be uploaded, we need to copy it to another location.

For example, we throw in addition to judgment and restrictions, store files directly to the server in the images:

<!-- 前端 -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
        <input type="file" name="img">
        <input type="submit" name="submit">
</form>

<?php
# 提示信息
echo '即将上传的文件为:' . $_FILES['img']['name'] . '<br>';
echo '临时存储路径为:' . $_FILES['img']['tmp_name'] . '<br>';

# 判断 images 文件夹下是否存在当前文件
if(file_exists('images/' . $_FILES['img']['name'])){
    echo '——————————————' . '<br>';
    echo '存储失败,数据库中已有该文件!' . '<br>';
}

# 如果不存在该文件就存储到 images 文件夹下
else{
    move_uploaded_file(
        $_FILES['img']['tmp_name'],
        'images/' . $FILES['img']['name']
    );
    echo '上传成功!';
}
?>

file_exists()https://www.php.net/file_exists

move_uploaded_filehttps://www.w3school.com.cn/php/func_filesystem_move_uploaded_file.asp

Published 267 original articles · won praise 418 · views 830 000 +

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/104445590