Page 1 of 1

problem with deleting files from folder

Posted: Thu Dec 24, 2009 3:25 pm
by spirala
I have list with files like this:

Code: Select all

$query= "SELECT file.fileID
            , file.NAME
            , file.size
            , file.create_date
            , file.user_ID 
            , file.br_downloads FROM file WHERE file.user_ID = '".mysql_real_escape_string($_SESSION['user_ID'])."' 
            AND name LIKE '%$trimmed%' ORDER BY $sort $sort_order limit $eu, $limit";
    $result=mysql_query($query);
    echo mysql_error();
    while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
    {
        echo "<tr align='left' bgcolor='$row[colourcode]'>";
        foreach ($rows as $data)
        {
            echo "<td align='left' font size='2' face='Verdana, Arial, Helvetica, sans-serif' color='#006699'>". $data . "</td>";
        } 
        echo "<td style='width: 300px'>
                <form method=post action='delete.php'> 
            <input name='delete' type='submit' value='Delete' style='width: 92px' /></td>
                </form>";
    }
As you can see there is a delete button and the code for deleting a file is wrong. Here is the code:

Code: Select all

if (isset($_GET['name']))
{
    $fileID=$_GET['name'];//id-to na file-ot
    $username=$_SESSION['user_ID'];
    
    //proveruvame dali fajlot e downloaduvan
    $sql="SELECT br_downloads FROM file WHERE fileID='$fileID'";
    $download=@mysql_query($sql);
    $counter=@mysql_fetch_array($download,MYSQL_ASSOC);
    // ako ne e downloaduvan dozvoli da se izbrise
    if ($counter['br_downloads'] == 0)
    { 
        $counter=$counter['counter'];
        //za da ja dobijam ekstenzijata 
        $query="SELECT * FROM file WHERE fileID='".$fileID."'";
        $result=mysql_query($query);
        $name=mysql_fetch_array($result, MYSQL_ASSOC);
        $filename = $name['name']; 
        $ext = substr($filename, strrpos($filename, '.') + 1);
        $path="C:/Documents and Settings/PC/My Documents/Visual Studio 2008/Projects/filemanager/filemanager/uploads/";
        $filepath=$path.$fileID/*.".".$ext*/; 
        
        // potoa od baza gi brisam podatocite
        $sql="DELETE FROM file WHERE fileID='".$fileID."'";
        $result=@mysql_query($sql);
        
        // go brisam fajlot od folderot
        if(unlink($filepath))
        {
            $success="You deleted the file successfully."; 
        }
    }
    else
    {
        $message="<tr colspan='4'><td>You can not delete this file.</td></tr>"; 
        header("Location: my_profile.php?$message");
    }
}
Can you please help me. Am I missing somethig?? The file has to be deleted from the uploads folder
Thanks in advance

Re: problem with deleting files from folder

Posted: Thu Dec 24, 2009 3:46 pm
by califdon
Your first script has a form that declares that the method to be used is POST, but it contains nothing but a Submit button. Your second script is looking for a $GET variable, which is looking for the variable in the URL string. So your 2 scripts are not communicating with each other. What is your intention? Do you want the user to select one of the files listed there, or is the purpose to delete ALL of the files listed? I can't really tell from your code what you want to do.

Re: problem with deleting files from folder

Posted: Thu Dec 24, 2009 4:13 pm
by spirala
my intention is to delete one of the files in the list and I don't know how to do it. Actually i tried something, but I am not sure how to access to the file a want to delete and to connect the 2 scripts

Re: problem with deleting files from folder

Posted: Thu Dec 24, 2009 5:14 pm
by califdon
OK, then you need a totally different approach. The way I would do it is like this: no change to the first 10 lines of your first script. Then:

Code: Select all

 
     while ($row = mysql_fetch_assoc($result))
     {
         extract($row);
         echo "<tr><td>$fileID</td><td>$NAME</td><td>$size</td><td>$create_date</td>";
         echo "<td>$user_ID</td><td>$br_downloads</td><td><input type='button' value='DELETE' ";
         echo "onClick='document.location=\"delete.php?ID=$fileID\";'></td></tr>";
     }
That's the end of the logic in the first script. That will display the data from each file selected, plus a button that says "DELETE" and when it is clicked next to one of the entries, it will call the delete.php script with the fileID of that file as the $_GET parameter, ID.

For the second script, all you need is:

Code: Select all

// your MySQL connect code
 
 if (isset($_GET['ID']))
 {
     $fileID=mysql_real_escape_string($_GET['ID']);
     $sql="DELETE FROM file WHERE fileID='$fileID'";
     mysql_query($sql) or die(mysql_error());
     echo "File was successfully deleted."
  }

Re: problem with deleting files from folder

Posted: Fri Dec 25, 2009 2:22 am
by spirala
thanks a lot. it works :) thanks a million :)

regards