• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

PHP Copy Function issue with file permissions

Hi all,

I am using to copy function to copy a file from one place to the other.

It works well but the problem is that the copied file cannot be deleted as it says permision denied.

Does anyone know how to fix this please?

THanks
0
error77
Asked:
error77
  • 3
  • 3
  • 3
2 Solutions
 
Marco GasiFreelancerCommented:
Hi error77. Are you sure files to delete and their directory have permission 0777?
0
 
error77Author Commented:
Well, the file that's copied is 777 on source but 644 when copied by the copy function :o/
0
 
Marco GasiFreelancerCommented:
Sorry, I'll try to be more clear: you asked for deleting files, but the folder where these files are in does have permissions set to 0777? Not files it self, but the directory itself?

And about what you said, ensure the destination directory, not only files, have permissions 0777.

Cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HellmarkCommented:
Yeah, you have to take into account not only the permissions of the file you're copying, but also that of the directory the file resides in and the directory that the file is being copied to. If you do not specify the permissions at the time of copying, it may get the permissions of the target destination directory placed on them. Also, check to see what it lists as the owner. It is possible for that to have changed, because the PHP script likely isn't being run by your normal user, so the owner of the file after the copy operation is most likely the user used to run the script. The combo of ownership change, and change to read only access to non users (644 means owner can read and write, group can only read, and all others can only read). If you're no longer listed as owner, you do not have write access, and thus cannot delete it.
0
 
error77Author Commented:
The Folder is created using mkdir and contains cmod 755 (for some reason it won't let me change it.)

So, yes, it's a permission issue. I just want to control these permissions?

Make sense?
0
 
Marco GasiFreelancerCommented:
Yes. I suggest to ask your provider the reason you can't change permissions for that folder. The default permissions with mkdir are 0777 so what you said sounds strange...
0
 
error77Author Commented:
Also, I still cannot delete these folder :o/
0
 
HellmarkCommented:
I'm not sure which method you're using to copy the file, but the GNU cp command does have the ability to preserve permissions. Here is a quote from the cp manpage


-p     same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
              preserve the specified attributes (default: mode,ownership,timestamps), if possible additional  attributes: context, links, xattr, all

So, using "cp -p originalfilename targetfilename" should help.
0
 
HellmarkCommented:
Also, I should note that you should use the the exec() or or shell_exec() if you use this method.

<?php
echo exec('cp -p originalfilename targetfilename');
?>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now