Professional Web Development Forums: Image Upload script - PHP (Stores images in Mysql DB)

Jump to content

Image Upload script - PHP (Stores images in Mysql DB) Bookmark

User is offline splatcatballa99 

  • Member
  • Group: Members
  • Posts: 42
  • Joined: 10-September 08

Find Posts Tutorial info

  • Added on: 03 April 2010 - 05:28 AM
  • Date Updated: 03 April 2010 - 05:35 AM
  • Views: 1,944
I made this script for a site and figured i'd share it with you guys.


Upload Page - Name upload.php

<div align="center">

<b>Image Uploader</b>

    <form action="upload2.php" method="post" enctype="multipart/form-data">

    Filename:

    <input type="file" name="file" id="file"  onfocus="highlight('file')" onblur="dhighlight(file)" />

    <input type="submit" name="submit" value="Submit" />
<br/>

    Allowed image types: .jpeg, .jpg, .gif, .png, .bmp


    The maximum upload size is: 2 megabytes.

</div>

Upload Page 2 - Name upload2.php

<?php

include('img_class.php');

    // check if a file was submitted

$img =new image();

$img->max_size ="2097000";

$img->allowed =array("image/jpeg", "image/jpg", "image/png", "image/bmp", "image/gif");

$img->image = $_FILES['file'];



    if(isset($_FILES['file'])) {

        if($img->chckimgs()) {

            if($img->insert_img()) {

            } else {

            echo "There was an unkown error!";

            }

        

        } else {

        echo "there was an error";

        }

    } else {

        echo "Please select a file to upload!";

        }

   

?>

Image upload class - Name img_class.php(Add your mysql info in this file)

<?php

// mysql connection info here




class image{

    public $image;

    public $max_size;

    public $allowed;

    

    public function chckimgs() {

        $e =1; // true

        

          if($this->image['size'] > $this->max_size) {

            $e =2; // false

        }  

        

        if (($this->image["type"] == "image/gif")

            || ($this->image["type"] == "image/jpeg")

            || ($this->image["type"] == "image/jpg")

            || ($this->image["type"] == "image/bmp")

            || ($this->image["type"] == "image/gif")

            || ($this->image["type"] == "image/png")

            || ($this->image["type"] == "text/plain")) {} else { $e=2;}


        if($e ==2) { return false; } else { return true;}

    }

    

    public function insert_img() {

        $data =addslashes(file_get_contents($this->image['tmp_name']));

        $size = getimagesize($this->image['tmp_name']);

        $id =$this->generateId(8);

        $time =time();

        $type =$size['mime'];

        $name =$this->image['name'];

        

        

        $query =mysql_query("INSERT INTO images (id, type, image, size, name, time) VALUES('$id', '$type', '$data', '$size', '$name', '$time')") or die(mysql_error());

        

         if(!$query) {

            return false;

            } else {

                echo "Your image was uploaded succesfully! You can view it at the following url...
";

                echo "<a href="image.php?id=".$id."">http://www.kraklak.com/image.php?id=".$id."</a>";

            return true;

            }

    }

    

    public function load_img($id) {

        $query =mysql_query("SELECT type, image FROM images WHERE id='$id' LIMIT 1");

        return mysql_fetch_assoc($query);

    }

    

    function generateId($length = 8) {

        $password = "";

        $possible = "0123456789bcdfghjkmnpqrstvwxyz"; 

        $i = 0; 

        while ($i < $length) { 

            $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);

            if (!strstr($password, $char)) { 

                $password .= $char;

                $i++;

            }

        }

        return $password;

    }


}

?>

Run this command in PHPmyadmin or root

CREATE TABLE IF NOT EXISTS `images` (

  `id` varchar(15) NOT NULL,

  `type` varchar(25) NOT NULL,

  `image` blob NOT NULL,

  `size` varchar(25) NOT NULL,

  `ctgy` varchar(25) NOT NULL,

  `name` varchar(50) NOT NULL,

  `time` varchar(30) NOT NULL,

  UNIQUE KEY `id` (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1;




NOTE: I'm not responsible for any damages this may cause your site or anything bad that happens. It's open source so feel free to edit it or change it as you feel.
0

Share:

Powered by (IM) Tutorials 1.1.0 © 2010, by Michael McCune

Enter your sign in name and password


Sign in options
  Or sign in with these services