SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP help with unlink()

    I am trying to delete a directory but it will have 2 files inside. So i have tried to use the unlink function to delete the files first and then the directory. I f i only have one file inside the directory my php is fine, however when i have two files inside i php throws up an error saying that it cant delete the directory becuase it has a file inside? One of the files, but the other got deleted?

    This is my php.

    PHP Code:
    elseif($type=="delete"){

            if(
    is_numeric($_GET['t'])){

                
    $getDir=mysql_query("select * from template where Temp_ID='{$_GET['t']}'",$con);
                
    $dirName=mysql_fetch_assoc($getDir);
                
    $dirDelete="../templates/".$dirName['Path'];

                if(
    $f=unlink($dirDelete."/index.php")){
                    
    unlink($dirDelete."/styles.css");
                    
    rmdir($dirDelete);
                    
    $sql11="delete from template where Temp_ID='{$_GET['t']}'";
                    
    mysql_query($sql11,$con);
                }


            }else{
                
    $msg1="Not a valid URL";
            }
            echo 
    "<meta http-equiv=\"refresh\" content=\"0;url=templateUpload.php?done=2\">";



  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You might need to loop through the directory and remove the files individually and then remove the directory. Have a look on the www.php.net/rmdir for some scripts that will help you
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this function, i'm yet to have problems with it:
    PHP Code:
    // delete entire directory
    function deleteDir($path
    {

       if (
    substr($path, -11) != "/")  $path .= "/"// Add trailing slash
       
       
    foreach (glob($path "*") as $file
       {
           if (
    is_file($file)) 
           {
               
    // Remove each file in this Directory
                
    if (!@unlink($file)) 
                {
                       return 
    false;
                }

           } elseif (
    is_dir($file)) { // if 2nd folder found inside folder 1, delete it
               
    deleteDir($file);
           }
       }
       if (
    is_dir($path)) 
       {
           
    rmdir($path);
       }
       
       return 
    true;

    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •