Go Back   DriverHeaven > Forums > Software / Tools > Programming, Coding, (Web)Design
Register Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old Apr 17, 2005, 06:13 PM   #1 (permalink)
germanjulian
Back in London
 
germanjulian's Avatar
 
Join Date: Jul 2003
Location: London
Posts: 1,794
germanjulian is on a distinguished road

php jpeg upload script of doom!!!!

well i nearly done my dissertation now 2 more days of hard writting. Maybe another 40 pages or so But I thought you might wanna check out my upload jpeg upload script cause i love it:
oh you can obviously upload several images at ones cause I am using an array for the html input: <input type='file' name='images[]' /> see here images is an array.

PHP Code:
<?php
session_start
();
require_once(
'../image_fns.php');
$conn db_connect();

while(list(
$key,$value) = each($_FILES[images][name])) //Loops through the uploaded array of Images
{
    if(!empty(
$value))// this will check if any blank field is entered
    
{   
        if(
$_FILES[images][type][$key] == "image/jpeg"//This checks that the uploaded image is a jpeg file
        
{
                
$filename $value;    // filename stores the value
                
$userdir $_SESSION['valid_user']; //The user who currently is logged in
                
$scrUserName $userdir;
                
$uploaddir 'images/'.$userdir.'/';   // upload directory path is set
                
@mkdir ($uploaddir,666); //Create directory
                
$randomnumber =rand(); //create random number
                
$srcFilename $_FILES[images][name][$key]; //name of Image
                
$srcName $randomnumber.$_FILES[images][name][$key]; //new name of image with random number in front of it
                
$uploadedfile $uploaddir $srcName;  //the path and name of the uploaded file
                
$src $uploadedfile;
                if (
copy($_FILES[images][tmp_name][$key], $uploadedfile))  //  upload the file to the server and if successfull
                
{
                    
$thuName $srcFilename"thumb.jpg"//create thumbnail name
                    
$desttemp $uploadedfile "thumb.jpg"//thumbnail directory and name.
                    
$dest $desttemp;
                    
//Set some variables for thumb creation
                    
$maxWidth "180";
                    
$maxHeight "180";
                    
$quality "90";
                    
                        if (
file_exists($src) && isset($dest))  // if the uploaded image was copied to the user image directory
                         
{
                                
// Returns information about a file path
                                
$destInfo pathInfo($dest); 
                                
// Put information into variables for DB insert
                                //Gets the image dimensions its an array
                                
$srcSize getImageSize($src);    
                                
//with and height
                                
$srcHeight $srcSize[1];
                                
$srcWidth $srcSize[0];
                                
//todays date
                                
$srcUploaddate date("Y-m-d");
                                
//Gets image size in bytes    
                                
$srcBytes filesize($src);
                                
$srcPath $src;
                                
                                
$srcSize getImageSize($src);                    
                                
// $destSize[0] = width, $destSize[1] = height
                                
$srcRatio $srcSize[0] / $srcSize[1]; // width/height ratio
                                
$destRatio $maxWidth $maxHeight;
                                
//Maths for thumb creation
                                
if ($destRatio $srcRatio) {
                                    
$destSize[1] = $maxHeight;
                                    
$destSize[0] = $maxHeight $srcRatio;
                                } else {
                                    
$destSize[0] = $maxWidth;
                                    
$destSize[1] = $maxWidth $srcRatio;
                                } 
                                
//Create new truecolour image
                                
$destImage imagecreatetruecolor($destSize[0], $destSize[1]);
                                
//Make sure the new image is antialiased
                                
imageantialias($destImagetrue);     
                                
//Created a new Image from the source                             
                                
$srcImage imagecreatefromjpeg($src);
                                
// Copy and resize part of an image with resampling (create thumbnail)
                                
imagecopyresampled($destImage$srcImage0000$destSize[0], $destSize[1], $srcSize[0], $srcSize[1]); 
                                
// Output image to file
                                
imagejpeg($destImage$dest$quality);
                                
$thuPath $dest;
                                
$result $conn->query("insert into Image (User_Name,Image_Size,Image_DisplayName,Image_Name,Image_Path,Image_UploadDate,Image_Thumbpath,Image_ThumbName,Image_Height,Image_Width)
                                                                     values('$scrUserName',$srcBytes,'$srcFilename', '$srcName','$src','$srcUploaddate','$thuPath','$thuName',$srcHeight,$srcWidth)"
);
                                  if (!
$result)
                                  echo 
'Could not store images in database - please try again later.';
                                            
                        } 
                
                }
        }
           
    }
}
$_SESSION['UPLOADED']=true;
header("Location: http://localhost/dissertation/functions/image/index_new.php"); 
?>
__________________
/|\ Asus P5W DH Deluxe, Intel C2D E6600, 2GB Corsair XMS2-6400C4 DDR2, E-VGA GeForce 7800 GT, Creative X-Fi Extreme Music, 500GB Seagate 7200.10 SATA, Lian Li PC-V1100 Aluminum Case Black, etc. http://germanjulian.com /|\
germanjulian is offline   Reply With Quote
Reply



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

 
Powered by: vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
Artwork by Allan 'Zardon' Campbell, vBulletin implementation by Craig '5320' Humphreys based on original artwork by Ratchet.

All times are GMT -5. The time now is 06:31 AM. Copyright ©2008 DriverHeaven.net