php教程--案例29(文件上传类)

t218.php

<?php
//case 29 文件上传类
//上传类
class upload
{
    //私有,文件类型数组
    private $allow_types = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif');
    //文件尺寸
    private $max_size = 1048576;
    //图片目录
    private $upload_path = './img/';
    //错误提示
    private $error = '';

    /**
     * @param $file 上传文件的数组数据
     * @param string $prefix 文件名前缀
     * @return bool|string 成功返回上传文件的目录地址,失败false
     */
    public function up($file,$prefix='')
    {
        //有错误时
        if($file['error'] != 0)
        {
            $upload_errors = array(
                1=>'文件过大,超过了php配置的限制',
                2=>'文件过大,超过了form表单的限制',
                3=>'文件没有上传完毕',
                4=>'文件没有上传',
                6=>'没有找到临时上传目录',
                7=>'临时文件写入失败',
            );
            $this->error = isset($upload_errors[$file['error']])?$upload_errors[$file['error']]:'未知错误';
            return false;
        }
        //类型不符
        if(!in_array($file['type'],$this->allow_types))
        {
            $this->error = '此类型不能上传,允许的类型为:'.implode('|',$this->allow_types);
            return false;
        }
        //文件过大
        if($file['size'] > $this->max_size)
        {
            $this->error = '文件不能超过'.$this->max_size.'字节';
            return false;
        }
        //生成一个随机id+扩展名
        $new_file = uniqid($prefix).strrchr($file['name'],'.');
        //子目录
        $sub_path = date('YmdH');
        //全路径
        $upload_path = $this->upload_path.$sub_path;
        //判断不是目录时,建立目录
        if(!is_dir($upload_path))
        {
            mkdir($upload_path);
            if(move_uploaded_file($file['tmp_name'],$upload_path.'/'.$new_file))
            {
                return $sub_path.'/'.$new_file;

            }
            else
            {
                $this->error = '移动失败';
                return false;
            }
        }


    }

    public function getError()
    {
        return $this->error;
    }

}

t219.php

<?php
header('content-type:text/html;charset=utf-8');
//引入文件上传类
require_once ('./t218.php');
//声明图片路径
$pic_path = '';
//
if(isset($_POST['up_load']))
{
    //实例化
    $up_load1 = new upload();
    //上传
    if(!($pic_path = $up_load1->up($_FILES['pic'],'user_')))
    {
        echo $up_load1->getError();
    }

}
?>
<html>
<head>
    <meta charset="utf-8">
    <title>测试上传文件类</title>
</head>
<body>
<h2>编辑用户头像</h2>
<p>用户名:小明</p>
<p>现有头像:</p>
<img src="<?php echo './img/'.$pic_path;?>" onerror="this.src='./img/default.jpg'">
<form action="./t219.php" method="post" enctype="multipart/form-data">
    <p>上传头像:<input name="pic" type="file"></p>
    <p><input type="submit" name="up_load"></p>
</form>
</body>
</html>

 

AI实战-加拿大的工业产品价格指数数据集分析预测实例(含4个源代码+18.20 MB完整的数据集) 代码手工整理,无语法错误,可运行。 包括:4个代码,共38.64 KB;数据大小:1个文件共18.20 MB。 使用到的模块: numpy pandas os sklearn.model_selection.train_test_split tensorflow.keras.models.Sequential tensorflow.keras.layers.Dense sklearn.impute.KNNImputer sklearn.impute.IterativeImputer sklearn.linear_model.LinearRegression matplotlib.pyplot sklearn.datasets.make_blobs sklearn.cluster.DBSCAN sklearn.neighbors.LocalOutlierFactor sklearn.ensemble.IsolationForest sklearn.svm.OneClassSVM sklearn.preprocessing.MinMaxScaler sklearn.preprocessing.StandardScaler sklearn.preprocessing.MaxAbsScaler sklearn.preprocessing.RobustScaler sklearn.preprocessing.PowerTransformer sklearn.preprocessing.QuantileTransformer sklearn.preprocessing.OneHotEncoder sklearn.preprocessing.LabelEncoder category_encoders seaborn sklearn.cluster.KMeans sklearn.metrics.silhouette_score sklearn.decomposition.PCA sklearn.datasets.load_iris scipy.cluster.hierarchy.linkage scipy.cluster.hierarchy.dendrogram sklearn.cluster.AgglomerativeClustering sklearn.mixture.GaussianMixture matplotlib warnings sklearn.metrics.mean_squared_error sklearn.metrics.r2_score plotly.express sklearn.ensemble.RandomForestRegressor sklearn.ensemble.GradientBoostingRegressor catboost.CatBoostRegressor sklearn.metrics.mean_absolute_error sklearn.model_selection.RandomizedSearchCV statsmodels.tsa.arima.model.ARIMA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值