NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Deleting Files

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-01-2006, 03:45 PM THREAD STARTER               #1 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Deleting Files


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 Code:
<?
$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>';
????: NamePros.com http://www.namepros.com/programming/212643-deleting-files.html
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
            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.
NetworkTown.Net is offline  
Old 07-01-2006, 05:12 PM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
something like the following should be ok:-

PHP Code:
<?
$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 />';
????: NamePros.com http://www.namepros.com/showthread.php?t=212643

         echo 
'<TABLE width=500 align=center BGCOLOR=#CCCCCC>';
            echo 
'<TR><TD align=center valign=center>';
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
               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 by filth@flexiwebhost; 07-01-2006 at 05:17 PM.
Peter is offline  
Old 07-01-2006, 05:19 PM THREAD STARTER               #3 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



I just get a ablank page with that.
NetworkTown.Net is offline  
Old 07-01-2006, 09:26 PM   #4 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 210
wackyjoe is on a distinguished road
 



Originally Posted by NetworkTown.Net
I just get a ablank page with that.
PHP Code:
  <?
$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);    
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
   while(
$entryName readdir($myDirectory))
   {
      if (
strtolower(substr($entryName, -3)) == "jpg")
      {
         
$dirArray[]=$entryName;
         echo 
$entryName.'<br />';
????: NamePros.com http://www.namepros.com/showthread.php?t=212643

         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);
?>
wackyjoe is offline  
Old 07-01-2006, 11:14 PM   #5 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
wackyjoe there is no reason to open the directory like you do to delete a file.
Peter is offline  
Old 07-02-2006, 03:38 AM THREAD STARTER               #6 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Originally Posted by wackyjoe
PHP Code:
  <?
$read 
'../DirPathNameHere/';
if (isset(
$_GET['delete']) && !empty($_GET['delete']) && is_file($read.$_GET['delete']) && ereg("^[[:alnum:]]+$",$_GET['delete']))
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
{
   
$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;
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
         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.
NetworkTown.Net is offline  
Old 07-02-2006, 07:26 AM   #7 (permalink)
Pro Coder & Designer
 
aween's Avatar
Join Date: Apr 2005
Location: Netherlands
Posts: 967
aween is just really niceaween is just really niceaween is just really niceaween is just really niceaween is just really niceaween is just really niceaween is just really niceaween is just really nice
 



Are permissions set correctly ?
__________________
aween web development
aween is offline  
Old 07-02-2006, 08:50 AM THREAD STARTER               #8 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Yep they are, they are set to 777
NetworkTown.Net is offline  
Old 07-02-2006, 09:48 AM   #9 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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 Code:
error_reporting(E_ALL E_NOTICE); 
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
And let me know if you receive any error.
Peter is offline  
Old 07-02-2006, 10:09 AM THREAD STARTER               #10 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Still getting blank screen.
NetworkTown.Net is offline  
Old 07-02-2006, 10:27 AM   #11 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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.
Peter is offline  
Old 07-02-2006, 10:56 AM THREAD STARTER               #12 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Originally Posted by filth@flexiwebhost
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.
NetworkTown.Net is offline  
Old 07-02-2006, 02:34 PM   #13 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Edited a bit.

I tried this and I know it works:
PHP Code:
<?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')
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
        {
            
$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 by SecondVersion; 07-02-2006 at 02:40 PM.
Eric is offline  
Old 07-02-2006, 03:20 PM THREAD STARTER               #14 (permalink)
Account Closed
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



SV still getting a blank page, here i found another script that dont give me a blank page,

PHP Code:
<?

function deldir($dir) {
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
    
$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) {
????: NamePros.com http://www.namepros.com/showthread.php?t=212643
        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.'&nbsp;<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
NetworkTown.Net is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:25 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger