文件上传知多少,PHP面试学到老,虽然够用也挺好,知识体系也重要
日拱一卒无有尽,功不唐捐终入海 💋
前言
文件上传作为常用功能,在面试中也是很基础的问题。很多开发者从网上随便搜一下面试题一背,一般也能过。
但是你真的学会了吗?

经典面试题
1. PHP如何获取上传的文件?
使用超全局变量$_FILES获取,如下所示
Array
(
    [name] => Array
        (
            [0] => facepalm.jpg
            [1] =>
        )
    [type] => Array
        (
            [0] => image/jpeg
            [1] =>
        )
    [tmp_name] => Array
        (
            [0] => /tmp/phpn3FmFr
            [1] =>
        )
    [error] => Array
        (
            [0] => 0
            [1] => 4
        )
    [size] => Array
        (
            [0] => 15476
            [1] => 0
        )
)
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
字段的含义
- $_FILES['userfile']['name']客户端机器文件的原名称
- $_FILES['userfile']['type']文件的 MIME 类型
- $_FILES['userfile']['size']已上传文件的大小,单位为字节
- $_FILES['userfile']['tmp_name']文件被上传后在服务端储存的临时文件名
- $_FILES['userfile']['error']和该文件上传相关的错误代码
2. 哪些配置影响文件上传的大小数量?
- php.ini中的 upload_max_filesize。
- php.ini中的 post_max_size
- php.ini中的 max_file_uploads
- 网页表单中的input的属性 MAX_FILE_SIZE
3. PHP除了POST还可以怎么接收上传文件?
PUT方法上传的文件,$putdata = fopen("php://input", "r");
4.PHP上传文件的关键方法?
move_uploaded_file(string $filename, string $destination): bool
将上传的文件移动到新位置

文件上传处理完整解析
上面的列举的文件上传相关的面试题都只是冰山一角。如果我们要学习一个知识点,那么仅仅依靠零星的面试题是不行的,系统化的学习才是重点。
文件上传的流程
文件上传的基本流程为
- 表单接收文件
- 上传到服务器临时文件夹
- 服务端把文件从临时文件夹转移到持久化的文件夹
php.ini文件上传的配置
file_uploads “1” PHP_INI_SYSTEM 
 upload_tmp_dir NULL PHP_INI_SYSTEM 
 max_input_nesting_level 64 PHP_INI_PERDIR 
 max_input_vars 1000 PHP_INI_PERDIR 
 upload_max_filesize “2M” PHP_INI_PERDIR 
 max_file_uploads 20 PHP_INI_SYSTEM
完整的文件上传最佳实践
表单校验文件大小和限制文件类型
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
  
 - 1
- 2
- 3
- 4
- 5
php获取文件并处理报错信息
header('Content-Type: text/plain; charset=utf-8');
try {
   
    if (
        !isset($_FILES['upfile']['error']) ||
        is_array($_FILES['upfile']['error'])
    ) {
        throw new RuntimeException('Invalid parameters.');
    }
    switch ($_FILES['upfile']['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:
            throw new RuntimeException('No file sent.');
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            throw new RuntimeException('Exceeded filesize limit.');
        default:
            throw new RuntimeException('Unknown errors.');
    }
    if ($_FILES['upfile']['size'] > 1000000) {
        throw new RuntimeException('Exceeded filesize limit.');
    }
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
        array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',
        ),
        true
    )) {
        throw new RuntimeException('Invalid file format.');
    }
    if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        sprintf('./uploads/%s.%s',
            sha1_file($_FILES['upfile']['tmp_name']),
            $ext
        )
    )) {
        throw new RuntimeException('Failed to move uploaded file.');
    }
    echo 'File is uploaded successfully.';
} catch (RuntimeException $e) {
    echo $e->getMessage();
}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57

注意,目前大多数项目的静态资源都是通过第三方云存储,所以,我们在实际业务中,可以直接把临时文件夹中的文件上传到第三方云中,并且不需要在项目中存一次做中转。
总结
常见并且容易被大家忽略的问题还有
- 如何处理异常
- 占用内存过大的脚本如何处理
- 手写框架
- 等等
如果你对哪些问题有疑问,欢迎评论在下面,我会根据大家的留言再开文章详细说明!

文章来源: coderfix.blog.csdn.net,作者:小雨青年,版权归原作者所有,如需转载,请联系作者。
原文链接:coderfix.blog.csdn.net/article/details/118565246
- 点赞
- 收藏
- 关注作者
 
             
           
评论(0)