NameSilo

Deleting Files

Spaceship Spaceship
Watch

NetworkTown.Net

Account Closed
Impact
2
Hi

I have made a image host script i want to make a admincp now so in the admincp i need help i want to know how i can do this:

List all images in the directory (in a 250 * 200 box or something in a list)
Then at the side of the images put a link so i can delte a image, i know it would use unlink or something but i can't figure out how to do it, iv got this so far;

PHP:
<?
$read = '../DirPathNameHere/';    
$myDirectory = opendir($read);    
   while($entryName = readdir($myDirectory))
   {
      if (strtolower(substr($entryName, -3)) == "jpg")
      {
         $dirArray[]=$entryName;
         echo $entryName.'<br />';

         echo '<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo '<TR><TD align=center valign=center>';
               echo '<img src=' .$read.$entryName. '><BR>';
            echo '</TD></TR>';
         echo '</TABLE>';
       }
   }
   closedir($myDirectory);
?>

And also i had this from sitepoint but did not understand any one: http://www.sitepoint.com/forums/showthread.php?t=367792


Thanks.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
something like the following should be ok:-

PHP:
<?
$read = '../DirPathNameHere/';
if (isset($_GET['delete']) && !empty($_GET['delete']) && is_file($read.$_GET['delete']) && ereg("^[[:alnum:]]+$",$_GET['delete']))
{
	unlink($read.$_GET['delete']);
}    
$myDirectory = opendir($read);    
   while($entryName = readdir($myDirectory))
   {
      if (strtolower(substr($entryName, -3)) == "jpg")
      {
         $dirArray[]=$entryName;
         echo $entryName.'<br />';

         echo '<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo '<TR><TD align=center valign=center>';
               echo '<img src=' .$read.$entryName. '><BR></TD>';
            echo '<TR><TD align=center valign=center><a hregf="'.$_SERVER['PHP_SELF'].'?delete'.$entryName.'">Delete image</a></TD></TR>';
         echo '</TABLE>';
       }
   }
   closedir($myDirectory);
?>

I have not checked the script but it shows you basically what needs to be done. Also just a note it would be better if $read was an absolute server path to the necessary folder instead of relatie to the current location.
 
Last edited:
0
•••
I just get a ablank page with that.
 
0
•••
NetworkTown.Net said:
I just get a ablank page with that.

PHP:
  <?
$read = '../DirPathNameHere/';
if (isset($_GET['delete']) && !empty($_GET['delete']) && is_file($read.$_GET['delete']) && ereg("^[[:alnum:]]+$",$_GET['delete']))
{
   $handle = opendir($read);
   while($file=readdir($handle))
   @unlink($read.$_GET['delete']);
   closedir($handle);

echo"<br>Files Deleted";

    
}    
$myDirectory = opendir($read);    
   while($entryName = readdir($myDirectory))
   {
      if (strtolower(substr($entryName, -3)) == "jpg")
      {
         $dirArray[]=$entryName;
         echo $entryName.'<br />';

         echo '<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo '<TR><TD align=center valign=center>';
               echo '<img src=' .$read.$entryName. '><BR></TD>';
            echo '<TR><TD align=center valign=center><a hregf="'.$_SERVER['PHP_SELF'].'?delete'.$entryName.'">Delete image</a></TD></TR>';
         echo '</TABLE>';
       }
   }
   closedir($myDirectory);
?>
 
0
•••
wackyjoe there is no reason to open the directory like you do to delete a file.
 
0
•••
wackyjoe said:
PHP:
  <?
