0

I am trying to develop a user page for a forum and I'm kinda struggling with the image upload. The problem is that I would like to limit the user to only be able to upload one single image, but be able to change it anytime. so basically, I would like to either overwrite the existing file either delete the old picture and add a new one instead. At this point I have a piece of code that adds a timestamp at the end of the file (which I don't really need actually).

CODE:

if(isset($_POST['upload']))
{

$extension=strstr($_FILES['uploadedfile']['name'], ".");
    $filename = "_/userfiles/userpics/".basename($_FILES['uploadedfile']['name'], 
$extension);
    $target = "_/userfiles/userpics/".basename($_FILES['uploadedfile']['name']);

$valid = true;  
if(file_exists($target))
{

    $filename = $filename . time();
    $target = $filename . $extension;
}
if($valid)
{
    // move the file into the folder of our choise
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target);       
    $img_sql = "INSERT INTO sp_userimage (imageid, path, id) value ('', '".$target."', '".$_SESSION['userid']."')";
    $img_result = mysql_query($img_sql);
    echo "upload sucessfull";
}
1
  • 1
    So you want to delete a file? Search for unlink. Commented Sep 12, 2013 at 10:18

6 Answers 6

2

Make use of unlink() in PHP Manual.

if(file_exists($target))
{
    unlink($target); // deletes file
    //$filename = $filename . time();
    //$target = $filename . $extension;
}
Sign up to request clarification or add additional context in comments.

3 Comments

doesnt that only search for the filename? the new file could have a different name tho..
for example, if the first picture is "cat.jpg" and the second is "cat02.jpg" it wont delete anything, because that filename does not exist already.
Whatever filename you pass inside the unlink gets deleted. Idea will be unlink your old file and upload the one the user has currently uploaded.
0

I think this might be a bit better suited for you. You might have to edit it a tad.

if($valid)
{
    // Check if user has a file.

    $img_check = mysql_query("SELECT * FROM sp_userimage WHERE id = " . (int) $_SESION['user_id']);
    if( mysql_num_rows($img_check) > 0 ){
       $row = mysql_fetch_object($img_check);

       // Delete the file.
       unlink($row->path);
    }

// move the file into the folder of our choise
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target);       
$img_sql = "INSERT INTO sp_userimage (imageid, path, id) value ('', '".$target."', '".$_SESSION['userid']."')";
$img_result = mysql_query($img_sql);
echo "upload sucessfull";

}

Comments

0

It might be easier to normalize the image type (e.g. only jpegs) and then name the file as the userid. For example:

$target = 'userpics' . DIRECTORY_SEPARATOR . $_SESSION['userid'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target);

This will simply overwrite the old picture with the new one. Given that this type of filename is deterministic, you also don't need to store the filename in the database.

Comments

0

Use unlink() function

read more here PHP unlink

okay ,if u want to delete the file for that particular user only.

then store the filename vs user in some MapTable in db.

mysql_query("CREATE TABLE t_usr_file_map( usr_id INT NOT NULL , file_name VARCHAR(100), )") or die(mysql_error());

and at the time of reupload , fetch the filename from the table for that user , unlink it and reupload the fresh one again.

OR,

or u can use PHP file_rename function at the time of upload. rename filename to the userid

rename ( string $oldname , string $newname [, resource $context ] )

and u can always do unlink based on user-id

Comments

0

Its very simple by unlink()

as:

unlink(dirname(__FILE__) . "/../../public_files/" . $filename);

Comments

0
if (file_exists($path))
{
  $filename= rand(1,99).$filename; 
  unlink($oldfile);
}
move_uploaded_file($_FILES['file']['tmp_name'],$filename);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.