文件上传

 


 

上传单张图片,以上传一个商品封面为例

html页面代码

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <p><input type="file" name="cover" /></p>
        <p><input type="submit" value="上传" /></p>
    </form>
</body>

 

PHP代码

 

class GoodsModel extends Model
{
    public function publishGoods()
    {
        $data = array();
        $goods_id = $this->insert($data);
        //上传封面图
        $file = $this->uploadGoodsCover($goods_id);
        print_r($file);
    }
    
    protected function uploadGoodsCover($goods_id)
    {
        $upload = new UploadFiles();
        $four_path = Helper::getFourPath($goods_id); // 000/00/10/09/
        $relative_path = 'goods/cover/' . $four_path; // goods/cover/000/00/01/00/
        $file_dir = Loader::getFilePath('static::' . $relative_path);
        $ret = $upload->upload('cover', $file_dir);
        
        if (!$ret) {
            return false;
        }
        
        $file = $upload->getSuccessInfo()[0];
        return $file;
    }
}

(new GoodsModel())->publishGoods();

 

输出数据如下:

Array
(
    [name] => 57d7900d264b9.jpg
    [type] => image/jpeg
    [tmp_name] => C:\Windows\Temp\phpBA9C.tmp
    [error] => 0
    [size] => 31051
    [extension] => jpg
    [savepath] => E:\store\TimoPHP\wwwroot\web\static\goods\cover\000\00\01\09\
    [savename] => 5809b14e2fa5c1486.jpg
)

 

上传多张图片

html页面代码

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <p><input type="file" name="cover[]" /></p>
        <p><input type="file" name="cover[]" /></p>
        <p><input type="file" name="cover[]" /></p>
        <p><input type="submit" value="上传" /></p>
    </form>
</body>

 

PHP代码

 

$upload = new UploadFiles();
$ret = $upload->upload('cover', $file_dir);
if (!$ret) {
    return false;
}
$files = $upload->getSuccessInfo()
print_r($files);

 

输出如下:

 

Array
(
    [0] => Array
        (
            [name] => 57d7900d264b9.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\Temp\phpBA9C.tmp
            [error] => 0
            [size] => 31051
            [extension] => jpg
            [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
            [savename] => 5809b14e2fa5c1486.jpg
        )

    [1] => Array
        (
            [name] => 01191439710123.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\Temp\phpBAAD.tmp
            [error] => 0
            [size] => 30127
            [extension] => jpg
            [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
            [savename] => 5809b14e2fe448387.jpg
        )

    [2] => Array
        (
            [name] => 20160929123459.png
            [type] => image/png
            [tmp_name] => C:\Windows\Temp\phpBAAE.tmp
            [error] => 0
            [size] => 18165
            [extension] => png
            [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
            [savename] => 5809b14e3022c4328.png
        )
)