$read = '../DirPathNameHere/';
if (isset($_GET['delete']) && !empty($_GET['delete']) && is_file($read.$_GET['delete']) && ereg("^[[:alnum:]]+$",$_GET['delete']))
{
   $handle = opendir($read);
   while($file=readdir($handle))
   @unlink($read.$_GET['delete']);
   closedir($handle);

echo"<br>Files Deleted";

    
}    
$myDirectory = opendir($read);    
   while($entryName = readdir($myDirectory))
   {
      if (strtolower(substr($entryName, -3)) == "jpg")
      {
         $dirArray[]=$entryName;
         echo $entryName.'<br />';

         echo '<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo '<TR><TD align=center valign=center>';
               echo '<img src=' .$read.$entryName. '><BR></TD>';
            echo '<TR><TD align=center valign=center><a hregf="'.$_SERVER['PHP_SELF'].'?delete'.$entryName.'">Delete image</a></TD></TR>';
         echo '</TABLE>';
       }
   }
   closedir($myDirectory);
?>

that dose not work either.
 
0
•••
Are permissions set correctly ?
 
0
•••
Yep they are, they are set to 777
 
0
•••
have you tried changing $read to a absolute path instead of a relative path.

Also try putting the following line at the top of the file (after the <?php):-

PHP:
error_reporting(E_ALL ^ E_NOTICE);

And let me know if you receive any error.
 
0
•••
Still getting blank screen.
 
0
•••
were you getting a blank screen before you made these changes?

Nothing about these changes should cause no output, can you double check by looking at the source when you go to the page.
 
0
•••
filth@flexiwebhost said:
were you getting a blank screen before you made these changes?

Nothing about these changes should cause no output, can you double check by looking at the source when you go to the page.

Well i found that code on sitepoint :| so i dont know but i just got it to start me off and help you/other to help me.
 
0
•••
Edited a bit.

I tried this and I know it works:
PHP:
<?php

$read = 'images/';


if (!empty($_GET['delete']) AND file_exists($read . $_GET['delete']))
{
    if (unlink($read . $_GET['delete']))
    {
        echo 'File deleted.<br /><br />';
    }
    else
    {
        echo 'File could not be deleted.<br /><br />';
    }
}

if ($myDirectory = opendir($read))
{
    while (false !== ($entryName = readdir($myDirectory)))
    {
        if (strtolower(substr($entryName, -3)) == "jpg" OR strtolower(substr($entryName, -3)) == 'gif')
        {
            $dirArray[] = $entryName;
            echo $entryName . '<br />';
            echo '<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo '<TR><TD align=center valign=center>';
            echo '<img src=' . $read . $entryName . '><BR></TD>';
            echo '<TR><TD align=center valign=center><a href="' . $_SERVER['PHP_SELF'] . '?delete=' . $entryName . '">Delete image</a></TD></TR>';
            echo '</TABLE>';
        }
    }
    closedir($myDirectory);
}

?>
 
Last edited:
0
•••
SV still getting a blank page, here i found another script that dont give me a blank page,

PHP:
<?

function deldir($dir) {
    $dh=opendir($dir);
        while ($file=readdir($dh)) {
            if($file!="." && $file!="..") {
            $fullpath=$dir."/".$file;
            if(!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                deldir($fullpath);
            }
        }
    }
    closedir($dh);
    
    if(rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

if(!empty($_POST['delete'])) {
    foreach($_POST['delete'] as $todel) {
        echo 'Deleting File: '.$todel.'<br />';
        deldir($todel); // Function call to deldir.
    }
    echo '<br />';
}
//change the below to the full path to the folder
//requires the trailing /
$folderpath = '/home/networkt/public_html/imgscript/';
$filelist = glob($folderpath.'*');
if(!is_readable($folderpath)){
  die('Invalid folderpath: '.$folderpath);
} 

echo "<form method='post' action=''>";

if(empty($filelist)) {
    echo 'No files found.<br />';
    } else {
        foreach($filelist as $file){
        echo $file.'ย <input type="checkbox" name="delete[]" value="'.$file.'" /><br />'."\n";
    }
}

?>

<input type="submit" name="submit" value="submit" />
</form>

I get this error when trying to delte something: Warning: rmdir(/home/networkt/public_html/imgscript/view): Permission denied in /home/networkt/public_html/imgscript/admincp.php on line 17

I set the files/folder to 777
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back