www.webdeveloper.com
Results 1 to 7 of 7

Thread: copy() Permission denied error

  1. #1
    Join Date
    Feb 2005
    Location
    The dark place
    Posts
    91

    copy() Permission denied error

    I'm trying to write a little script that creates backups of my many mysql databases. Everythings going fine but I'm at a little stumbling point - hopefully someone can help.

    The way I am trying to do it is to create backup files by first copying a blank text file which already resides on the server using...
    PHP Code:
    copy($file_to_copy $name_of_copy); 
    ...and then I intend to stuff the backup information into that duplicated, uniquely named backup file.
    The problem I'm getting is ...

    Warning: copy(mailinglist08Dec05132559.csv): failed to open stream: Permission denied

    ...regardless of how I chmod the source file. The script can 'see' the source file OK (I've noticed if I inte...ntionally mistype the source file I get different errors).

    For completion, here's my simple copy function:
    PHP Code:
    function f_CreateBackupFile($file_to_copy $name_of_copy){
    // CREATE A COPY OF THE BACKUP FILE AND NAME IT ACCORDINGLY
        
    if (is_file($file_to_copy)){
            if (
    copy($file_to_copy $name_of_copy)) {
               
    f_narrate("Created new file " $name_of_copy 6);
            } else {
                
    f_narrate("Failed to copy file " $file_to_copy 6);
            }
        } 
    // END COPY BACKUP FILE LOOP


    Thanks all!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    First thing I'd check is the permissions on the directory to which you want to copy the file. The PHP user needs to have read/write/execute permission on that directory (as well as read/execute on that directory's parent directories).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2004
    Posts
    11

    2 ways around this

    What is happening is that php doesn't have permission to write to the destination directory. I'm assuming you (like me) are on a shared host, where php is run as a different user, and you don't have the ability to change the permissions for php globally.

    There are two ways around this.

    The first is to create a directory with 777 permissions so that php can write to it. This is fine if you are going to manually download the file shortly thereafter and then delete it. But, if you plan on leaving this file in place, this is not a very safe solution.

    The second is to use php's built-in ftp functions to connect to "localhost" as yourself (logging in with your user name and password), and then putting the file in the directory you want.

    Hope one of these works for you!

  4. #4
    Join Date
    Feb 2005
    Location
    The dark place
    Posts
    91
    You are both brilliant!

    I was copying the blank file into the same root dir - now I've created a chmod777 backup subdirectory and it works just peachy!

    Thanks I'd have been poking at that for hours!

  5. #5
    Join Date
    Feb 2008
    Posts
    2
    hii guys i have the same problem on my server
    every time i upload images i got this permission denied error and i have did 777
    but some pages work properly and others no
    inspite they r copying to the same folder????????

  6. #6
    Join Date
    Aug 2005
    Posts
    70
    Did u set full path like :

    /home/user/john/images

  7. #7
    Join Date
    Jul 2009
    Location
    Santiago, Chile
    Posts
    1

    You must do a few steps more.

    Well, I've runned into the same problem and I found that to give full permissions (777) is only the first step in the way to go. In my situation, after giving full access, I kept on with no success so I found that printing the real error, with print_r(error_get_last()) gave me the following info:

    Array ( [type] => 2 [message] => move_uploaded_file() [function.move-uploaded-file]: SAFE MODE Restriction in effect. The script whose uid is 613 is not allowed to access /home/sites/mysite/www/myapp/excelFiles owned by uid 533 [file] => /home/sites/mysite/www/myapp/fileLoader.php [line] => 190 )

    The solution was to do a chown to fileLoader.php so it would belong to the user with uid 533 instead of 613.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles