Jump to content

[SOLVED] Need help with this scrip to delete images from a folder in server


Recommended Posts

I'm having issues deleting some images with this code i created. THe point of the script is to delete a certain image from the server. What i did was created a form, in it all the images that were on the server appear, followed by a delete button. When that delete button is pressed the image is supposed to be deleted. Hwoever thats not the case. What i keep getting is this:

 

Warning: unlink(images/gallery/slideshow/) [function.unlink]: Is a directory in /home2/socalcom/public_html/yourimageisart/deleteImage.php on line 11

The file images/gallery/slideshow/ could not be deleted.

 

I know what it means. What i don't know is what i'm doing wrong on the script.

 

Anyway here is the script:

<?php
$images = "images/gallery/slideshow/"; # Location of small versions 
$height = 75; //height for each thumbnail
$width = 75; //the width for each thumbnail

if ($handle = opendir($images)) { 
    while (false !== ($file = readdir($handle))) { 
         if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { 
              $files[] = $file; 
               } 
         } 
closedir($handle); 
} 

echo'<form method="post" action="deleteImage.php" enctype="multipart/form-data">';
        echo'<ul>';
foreach($files as $file) 
{ 
        echo '
        <li>
 	     <input type="image" name="imagefile" src="'.$images.$file.'" height="'.$height.'" width="'.$width.'" /><br/>
	     <input type="submit" name="submit" value="Delete"/>
	</li>
	'; 
        $colCtr++; 
}		 
        echo'</ul>'."\r\n";
echo'</form>';
?>

 

Thats the code that opens the directory, and shows the items in a form

 

The code below is the deleteImage.php code that supposed to delete the selected image

<?php
$path="images/gallery/slideshow/"; //path of images
$image = $_POST['imagefile'];
$file = $path . $image;

        if(file_exists($file))
       {
            if(unlink($file))
            {	
                echo "The file <i>".$file."</i> has been deleted.";
            }
            else 
            {
                  echo "The file <i>".$file."</i> could not be deleted.";	
            }	
      }
      else 
      {
            echo "The file <i>".$file."</i> could not be found.";	
      }
?>

 

Anyway i figured putting the whole script might help. If anyone can shed some light on what i'm doing wrong please!

 

You need to re-think your logic.

 

1) if there is more than one file in the directory, you will have multiple INPUT's named "imagefile" and multiple INPUT's named "submit".  The way it is written, you have NO WAY to tell WHICH button was clicked or which file you should delete.

 

