SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: unlink()

  1. #1
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question unlink()

    Having some issues using the unlink() function.

    Warning: unlink(img/inv/53): Permission denied in C:\apache2triad\htdocs\dunham\app\controllers\inventories_controller.php on line 74 Warning: Cannot modify header information - headers already sent by (output started at C:\apache2triad\htdocs\dunham\app\controllers\inventories_controller.php:74) in C:\apache2triad\htdocs\dunham\cake\libs\controller\controller.php on line 446

    that's the error i'm getting on my localhost. i'm using apache2triad on winxp pro as my local development system.

    i was reading about the function php.net/unlink and most of the examples were based on *nix servers.

    what needs to be modified for this function to work in my environment?

  2. #2
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you have on line 74? You have echo, print... ?

  3. #3
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's the call to the unlink function. it's not a syntax error but a permissions based error that i'm trying to resolve. it's on a windows box so cmod isn't the issue, need to know what in php needs to be altered for this to properly work on my development box.

  4. #4
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've had similar problems when developing on a local machine. In my case, I was running PHP and Apache using XXAMP on my local machine. I was working in dreamweaver (I like the code coloring it does).

    I kept getting permission errors when I went to delete a certain directory or file. I found that it was Dreamweaver causing the problem, so if I closed down dreamweaver and ran the script again it worked fine.

    I don't know if something similar is happening to you or not, but hopefully this helps.
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  5. #5
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeanco View Post
    I've had similar problems when developing on a local machine. In my case, I was running PHP and Apache using XXAMP on my local machine. I was working in dreamweaver (I like the code coloring it does).

    I kept getting permission errors when I went to delete a certain directory or file. I found that it was Dreamweaver causing the problem, so if I closed down dreamweaver and ran the script again it worked fine.

    I don't know if something similar is happening to you or not, but hopefully this helps.
    thanks Ryan, i gave that a try (i'm using DW), i closed it down, reran the script and still getting the permission error. wondering if something i should be doing with a .htaccess script? permission errors drive me nuts.

  6. #6
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With me, I was trying to delete a directory. It would let me empty all files in the directory and but then gave an error when I tried to delete it. I had to close down DW and delete the directory manually. With dreamweaver still closed, I then ran the script that created and filled the directory, followed by the one that deleted it. This finally worked for me.

    It may be worth it to spend a little more time making sure that lingering effects of dreamweaver file permissions aren't around. Maybe try a reboot and run the script before dreamweaver is open.
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  7. #7
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeanco View Post
    With me, I was trying to delete a directory. It would let me empty all files in the directory and but then gave an error when I tried to delete it. I had to close down DW and delete the directory manually. With dreamweaver still closed, I then ran the script that created and filled the directory, followed by the one that deleted it. This finally worked for me.

    It may be worth it to spend a little more time making sure that lingering effects of dreamweaver file permissions aren't around. Maybe try a reboot and run the script before dreamweaver is open.
    ya, i'm going to give that a try. rmdir() works fine, but that will only remove a directory where it is empty. so what i may need to do is if i can't get unlink() to delete a folder with contents in it, i'll have to write a class that deletes all the files in a directory first, then use rmdir() to get rid of that directory.

  8. #8
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    figured i would share this bit of code that worked.

    PHP Code:
    //*Nix Based
    $directory escapeshellarg($directory);
    exec("rm -rf $directory");

    //Windows
    $directory escapeshellarg($directory);
    exec("rmdir /s /q $directory"); 
    note that on the *nix solution the directory you're going to delete needs a permission of 0777.

    this will remove a directory and all of its contents including subdirectories and their contents.

  9. #9
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case it helps, here's the code for how I recursively deleted a directory
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  10. #10
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeanco View Post
    In case it helps, here's the code for how I recursively deleted a directory
    hi Ryan, thanks for the code. i switched over to your solution so i didn't have a platform specific solution. although i had to make one modification to your code to get it to delete nested directories.

    PHP Code:
        function emptyDirectory($directory) {

            if(!isset(
    $directory))  { return false; }

            if(
    substr($directory,-1) == '/') {
                
    $directory substr($directory,0,-1);
            }

            if(!
    file_exists($directory) || !is_dir($directory))    {

                return 
    FALSE;

            } elseif(!
    is_readable($directory))    {

                return 
    FALSE;

            } else {

                
    $handle opendir($directory);

                while ((
    $item readdir($handle))!== FALSE ) {

                    if(
    $item != '.' && $item != '..') {

                        
    $path $directory.'/'.$item;

                        if(
    is_dir($path)) {

                            
    $this->emptyDirectory($path); //i needed to add the $this pointer, your code was missing it.

                        
    } else {

                            
    unlink($path);
                        }
                    }
                }

                
    closedir($handle);

                if(!
    rmdir($directory)) {

                    return 
    FALSE;
                }

                return 
    TRUE;
            }
        } 

  11. #11
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are getting a permission issue then the apache user doesnt have the rights to delete that folder. So check the permission of that folder. On the issue with the person with Dreamweaver, most likely dreamweaver locked the folder/file so you couldnt delete the folder/file while dreamweaver was open.

    But its a Windows permission issue and the cannot send header error is a result of data passed to the browser before the header command.

    Silly

  12. #12
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, the code works for me without the $this-> pointer. I'll make note of it though.

    Thanks.
    Ryan Price
    Subscribe to my blog for regular tips and tricks


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
  •