Apache server file permissions - php

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

Related

Save file in /var/www from html upload file

I have an issue in a program that uploads files from html in my local server, into the /var/www/html/my_app directory.
In this directory I have changed the permission with:
sudo chmod -R 777
So I can save the photo that uploads in the directory inside my_app.
But here is the problem, when I upload a file inside the directory it always has the read permission.
Can I change that? So all the files I upload have permission.
chmod 777
move_uploaded-file always set permission to 600 for any uploaded file whatever configuration set in apache umask. you can use chmod in context with your code while uploading file and set permission.
chmod($target_path, 0664);
http://php.net/manual/en/function.move-uploaded-file.php In the comment section another users have same issue so they are describing same thing.

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

fopen Creates File, But how to change permissions?

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

is this the right way to setup an upload path using a variable?

I am trying to upload a document to a folder, whose name is inside the $folder variable. the page goes blank and prints no error but when i connected using ssh and try to open that folder it says: Permission denied.
This is how i am doing it:
$upload_path = '/stuff/$folder/';
And this is how i create the folders:
mkdir("/stuff/$folder", 0700);
First, $upload_path should be set with " :
$upload_path = "/stuff/$folder/";
To access folder with ssh, you need to set different permissions on folder creation. the owner of the folder is the user that running apache on your server (mostly www-data, but the user connected thought ssh is not the same) :
mkdir("/stuff/$folder", 0755);
it usual works with 775 (not to be writable by everyone)
mkdir("/stuff/$folder", 0775);
if that doesn't work try with writable by everyone, it must work
mkdir("/stuff/$folder", 0777);

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