delete from folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

delete from folder

Post by chris_s_22 »

This is the code i use to upload a photo to a folder and put file name in database.

However if someone was to re use the form to change photo id like it so that the old one was deleted
How do i do this

Code: Select all

<?php 
include 'Connect.php';
 
if(!isset($_POST[submit])) // checks that the data being recieved came from a POST variable named 'submit'  
{
     // if error reshow the form You cannot access this page directly.
     include 'index.php';
     exit;
}
else
{
    if ((($_FILES["photo"]["type"] == "image/gif") 
    || ($_FILES["photo"]["type"] == "image/jpeg") 
    || ($_FILES["photo"]["type"] == "image/pjpeg"))
    && ($_FILES["photo"]["size"] < 51000))
    {
 
        $ext = findexts ($_FILES['photo']['name']) ; //This applies the function to our file 
        $ran = rand () ;//This line assigns a random number to a variable.
        $ran2 = $ran."."; //adds a . on the end of $ran 
        $target = "images/"; //This is the directory where images will be saved
        $pic = $ran2.$ext;//This gets information from the form that has since been randomised and checked
        $target = $target . $ran2.$ext;//This combines the directory, the random file name, and the extension
        //If everything is ok we try to upload it 
 
        //Writes the information to the database 
        mysql_query("UPDATE members SET photo = '$pic' WHERE username = '$username'") ;
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
        {
        header('Location: index.php');   
        } 
        else 
        { 
        echo "Sorry, there was a problem uploading your file."; 
        }  
    }
    else
    {
    echo "Invalid file";
    }
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: delete from folder

Post by Christopher »

You would need to do a "SELECT * FROM members WHERE photo = '$pic' AND username = '$username'" to check if the user already has that photo. No need to fetch the record, just check if there are more than zero records in the result set.
(#10850)
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: delete from folder

Post by AlanG »

You will have to select the photo's filename from the database and use the unlink function to delete it. You can then proceed with a new photo upload.

e.g.

Code: Select all

unlink('/path/to/photo/' . $filename);
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: delete from folder

Post by chris_s_22 »

thanks alanG exactly what i needed
Post Reply