Page 1 of 1

picture upload script

Posted: Tue Nov 11, 2008 10:25 pm
by cap2cap10
Greetings php technorati:

Ok, I have a good photo upload script but I need to grab the new file name and extension for the photo and stick it into a Mysql table called image_name via sql UPDATE command. Here is the code:

Code: Select all

<?php
/*
 
- PHP5 Image upload script
- coded by kreoton
- downloaded from http://www.kreoton.net
 
*/
 
class imageupload
{
    //pblic variables
    public $path = '';
    public $errorStr = '';
    public $imgurl = '';
 
    //private variables
    private $_errors = array();
    private $_params = array();
    private $_lang = array();
    private $_maxsize = 1048576;
 
    public $_im_status = false;
 
    //public methods
    public function __construct ()
    {
        require 'config.php';
        $this->_types = $types;
        $this->_lang = $lang;
        $this->_upload_dir = $upload_dir;
        $this->_maxsize = $maxsize;
 
        $this->path = $PHP_SELF;
        
        if (is_array($_FILES['__upload']))
        {
            $this->_params = $_FILES['__upload'];
            if (function_exists('exif_imagetype'))
                $this->_doSafeUpload();
            else
                $this->_doUpload();
 
            if (count($this->_errors) > 0)
                $this->_errorMsg();
        }
    }
 
    public function allowTypes ()
    {
        $str = '';
        if (count($this->_types) > 0) {
            $str = 'Allowed types: (';
            $str .= implode(', ', $this->_types);
            $str .= ')';
        }
 
        return $str;
    }
 
    // private methods
    private function _doSafeUpload ()
    {
        preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
        if (exif_imagetype($this->_params['tmp_name']) && in_array(strtolower($matches[1]), $this->_types))
        {
            if ($this->_params['size'] > $this->_maxsize)
                $this->_errors[] = $this->_lang['E_SIZE'];
            else
                $this->_im_status = true;
 
            if ($this->_im_status == true)
            {
                $ext = substr($this->_params['name'], -4);
                $this->new_name = md5(time()).$ext;
 
                move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);
 
                $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.$this->new_name;
            }
        }
        else
            $this->_errors[] = $this->_lang['E_TYPE'];
    }
 
    private function _doUpload ()
    {
        preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
        if(in_array(strtolower($matches[1]), $this->_types))
        {
            if ($this->_params['size'] > $this->_maxsize)
                $this->_errors[] = $this->_lang['E_SIZE'];
            else
                $this->_im_status = true;
 
            if ($this->_im_status == true)
            {
                $ext = substr($this->_params['name'], -3);
                $this->new_name = md5(time()).$ext;
 
                move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);
 
                $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.'/'.$this->new_name;
            }
        }
        else
            $this->_errors[] = $this->_lang['E_TYPE'];
    }
 
    function _errorMsg()
    {
        $this->errorStr = implode('<br />', $this->_errors);
    }
}
 
?>
Can someone show me how and where to stick the sql statement and the variable(image_name) that represent the new file name and Extension.

Thanks in advance,

Batoe

Re: picture upload script

Posted: Tue Nov 11, 2008 11:15 pm
by infolock
This doesn't look like a complete class. functions are missing params, and some of the functions even look incomplete..

Re: picture upload script

Posted: Tue Nov 11, 2008 11:40 pm
by cap2cap10
There is another file with other parameters and the script actually works well but, it does not save the new image name to a mysql table. This variable is necessary since each user will have a different image name which allows me to use a simple php code to display the photo. Hence, what I need is to grab from the code the new name and extension once it is created,and then place it into a mysql table. As far as I can tell:

new_name = md5(time()).$ext;

This is the part of the code that reflects the new image name and extension.
Any takers?


Batoe

Re: picture upload script

Posted: Wed Nov 12, 2008 9:26 am
by infolock
need to see the other file then. please post it.

Re: picture upload script

Posted: Thu Nov 13, 2008 10:46 pm
by cap2cap10
Here is the other file associated with this script:

Code: Select all

<?php
 
/*
 
- PHP5 Image upload script
 
*/
 
$title = 'PHP5 Image Upload';
 
$types = array('jpg', 'gif', 'png');
$maxsize = 1024*1024;
 
$upload_dir = 'uploads/';
 
//language
$lang['E_TYPE'] = 'Wrong image type.';
$lang['E_SIZE'] = 'File size is not acceptable.';
 
?>
Ok can anyone show me where to stick the sql update statement with the image_name variable
note: image name can also be the whole url with the new file name and extension.

Thanks in advance,

Batoe