Jump to content

Removing a file using php


rxbanditboy1112

Recommended Posts

Hi,
I have been trying to use php to remove files from my server. I have created a photo gallery upload system. Now i am trying to remove multiple photos at a time using a checkbox.
I believe i need to use an array to store id numbers of things that are checked. Also not only do i want to remove the entries in mysql but i want to delete the actual photos also. I have been trying to use the unlink() function to delete files, but it gives me an error and tells me that the function does not exist.
Something like that...
Am i missing something? Is there a better way to do this?
Thanks!!!
Link to comment
https://forums.phpfreaks.com/topic/34915-removing-a-file-using-php/
Share on other sites

Well now it doesn't give me an error, but it doesn't remove the database entry or the picture.

case "perform":
$sql = mysql_query("SELECT *
        FROM gallery_photos
        WHERE photo_category = $_POST[category_id]");
$checked=$_POST[photos_tobedeleted];

for ($i=0; $i < $_POST[number_of_rows]; $i++)
{
if ($checked[$i])
{
$deleting_file_name = mysql_result(mysql_query("SELECT photo_filename FROM gallery_photos WHERE photo_id='$checked[$i]'"),0);
unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/".$deleteing_file_name."');
$sql2 = mysql_query("DELETE FROM gallery_photos WHERE photo_id='$checked[$i]'");
           
      if(!$sql2){
        echo 'Error performing DELETE query: '.
              mysql_error();
      }
  }
}

photos_tobedeleted are the name of the check boxes (but im not sure if i did that right).
What I mean... (less resources, not looping)

this...

[code]$checked=$_POST[photos_tobedeleted];

for ($i=0; $i < $_POST[number_of_rows]; $i++)
{
  if ($checked[$i])
  {
  $deleting_file_name = mysql_result(mysql_query("SELECT photo_filename FROM gallery_photos WHERE photo_id='$checked[$i]'"),0);
  unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/".$deleteing_file_name."');
  $sql2 = mysql_query("DELETE FROM gallery_photos WHERE photo_id='$checked[$i]'");
           
      if(!$sql2){
        echo 'Error performing DELETE query: '.
              mysql_error();
      }
    }
}[/code]


could be

[code]if ( ! empty ( $_POST['photos_tobedeleted'] ) )
{
$photos = array_diff ( array_map ( 'intval', $_POST['photos_tobedeleted'] ), array ( 0 ) );

if ( ! empty ( $photos ) )
{
$query = mysql_query ( "SELECT photo_filename FROM gallery_photos WHERE photo_id IN('" . implode ( "', '", $photos ) . "');" );

if ( mysql_num_rows ( $query ) > 0 )
{
While ( $r = mysql_fetch_assoc ( $query ) )
{
@unlink ( $_SERVER['DOCUMENT_ROOT'] . '/fileupload/' . $r['photo_filename'] );
}

mysql_query ( "DELETE FROM gallery_photos WHERE photo_id IN('" . implode ( "', '", $photos ) . "');" );
}
}
}[/code]


printf
[quote author=rxbanditboy1112 link=topic=123173.msg508818#msg508818 date=1169241377]
Also i think im setting the checkboxes up incorrectly...

<input type="checkbox" name="photos_tobedeleted[]" value="<? echo $photo_id;?>" />

for every row it'll display that. Should it instead be photos_tobedelete[$i] ?
[/quote]
AFAIK the way you already have it, is the correct way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.