search
Homephp教程php手册如何使用php封装类实现图片上传可直接引用

如何使用php封装类实现图片上传可直接引用

Jun 13, 2016 am 11:32 AM
classimagephpuploadusepicturehowFinishaccomplishencapsulationQuoteofkind

 

class image {

    /**
     *完成图片的上传
     *
     *@param array $file 待上传的文件信息的数组,用于5个元素的那个数组
     *@return mixed 如果执行成功,返回上传了的文件名,否则返回false
     */
    public function upload($file) {

        if($file['error'] == 0) {
            $allow_types = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif');
            if(in_array($file['type'], $allow_types)) {
                $maxsize = 2000000;
                if($file['size'] <= $maxsize) {
                    //上传
                    //需要将文件重命名,1,防止不规则的字符出现在文件名中,2,防止重名
                    //采用时间戳加随机数的形式
                    //后缀名如何获得?在原始文件名中获得后缀名
                    //在文件名中最后一个点截取到最后就是扩展名
                    //strrchr(在哪个字符串中查,查的字符串);
                    $new_filename = time() . mt_rand(10000, 99999) . strrchr($file['name'], '.');

                    //移动
                    //此函数返回移动成功还是失败
                    if(move_uploaded_file($file['tmp_name'],'images/'. $new_filename)) {
                        return $new_filename;
                    }
                }
            }
        }

        //只有一种情况返回文件名,其他全部返回false
        return false;
    }
}
?>

//-------------------------------------------------------------------------------------
<?php
header("content-type:text/html;charset=utf-8");
function __autoload($image){
        require_once($image.'.class.php');
}

    $image = new image();
    $user = $_POST['user'];
    $img = $_FILES['img'];
    //var_dump($img);
    $img = $image ->upload($img);
    mysql_connect('localhost','root','123');
    mysql_select_db('lyb');
    mysql_query('set names utf8');
    $q = "insert test_image(name,url) values('$user','$img')";
    //var_dump($q);
    $result = mysql_query($q);
                if($result){
                 
                   echo "添加成功.....<br /><br />";
                 }
                 else{
                   echo "添加失败。。。";
                 }
?>

//--------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片上传类</title>
</head>
<body>
<form enctype="multipart/form-data"  method="post" action="images.php">
姓名:<input type="text" name="user" id="user"/><br>
图片:<input type="file" name="img" id="img"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.