<?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";
}
}
}
?>

Dani AI

Generated

If your goal is to replace a user’s existing avatar safely, avoid matching by the old filename and always update by a stable key (e.g., username or user_id). Also, do not trust $_FILES['name']; generate your own filename, validate by MIME type, and use prepared statements. The outline below ensures you only commit the DB change if the upload succeeds, and then removes the previous file.

$mysqli->begin_transaction();

$username = $_SESSION['username']; // validate this exists
$stmt = $mysqli->prepare('SELECT image FROM users WHERE username = ? FOR UPDATE');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($oldImage);
$stmt->fetch();
$stmt->close();

if (!isset($_FILES['uploads']) || $_FILES['uploads']['error'] !== UPLOAD_ERR_OK) {
    throw new RuntimeException('Upload failed.');
}

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = $finfo->file($_FILES['uploads']['tmp_name']);
$map   = ['image/jpeg'=>'jpg','image/png'=>'png','image/gif'=>'gif'];
$ext   = $map[$mime] ?? null;
if (!$ext) { throw new RuntimeException('Invalid image type.'); }

$newBase = bin2hex(random_bytes(8)) . '.' . $ext;
$relPath = 'image/' . $newBase;
$absPath = __DIR__ . '/' . $relPath;

if (!move_uploaded_file($_FILES['uploads']['tmp_name'], $absPath)) {
    throw new RuntimeException('Could not move uploaded file.');
}

$upd = $mysqli->prepare('UPDATE users SET image = ? WHERE username = ?');
$upd->bind_param('ss', $relPath, $username);
$upd->execute();
$mysqli->commit();

if ($oldImage && is_file(__DIR__ . '/' . $oldImage)) { unlink(__DIR__ . '/' . $oldImage); }

Notes:

Recommended Answers

All 3 Replies

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());

ok.thank you

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!";

    }
}

}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.