2) INPUT TYPE=IMAGE is basically a submit button with your own image on it.  So it should not (IMHO) get POSTed unless you actually click on it, although some browsers may POST it whether you click it or not.  If it is getting posted, then all of them are getting posted (see #1 above) and PHP is just getting the last one sent by the browser (whichever that is).  But there is no VALUE attribute, so I guess it is coming to PHP as an empty value.  (Use print_r($_POST) to see what you are actually getting).

 

Your delete script is picking up an empty value from $_POST['imagefile'].  The file_exists() function returns true because $file only contains the directory name and the directory exists (a directory is just a special type of file), and so your script is trying to unlink() the directory.

 

The easiest way to make this code work, would be to make each list entry a separate form, and add a hidden field to it that contains the filename you want to delete.  I don't know how many files are in your directory, and I don't know if there is any real world limit on the number of forms on a page (I would guess that some browser will choke at some point).

 

I'm not sure of any other way to do this using a separate button for each file.  But, you might be able to use a checkbox for each file, setting the name to an array and the value to the filename; then have a single delete button at the end of the form. 

<INPUT type="checkbox" name="chkFiles[]" value="$file">

You will end up with an array $_POST['chkFiles'][#] = filename_to_be_deleted which you can walk through and delete each file.  You may have to urlencode() and urldecode() the filenames, maybe?

Thanks for the reply

 

You were right, i had allt he files under one form. I hadn't realized that at first it took me a while to finally realize my first mistake which was to put each file with their form.

 

My second mistake which you are right also, was that i had no values. I ended up just adding a hidden value.

 

Although, that was my other area that i got stuck now, having them just delete the files with checkboxes like you said.

 

I mean i know i would have to add a checkbox at the bottom of each image, and then a single delete button, but would i put them all under just one form? like

 

<form method="#" action="#">
   <li>Image<br/>
   <Input type=checkbox name="chkFiles[]" value="$file"/>
   </li>
<li>Image<br/>
   <Input type=checkbox name="chkFiles[]" value="$file"/>
   </li>
<li>Image<br/>
   <Input type=checkbox name="chkFiles[]" value="$file"/>
   </li>
....more files
</form>

 

Would i do that way then?

 

and then my new delete file reads as follow

 

<?php
$path="images/gallery/slideshow/"; //path of images
$file = $_REQUEST['path'];

    if(is_dir($path)){
	if($dh = opendir($path)){
		unlink($file);
	closedir($dh);
}
}
echo"File Deleted"
?>

 

Would i have to modify  it?

 

first off i skimmed the post (so i maybe way off track but from your form) the code would be more like this

 

<?php
   $path="images/gallery/slideshow/"; //path of images
    if(is_dir($path)){
     foreach($_REQUEST['chkFiles'] as $file){
         if(file_exists($path.$file)) unlink($file);
      }
   }
}
echo"File Deleted"
?>

Yes, to use the multiple checkboxes and one delete button, they ALL have to be in the same form.  You will probably want to display the filename as well so the user knows which checkbox to check

 

echo'<form method="post" action="deleteImage.php" enctype="multipart/form-data">';
echo'<ul>';
foreach($files as $file) 
{ 
    echo '<li>Image: ' . htmlentities($file) . '<br/>
          <Input type=checkbox name="chkFiles[]" value="' . $file . '"/>
          </li>';
    // You may have to use urlencode($file) here, I'm not real sure
}       
echo'</ul>'."\r\n";
// add the submit button here
<input type="submit" name="submit" value="Delete Checked"/>
echo'</form>';

 

Now you should have an array of $_POST['chkFiles'][] containing filenames.  Going back to your original code with a couple of changes:

<?php
   $path="images/gallery/slideshow/"; //path of images
//   $image = $_POST['imagefile'];

foreach ($_POST['chkFiles'] as $image) {
   $file = $path . $image;
   // you may have to use urldecode here (especially if you had to use urlencode in the other)
   // $file = $path . urldecode($image);
   
        if(file_exists($file))
       {
            if(unlink($file))
            {   
                echo "The file <i>".$file."</i> has been deleted.";
            }
            else 
            {
                  echo "The file <i>".$file."</i> could not be deleted.";   
            }   
      }
      else 
      {
            echo "The file <i>".$file."</i> could not be found.";   
      }
}
?>

 

I have NOT tested this code, but I think it is mostly correct.  You may want to stick some checks in there to see if you actually get any checked boxes before the loop.  During testing, add error_reporting(E_ALL); to the top of all scripts so you can see any messages you get.

DavidAm thanks, that did the trick, i tested it deleting just one file and it worked and tested deleting more than one file and it worked.

 

And yea you had a small typo like madTechie pointed out.

 

Anyway thanks allot you guys for the help.

You know what i have another quick question i've been trying to get this but i'm locked up

 

Once there are no images i want it to say somethingg like nothing on the folder or something like that

 

SO i figured i put something like this on the top

 

if($images==0){
   echo"There are no images";
}else{echo the form with images....
}

 

I tried that while it did echo that, it did it even when there images on the folder

 

 

LOL, it was just an over sight.. dame.. you should see some of mine..

 

 

So solved ?

if so please click topic solved (bottom left)

Yes I always read it.

 

 

EDIT: try this update

        echo'<form method="post" action="deleteImage.php" enctype="multipart/form-data">';
        echo'<ul>';
        if(empty($files)) echo"There are no images"; //add this
        foreach($files as $file) 

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.