<?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 / ' .time(); // 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');
}
?>
<form action="demo.php" enctype="multipart/form-data" method="post">
<input type="file" name="txtfile" />
<input type="submit" />
<?php
#################### upload Multiple Image / File in PHP
how to upload multiple images in php
########
//print_r($_FILES['txtfile']);
$c=count($_FILES['txtfile']['
tmp_name']);
for($i=0 ; $i<$c ; $i++){
if(isset($_FILES['txtfile']) && $_FILES['txtfile']["name"][$i]!=""){
$ext = substr(strrchr($_FILES['txtfile']['name'][$i], '.'), 1);
$path = 'demo / ' .
time();
$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');
}
}
?>
<form action="demo.php" enctype="multipart/form-data" method="post">
<input type="file" name="txtfile[]" multiple />
<input type="submit" />
Finally I found which i'm looking for!
ReplyDeleteThanks you so much!
W3ondemand