PHP: generated zip has chmod 644 - php

I'm using ZipArchive to create Zips and then provide them to download. I save them in a folder and I want to delete all every night (my cronjob would do that).
But now I've seen that they are created using CHMOD 644, and if I try to delete them with my script, I always can't delete them because I do not have sufficient rights.
How can I declare that every new Zip which is created uses 777?
Thanks for help! Flo

Are you able to chmod it like so?
chmod("/somedir/somefile", 0777);

The ability to delete files from a directory depends on the permissions of the directory, not the individual files inside. If your directory is 0777, you'll be able to delete the files regardless of their permissions. On the other hand, even if your files are 0777, you might not be able to delete them if your directory is unwritable to your cronjob.

Related

Apache server file permissions

I know this was brought up many times. Still I can not find a sutable answer.
I want to upload a file to a server and want to set temporary 777 permission to the file where I put content. I am the only owner of the file and the directory and it does not seem to be an option to change or add an owner from the control panel and I dont want to set 777 to that folder. Since neither apache nor script own the file chmod does not work. Is there a way to get the ownership to php, a particular script or the server throught chown function? The script that POST a file is not publically accessible btw.
Thanks.
Dunno if I am right or not.
First you can get the owner of the file with this
$filename = 'index.php';
print_r(posix_getpwuid(fileowner($filename)));
with
chmod('/var/www/folder/', 0777)
chmod('/var/www/folder/index.php', 0777)
you can change any folder or file to 0777
you can also check with chmod the file or folder have
if( chmod($path, 0777) ) {
chmod($path, 0755);
}

Ubuntu server folder permissions

I have an uploads directory on my server, php upload works fine when folder's permission is set to 755 but if I make new dir inside uploads (uploads/subuploaddir) upload fails and I need to set 755 permission for this folder manually, so my question is how can I set 755 permission for uploads dir and all of it's future created sub directories?
PS. chmod -R 755 ...mypath/uploads doesn't work
Thanks in advance.
All answers are very appreciated.
Peter
UPDATE:
Problem solved, my fault. PHP creates folders with permission 777 as default and I created folders for testing purposes through FTP manually, so it was created by another system user and with different permission.
Use
umask(022);
in PHP before creating a file (e.g. line 1 in index.php). Using that option, every files that PHP creates will have a permission that equals to 755. If that doesn't work, search your code for calls to umask and change them to the one above.
you can use this command
chmod -R 755 directorypath or file

Setting File Permissions in Windows with PHP

I have a script that uploads a *.csv to import into a DB table that I have which works great in linux through chmod($target, 0777); but I can't for the life of me find the solution to do exactly this but on a Windows based Apache server.
Some other posts have people responding "don't put in the 0777 and it should work" but that's not the case for me. Thanks!
Thanks to the comment left on my original post I was able to figure it out with a little more help from https://web.archive.org/web/20171121192635/http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/
The problem only happens when you use PHP to upload a file. When you upload a file, PHP sends the file to a temporary directory on the
hard drive (for me it is C:\Windows\Temp) and then copies it over to
it’s intended directory. Once the file has landed in the temporary
directory, it is assigned the permissions of that directory. The
problem is when Windows copies that file, it keeps the temporary
directory’s permissions and doesn’t inherit your web directory’s
permissions.
The easiest way to fix this problem is to add to the temporary directory your intended web directory’s permissions. There’s no need
to erase the permissions already in the temporary directory, just add
the web directory’s permissions to them. In other words, follow these
steps
To change the permissions of your temporary upload directory, find
the “upload_tmp_dir” in your php.ini file.
Set it to the directory
of your choosing (outside your web folders of course) or leave it at
default (for me it is C:\Windows\Temp).
Browse to this folder and add the permissions of your web folders to it.
While Brian Leishman's answer will work, if you do not have the ability to edit permissions on the temp folder, you can make your uploaded file inherit permissions from its new location manually with the following on the command line:
icacls "target.txt" /q /c /reset
So, using PHP's exec() function:
exec( 'icacls "target.txt" /q /c /reset' );
For details of the various icacls flags, see: https://technet.microsoft.com/en-us/library/cc753525.aspx
Tip: Using the /t flag, you can use pattern matching to process multiple files.

File permission when creating a file

I have a php file that when ran, it creates another file. Should the php file that creates the file have 750 permissions to write the file or should the dir that the file is being written to have the 750 permission?
First, the permissions of the php file have nothing to do with that.
Assuming that the user who executes the script is the owner of the directory the permissions of that directory should be set to
0700
or
0750
if members of the group that owns the directory should already have write and listing permissions. But I cannot give you the exact permissions for your use case. Therefore I need to know what is the exact scenario
There is a rule of thumb in the permission. Maximum permissions are Files 0644 and Folders 0750.
If you need to give any permission simply chmod it just before the action and then change it to the above rules.
Hope it help.

How to delete Folders created with PHP mkdir?

I have created folders using PHP's mkdir command.
Now I want to delete these folders over FTP or SSH.
I get the error "permission denied".
I am on a managed server so I do not have root access.
What can I do so I will be able to delete these folders?
Do I need to change the file permissions (chmod) using PHP?
The folders would have been created with the ownership/permissions of whatever account PHP was running under (Apache's, if you're doing this from a web-based script).
You wouldn't be able to chown the directories to another account, as that requires root permissions. You could have the script that creates the directories set them to mode 0777, which'd give everyone read/write/delete access to them, but you might not want to open up things that wide.
you have to change the permissions first:
chmod("/somedir/somefile", 755);
or whatever you like
then you can remove with
rmdir("dir")
Yes, you must run chmod after directory or file creation with PHP. Its because PHP runs with Apache permissions.
After chmod to PHP/Apache user you can rename, move or delete folders and files.
Check your permission first if you got any problem. Some folder you only can delete or chmod if you are owner.
If you are owner, then you can use PHP chmod.
CHMOD("PATH_TO_FOLDER",0755);
Then use unlink to delete files in folder:
unlink("PATH_TO_FOLDER/*.*");
And then
rmdir("PATH_TO_FOLDER")

Categories