fopen Creates File, But how to change permissions? - php

I am creating a new file using fopen.
$filename = 'user_data/10.xml';
$openhandle = fopen($filename, 'w+');
Then I check if the file has been created using: file_exists() function.
The problem is: The file is being created with some owner, probably the folder name, but its not me. Also the permissions of the file is only readable by the owner.
And since I am not the owner, I can't read the file, or change the permissions.
But If attempt to change it using:
chown($filename, 'myusername');
chmod($filename, 777);
I tried changing the file owner and permissions using the Terminal using sudo. That worked properly.
So I also tried using the functions above with shell_exec() so it runs in root.
But had no luck.
Although, I don't have much experience with file permission numbers, the chown command is also not working.
So how should I change the owner and permissions of the file so i'm the owner and its readable and writable by my other PHP scripts?

You should be able to chmod it using only the following line:
chmod($filename, 0777);
Note the 0 before the 777.
Also do not change the ownership before it has been chmod'ed

Related

PHP Renaming files permission denied (Apache)

I'm stuck with a problem that I thought was easy. I have to temporarily rename a bunch of files. All folders are 0755, files are 0644. The server runs PHP 5.5 in CGI Mode. For test purposes I put both the script and the file to rename in the same folder:
$root = $_SERVER['DOCUMENT_ROOT'];
chmod ($root."bla/_bla.php", 0777);
rename($root.'bla/_bla.php',$root.'bla.php');
chmod ($root."bla/bla.php", 0644);
But all I get is the "permission denied" error. Owner and group are for all files and folders the same. No luck even if I change folder and file to 0777. What exactly am I missing here?
Thanks in advance for any help. Hope, this isn't a duplicate, but I couldn't find an answer here.
Try to chmod your php script. I meant this script not the one you want to rename. If you are using linux goto terminal and chmod. I think this would work

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);
}

Can not write to file, even though is_writable returns true

Anyone have any ideas on this? I check the file with file_exists() and is_writable(), both of which return true. I set the directory and file to 777 just to be safe as well, but trying fwrite() or file_put_contents() both result in no change in the file
Here is the relevant code:
$filePath = dirname( __FILE__ ) . '/' . 'myfile.txt';
file_put_contents($filePath, "waffles are delicious");
You've checked the permissions of the file but also have a look at the ownership.
I've often seen issues where files are unpacked from archives with a uid/gid from the originating system. This is usually the case when you've uploaded and unpacked a zip or tar.gz installation archive of a framework, CMS or forum instead of waiting for all the files to be uploaded through FTP.
To change the file owner you'll need to do a chown command in the shell to change it to the username/group of your account. You will probably need to be root/su to do this.
chown -R username:groupname *
The -R makes it recursive an applies to all files in all subdirectories.
Try removing the closing php tag to make sure this isn't a whitespace issue.

PHP Function Rename Permission denied

In server, script create new folder, set chmod to 0777, but then it tryes to move files to that folder i get error: Permission denied.
mkdir("../".$new_1, 0777);
chmod("../".$new_1, 0777);
mkdir("../".$new_1."/".$new_2, 0777);
chmod("../".$new_1."/".$new_2, 0777);
rename("files/".$failai[$i].".jpg", "../".$new_1.'/'.$new_2."/".$failai[$i].".jpg");
Warning: rename(files/new_file.jpg,../112a/112b/Tech_diz_1.jpg) [function.rename]: Permission denied in ..code/Jpg&Html.php on line 82
Any solutions?
you'll need to have read and write permissions in the source folder, too.
only having permissions for the target-folder isn't enough as the file is removed from it's source.
Do you have the write access to the file? If not, make sure you chmod the file to 777 or at least to 644.
Also, check the existence of the file by giving a file_exists() on the file name before you rename. :)
Also, after moving file, you might need to set the permissions using chmod() to make it available for renaming. You can do it this way:
<?php
chmod($uploadedFile, 0755);
?>
You should also have permission to change the file "files/".$failai[$i].".jpg". I would guess that that is going wrong

How to chmod the folder to make it writable for the server in php

How to chmod the folder to make it writable for the server in php ?
I am using this function chomd as following
chmod("/images/original", 0750);
but still don't have the permission to write file . why ?
If the user your webserver runs on has no write access then it most likely is not the owner of the file, and therefore it cannot change permissions either.
To allow write access on the file for your webserver process either change the ownership (chown) or allow sufficient write permissions (chmod) on a shell.
try
chmod("/images/original", 0777);

Categories