Jump to content

Can anyone see what is wrong with this code. (unlink images)


petezaman

Recommended Posts

Hi all, I have two php pages, the first allows the user to select a directory in which to view the images and make selections to delete from that directory.

 

The first page "imageoverviewselect" has this forms details

 

<form action="imagesoverview.php" method="post" name="imageoption">

 

Thes second page "imagesoverview.php" uses the following snippets of code.

 

<?php
$upclass = $_POST['imageoption'];
switch ($upclass) {

case 1:
$temphead = "Staff Profile Overview";
$tempdir = "../images/staff/";
break;
case 2:
$temphead = "Dolphins Class Activity Overview";
$tempdir = "../images/activities/dolphins/";
break;
case 3:
$temphead = "Dolphins Class Display Overview";
$tempdir = "../images/displays/dolphins/";
break;
case 4:
$temphead ="SeaHorses Class Activity Overview";
$tempdir = "../images/activities/seahorses/";
break;
case 5:
$temphead ="SeaHorses Class Display Overview";
$tempdir = "../images/displays/seahorses/";
break;
case 6:
$temphead ="Sharks Class Activity Overview";
$tempdir = "../images/activities/sharks/";
break;
case 7:
$temphead ="Sharks Class Display Overview";
$tempdir = "../images/displays/sharks/";
break;
case 8:
$temphead ="Turtles Class Activity Overview";
$tempdir = "../images/activities/turtles/";
break;
case 9:
$temphead = "Turtles Class Display Overview";
$tempdir = "../images/displays/turtles/";
break;
case 10:
$temphead = "Whales Class Activity Overview";
$tempdir = "../images/activities/whales/";
break;
case 11:
$temphead ="Whales Class Display Overview";
$tempdir = "../images/displays/whales/";
break;
case 12:
$temphead = "Trip/Visits Overview";
$tempdir = "../images/trips/";
break;
}

print '<h1>'. $temphead .'</h1>
<p> Please Select Image Or Images To Delete Then Press The Submit Button At The Bottom Of The Page </p>';

?>

 

This gives the tempdir variable a name to work with.

 

I then have this code to unlink the file.

 

<?php

$path = $_POST['tempdir']; 
if(isset($_POST['file']) && is_array($_POST['file'])){	
foreach($_POST['file'] as $file)	{			
unlink($path . "/" . $file) or die( "Unable To Delete File. Please Try Again Later" );	
}}?>

 

Followed by the following code to show the images.

 

<form name="form1" method="post">
<?php
$path = $tempdir;
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) { 
if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue; 
if ($file == "getalbumpics.php")
continue;
echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "Select <br>"; 
echo "<img src=\"";
echo $path;
echo $file;
echo "\" width=\"160\" height=\"120\"  ><br />";


}

closedir($dir_handle);

?>
<input type="hidden" name="tempdir" value="<?php echo $tempdir ?>" />
<input type="submit" name="Delete" value="Delete"></form>

 

Now, on initially selecting the directory, a load of images are displayed correctly with a selection box and the delete button. However, after pressing delete, I would like the images to be shown again. It just shows the text and the Unable to Open Folder (from the die routine).

 

If I remove the die command, the button shows back up.

 

Any ideas?

 

Cheers

Pete

So, am i right in thinking, you want a page where you can delete images, when you delete an image it says "deleted", and then returns you to the "gallery" so to speak?

 

You could try using a Header redirect,

 

From the original page with the images->Click delete->Go to delete.php, displays "File deleted", with a link "Continue", then that link goes back to the original page with the images.

 

Hope this Helps,

-CB-

I'm assuming that your script is deleting the images correctly?

 

The issue with the "Unable to open folder" is just that.  The $tmppath variable thatyou are passing to PHP is either probably....

 

#1) Empty (var_dump it to see)

or

#2) A relative path, which from your scripts execution point might not map to the right directory.

 

My advice would be set your paths as absolute - and see if the script runs as intended.  Willing to bet it does :)

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.