Jump to content

Recommended Posts

This script is for uploading profile pictures and it works perfectly but when i want to change my current profile picture it doesnt overwrite my image in FTP but does overwrite it in my database, ive tried everything but nothing seems to work,  :shrug: please help.

 

<?php

session_start();

include("checklogin.php");

$_SESSION['myusername'];

$username = $_SESSION['myusername'];

if ($_POST['submit'])
{

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{
    //start upload process

$location = "./profileimages/$name";
        move_uploaded_file($tmp_name,$location);
        $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error());

die("Your profile picture have been upload! <a href='profile.php'>Profile</a>");



}
else
die("Please select a file!");




}

echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>";

echo "Upload your image:

<form action='upload.php' method='POST' enctype='multipart/form-data'>
  File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'>
</form>

";

?>

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/
Share on other sites

This script is for uploading profile pictures and it works perfectly but when i want to change my current profile picture it doesnt overwrite my image in FTP but does overwrite it in my database, ive tried everything but nothing seems to work,  :shrug: please help.

 

<?php

session_start();

include("checklogin.php");

$_SESSION['myusername'];

$username = $_SESSION['myusername'];

if ($_POST['submit'])
{

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{
    //start upload process

$location = "./profileimages/$name";
        move_uploaded_file($tmp_name,$location);
        $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error());

die("Your profile picture have been upload! <a href='profile.php'>Profile</a>");



}
else
die("Please select a file!");




}

echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>";

echo "Upload your image:

<form action='upload.php' method='POST' enctype='multipart/form-data'>
  File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'>
</form>

";

?>

Do one thing delete the old picture using unlink function, and upload the new one

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888982
Share on other sites

do i insert that before, "//get file attributes"?

first check whether user has changed the image or not while updating their profile. This you can do by comparing filename from the database and the filename that comes from the $_POST var. if its same you dont need to replace, if its not, then insert the code before //get file attributes.

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888993
Share on other sites

do i insert that before, "//get file attributes"?

first check whether user has changed the image or not while updating their profile. This you can do by comparing filename from the database and the filename that comes from the $_POST var. if its same you dont need to replace, if its not, then insert the code before //get file attributes.

or to check whether user has change their profile image or not, if file field is empty then probably they havent changed their picture so you dont need to update picture field and delete it. if its not empty you need to delete as well as update database

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888998
Share on other sites

i cant get my head around this one, im quite new to php

 

if($_FILES['myfile']['name']!=""){
//this means user has browsed for new picture to upload
$old_picture_path="/images/user1.jpg"; //change your path
unlink($old_picture_path);
//then do upload of new picture and update database with new picture location
}
//get file attributes
// your codes

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889006
Share on other sites

error

<?php

session_start();

include("checklogin.php");

$_SESSION['myusername'];

$username = $_SESSION['myusername'];

if($_FILES['myfile']['name']!=""){
//this means user has browsed for new picture to upload
$old_picture_path="/images/user1.jpg"; //change your path
unlink($old_picture_path);
//then do upload of new picture and update database with new picture location
}

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{
    //start upload process

$location = "./profileimages/$name";
        move_uploaded_file($tmp_name,$location);
        $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error());

die("Your profile picture have been upload! <a href='profile.php'>Profile</a>");



}
else
die("Please select a file!");




}

echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>";

echo "Upload your image:

<form action='upload.php' method='POST' enctype='multipart/form-data'>
  File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'>
</form>

";

?>

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889009
Share on other sites

error

 

what error you get??

 

anyway here is your code again, i have modified it

<?php

session_start();

include("checklogin.php");

$_SESSION['myusername'];

$username = $_SESSION['myusername'];

if ($_POST['submit'])
{

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{
    //start upload process
   /*in database just save image name dont save whole location of the image, your path remains same always, just the imagename changes and wherever you are showing image then fetch image name from database and use the location which you already know.
  write a query to get image name from the table for the user $username. say you have imagename in variable $oldimage*/
   $old_picture_path="./profileimages/".$oldimage; 
   unlink($old_picture_path);
//new image 
   $location = "./profileimages/$name";
        move_uploaded_file($tmp_name,$location);
        $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error());
   
   die("Your profile picture have been upload! <a href='profile.php'>Profile</a>");
   
   
   
}
else
die("Please select a file!");




}

echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>";

echo "Upload your image:

<form action='upload.php' method='POST' enctype='multipart/form-data'>
  File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'>
</form>

";

?>

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889019
Share on other sites

this is how i am viewing the image but the old image still didnt get deleted from FTP

<?php

session_start();

include("checklogin.php");

$username = $_SESSION['myusername'];

$query = mysql_query("SELECT * FROM members WHERE username='$username'");
if (mysql_num_rows($query)==0){
   die("User not found!");
}else{
   $row = mysql_fetch_assoc($query);
   $location = $row['imagelocation'];
   if($location == ""){
      echo "<img src ='/profileimages/box.png' width='225' height='275' border='0'></a>";
   }else{
      echo "<img src ='$location' width='225' height='275' border='0'>";
   }
}


?>

Link to comment
https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889031
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.