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
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).
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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.
Link to comment
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.