php unlink on windows fails (permission denied), rename works - php

I have iis8.5 and php 7.1 (fastcgi, says phpinfo). I'm trying to unlink (delete) a folder.
unlink(realpath($folder));
and get a permission denied. If I try
exec(sprintf("rd /s /q %s", escapeshellarg($folder)));
it still doesn't work, but I don't get a reason either. So its permissions then - ok.
If I do a get_current_user() in the script it returns IUSR.
In IIS I have the app pool identity of the site to a user who has FULL CONTROL on the parent of the folder I'm trying to remove. IUSR also has FULL CONTROL of the same folder (and read from the root down to there, obviously).
Not sure what the permissions have to be in order for php to have delete capability. Many many SO answers simply say "you have to have the correct permissions" or "make sure you set the correct permissions". grumble
What are the permissions I need to add and where do I do that to make unlink or rmdir work?

https://msdn.microsoft.com/en-us/library/bb727008.aspx
"Giving a user permission to write to a file but not to delete it doesn't prevent the user from deleting the file's contents. A user can still delete the contents." Write will let you empty the file but not remove it.
You'll need "Full Modify" or "Execute" to delete the file.

Related

PHP script creates files but owned by www-data. Now cannot be deleted [duplicate]

I currently have a directory (udir), which has only read and write permissions for all users. This directory contains two files (file1 & file2)
I initially though that only write access was needed (on the directory) for me to be able to delete/remove a file via (rm udir/file1) but the rm command would give me access denied. when i set the permissions to read, write, and execute, the rm command works.
Obviously the execute access is needed as well but why??
I thought the execute access on a directory was to be able to make it a working a directory and search its contents and access sub directories.
You actually need read, write and execute permissions on the directory, not on the file itself since the operation is done considering the permissions effects of directories.
A good documentation can be found on this link, which mentions the below in the section Special Considerations on Directories:
To delete a file requires both write (to modify the directory itself)
and execute (to stat() the file's inode) on a directory.  Note a user
needs no permissions on a file nor be the file's owner to delete it!

Deleting video files using unlink() in PHP 7

I'm facing a problem with deleting video files from folder using php unlink() function , image are deleting but when trying deleting videos it says
unlink(file_path) : permission denied.
You (running your script as either through CLI or a webserver) need write access to the directory in which the files are located. so access to the file is not enough.
Your image directory would be different and writable for webserver or cli.
chmod("your/video/dir/path",0777);
try using above code before unlink the video in your script.
Edit: Looks like you are using Windows. Unfortunately, my answer is for Unix-like operating systems (so Linux, MacOS). You could try installing Bash extension for Win8+, but still I'm not sure if this would work. However, I'm keeping my answer in case of anyone with similar problem is looking here for an answer.
Changing permissions through PHP might work in some cases, but not always, because if you don't have permissions to delete the file you might also not have permissions to change them.
The best solution is to create a directory where you will keep files to which PHP will have full access. Let's call it dirname. Once you have created a directory, change its owner and group to the one corresponding to your web server user's name (if you're using Apache, it's "www-data"), for example: chown www-data:www-data dirname.
Once you've done that, change folder's permissions. My suggestion is 744, it will assure that user who owns it will have all permissions, and everyone else will be able only to read it. To do that, execute the following command: chmod -R 777 dirname.
Now you should be able to do anything you want with the files in given directory directly from PHP.

Deleting 'Locked' Folders on the server

I have an opencart folder on my server, and I want to delete it, however I can't because my FTP client simply say's:
550 shop_Opencart: Operation not permitted
I understand that some files have different permissions etc but why, as the server admin, can I not delete these files, or change the file permissions? Is there anything else that stops these files from being deleted or modified, other than file permissions?
Even using the control panel (in this case PLESK) I cannot delete them, I get a Permission denied error.
What can I do?
Try chmod the folder using your ftp client and then delete it.
Connect via SSH using Putty as the root user and make the changes via that.

Operation not permitted - unlink on local machine

Im trying to unlink a folder on the local version of my site.
I get the error:
operation not permitted
Any ideas how I can get unlink to work on my local machine? Im using MAMP.
See the documentation:
unlink — Deletes a file
and
See Also: rmdir() - Removes directory
You have a directory. You need to use rmdir, not unlink.
It means the script is not allowed to delete the folder. This can have various reasons - the most likely one is that you are trying to unlink() a folder instead of using rmdir() to delete it.
Here are the possible reasons for "operation not permitted" (EPERM) from the unlink(2) man page:
EPERM The system does not allow unlinking of directories, or unlinking of directories requires privileges that the calling process
doesn't have. (This is the POSIX prescribed error return; as noted
above, Linux returns EISDIR for this case.)
EPERM (Linux only)
The file system does not allow unlinking of files.
EPERM or EACCES
The directory containing pathname has the sticky bit (S_ISVTX) set and the process's effective UID is neither the UID of
the file to be deleted nor that of the directory containing it,
and the process is not privileged (Linux: does not have the CAP_FOWNER capability).
This is a permissions problem.
Try giving the file you want unlink permissions like CHMOD 666.
You probably created the file yourself and want PHP (another user then yourself, probably Apache or www-data depending on how MAMP is installed) to delete the file for you - without the right permissions, this cannot be done.

Giving file write permission using php

I have a file in my project folder.How i can i give file write permission using php.I used this code
chmod($file,0777);
But it giving an error
Warning: chmod() [function.chmod]: Operation not permitted
The file is created by another user.Is their any way to do this .Thanks in advance
This happens because PHP does not have rights to do the change. The user under which PHP runs is usually the web server's user and different from the user you use to add files.
You generally only do chmod on files created with PHP. To be able to do this on other files you need to change the owner (chown).
The current user is the user under
which PHP runs. It is probably not the
same user you use for normal shell or
FTP access. The mode can be changed
only by user who owns the file on most
systems.
From http://php.net/manual/en/function.chmod.php
Well - you just can't if it says you are not permitted to.
Point is - the file belongs to some user and group, most likely root:root - and you are just a user on the server. If root's the owner of that file, you can't change the permissions at all.
Notes:
$file must be a filename. If you put the handle there, the file (most likely) doesn't exists, but still.
Check if the filename is not beginning with / or something. Try different variations.
you can install php via SUEXEC or SUPHP instead of mod_php which allows to change the user as which php is executed, still this dosnt solve anything if the owner is set wrong

Categories