1

<?php
session_start();

<?php
session_start();
require('connect.php');
if(isset($_SESSION['username']))
{
$dir="image/";
$file_name=$dir.basename($_FILES['uploads']['name']);
$fileUpload=1;
$imageType=pathinfo($file_name,PATHINFO_EXTENSION);
 $image= addslashes(file_get_contents($_FILES['uploads']['tmp_name']));
   $image_name = addslashes($_FILES['uploads']['name']);
     $image_size = getimagesize($_FILES['uploads']['tmp_name']);
if(move_uploaded_file($_FILES['uploads']['tmp_name'], $file_name))
{
echo "uploaded succesfully" ;
echo "<img src='$file_name' width='50px' height='50px'>";
$querry="SELECT image FROM users WHERE username='$username'" or die(mysql_error());
$result=mysql_query($querry) or die(mysql_error());
$row=mysql_fetch_assoc($result) or die(mysql_error());
$oldimage=$row['image'];
unlink('directory/image/'.$oldimage);
/* $deleter = "DELETE FROM users WHERE image = '$oldimage'";
 if(mysql_query($deleter)) {
echo "Successful!";
 } */
if(!get_magic_quotes_gpc())
{

    $fileName = addslashes($file_name);
}
$sql="UPDATE users SET image='$fileName' WHERE image='$oldimage'";
$result=mysql_query($sql) or die(mysql_error());
if($result)
{
echo "created successfully";
echo "<br>";
echo "<a href='homeprofile.php'>Go back to home page</a>";
}
else
{
echo "cant create";
}
}
}
?>
2
Contributors
3
Replies
37
Views
2 Years
Discussion Span
Last Post by shashigowda
1

Thank You for Quering.... Let see below code and reply me

$existing_image = $r['image'];

if(isset($_FILES['uploads']) && ($_FILES['uploads']['size']>0)){
        if(trim($existing_image)!='No'){$photo=$existing_image;}
        else { $photo = date('dmY-his');
        $photo .="-".$_FILES['uploads']['name'];

        }
        move_uploaded_file($_FILES['uploads']['tmp_name'], 'directory/image/'.$photo);
        }
        else { $photo = $existing_image; }

I am using here mysqli not mysqli, after this set query:

$q = "update users set image='$photo'";
$result=mysqli_query($q) or die(mysqli_error());

Edited by alisajjad160

0

Even this code also working.

<?php
session_start();
require('connect.php');
if (isset($_SESSION['username'])){
$id=$_SESSION['username'];
$dir="image/";
$file_name=$dir.basename($_FILES['uploads']['name']);
$fileUpload=1;
$imageType=pathinfo($file_name,PATHINFO_EXTENSION);
    if(file_exists($file_name))
{
echo "file alredy exists,please rename and upload again";
$fileUpload=0;
}
if($imageType!='jpg' && $imageType!='gif' && $imageType!='png')
{
echo "image should bein jpg or gif or png format";
$fileUpload=0;
}
if($fileUpload!=1)
{
echo "please try again with valid document";

}
else
{

    $copied = move_uploaded_file($_FILES['uploads']['tmp_name'], $file_name);

    if ($copied) 
    {
        $sql = mysql_query("UPDATE users SET image='$file_name' WHERE username='$id'");
       echo "succesfull updated";
       echo "to go back home page<a href='homeprofile.php'>Click Here</a>";
    }
    else 
    {
        echo "There are An Errors In Uploading!";

    }
}

}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.