$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail image
you can read more on resizing images after uploading here.
$sql=$dbo->prepare("update mem_signup set profile_photo=:profile_photo
where mem_id=$_SESSION[mem_id] and userid='$_SESSION[userid]'");
$sql->bindParam(':profile_photo',$profile_file_name,PDO::PARAM_STR, 199);
if($sql->execute()){
echo "<br>Successfully updated Profile photo<br>";
echo "<img src=$tsrc>";
}// End of if profile is ok
else{
print_r($sql->errorInfo());
$msg=" <br>Database problem, please contact site admin <br>";
}
$profile_photo_path="../profile-photo/";
$count=$dbo->prepare("select profile_photo from mem_signup
where mem_id=:mem_id");
$count->bindParam(":mem_id",$_SESSION['mem_id'],PDO::PARAM_INT,1);
if($count->execute()){
$row = $count->fetch(PDO::FETCH_OBJ);
}else{
print_r($dbo->errorInfo());
}
echo "<FORM ENCTYPE=\"multipart/form-data\"
ACTION=\"profile-photock.php\" METHOD=POST>
<INPUT NAME=\"userfile\" TYPE=\"file\">
<INPUT TYPE=\"submit\" VALUE=\"Upload Photo\"></FORM>
";
if(strlen($row->profile_photo) > 1 ){
// Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$row->profile_photo;
echo "<img src=$tsrc>";
}
/////////////////////////////
$profile_photo_path="../profile-photo/";
// To display file name, temp name and file type ,
//use them for testing your script only//////
//echo "File Name: ".$_FILES['userfile']['name']."<br>";
//echo "tmp name: ".$_FILES['userfile']['tmp_name']."<br>";
//echo "File Type: ".$_FILES['userfile']['type']."<br>";
//echo "<br><br>";
///////////////////
// the path with the file name where the file will be stored.
$add=$profile_photo_path.$_FILES['userfile']['name'];
//echo $add;
if(move_uploaded_file ($_FILES['userfile']['tmp_name'],$add)){
//echo "<br>Successfully uploaded the image<br>";
chmod("$add",0777);
}else{echo "<br>Failed to upload file Contact Site admin to fix the problem<br>";
@unlink($add);
exit;}
/////////////////////////
if (!($_FILES['userfile']['type'] =="image/jpeg" OR $_FILES['userfile']['type']=="image/gif")){
echo "<br>Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
unlink($add);
exit;}
//////////////////
///////// Start the thumbnail generation//////////////
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail image
if($_FILES['userfile']['type']=="image/gif")
{
$im=ImageCreateFromGIF($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$profile_file_name=$_SESSION['mem_id'].".gif";
// Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$profile_file_name;
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
chmod("$tsrc",0777);
}////////// end of gif file thumb nail creation//////////
////////////// starting of JPG thumb nail creation//////////
if($_FILES['userfile']['type']=="image/jpeg"){
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$profile_file_name=$_SESSION['mem_id'].".jpg";
// Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$profile_file_name;
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
$sql=$dbo->prepare("update mem_signup set profile_photo=:profile_photo where mem_id=$_SESSION[mem_id] and userid='$_SESSION[userid]'");
$sql->bindParam(':profile_photo',$profile_file_name,PDO::PARAM_STR, 199);
if($sql->execute()){
echo "<br>Successfully updated Profile photo<br>";
echo "<img src=$tsrc>";
}// End of if profile is ok
else{
print_r($sql->errorInfo());
$msg=" <br>Database problem, please contact site admin <br>";
}
unlink($add);
echo $msg;
Php file upload ( single file )
Multiple file upload Photo gallery script with picture upload using PHP MySQL
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Chinedum | 07-03-2018 |
| Nice script. Please what does the $dbo stand for? | |