File uploading in PHP code

<?php
// upload file into folder 

#################### code for Create Thumb of Image ###############function make_thumb($src,$dest,$desired_width,$desired_height="188"){
  
/* read the source image */
  
$ext substr(strrchr($src'.'), 1);
  if(
$ext!='gif'){
  
$source_image imagecreatefromjpeg($src);
  } else {
   
$source_image imagecreatefromgif($src);
  }
  
$width imagesx($source_image);
  
$height imagesy($source_image);
  
//  $desired_height = "188";//floor($height*($desired_width/$width));
  
$virtual_image imagecreatetruecolor($desired_width,$desired_height);
  
/* copy source image at a resized size */
  
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
  
/* create the physical thumbnail image to its destination */
   
if($ext!='gif'){
 
imagegif($virtual_image,$dest,100);
  } else {
  
imagejpeg($virtual_image,$dest,100);
  }
  } 
############################## File Upload Code for Single file / image #####################
if(isset($_FILES["txtfile"]) && $_FILES["txtfile"]["name"]!=""){  // if file set 
$ext substr(strrchr($_FILES["txtfile"]['name'], '.'), 1);  // get extinction of file name $path 'demo / ' .uniqid(); // file path where you want to upload file  demo is folder name $url $path.'.'.$ext ;  // file url path
 
if (file_exists($url)){ // if file already exist 
        
unlink($url); // delete old file 
    
}

    move_uploaded_file($_FILES["txtfile"]["tmp_name"], $url); // upload file 
$id mysql_insert_id(); // get last insert id for update path $type substr($_FILES['txtfile']['type'],0,5) ; // know if file type is image if($type== 'image'){$thumburl=$path.'thumb_'.'.'.$ext;make_thumb($url,$thumburl,"150","150");  // make or resize of image }mysql_query('update tablename SET imageurl = $url , thumburl = $thumburl  WHERE id = $id');

}?>

<?php 
####################  upload Multiple Image / File in PHP ########

//print_r($_FILES['txtfile']);$c=count($_FILES['txtfile']);
for(
$i=$i<$c $i++){ 
if(isset(
$_FILES['txtfile']) && $_FILES['txtfile']["name"][$i]!=""){ $ext substr(strrchr($_FILES['txtfile']['name'][$i], '.'), 1);$path 'demo / ' .uniqid();$url $path.'.'.$ext ;
 if (
file_exists($url)){
        
unlink($url);
    }

    move_uploaded_file($_FILES['txtfile']["tmp_name"][$i], $url); // upload file 
$id mysql_insert_id();$type substr($_FILES['txtfile']['type'][$i],0,5) ;
if(
$type== 'image'){$thumburl=$path.'thumb_'.'.'.$ext;make_thumb($url,$thumburl,"150","150"); // pass with and height }mysql_query('update tablename SET imageurl = $url , thumburl = $thumburl  WHERE id = $id');

}

}?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s