Im trying to write to file using php but it doesn't see to work.
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose("$file");
I published the site using the school server. and the php file/text file are all in public_html folder. The problem is, running this into terminal writes the file. Running it as a website doesn't.
How do i fix this?
P.S. I've tried chmod 644, 777, 755
Remove the quotes in fclose :
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose($file);
Also, you have to grant all permission : see that answer.
Hmm... is it a UNIX/Linux server?
You probably want to give the group www-data permissions to write the folder.
Assuming your web folder is /var/www, try doing this:
sudo chown `whoami`:www-data /var/www
sudo chmod 775 /var/www
And try again.
If you're working in your personal web folder (http://domain.ext/~yourusername), the commands should be:
chown `whoami`:www-data ~/public_html
chmod 775 ~/public_html
Does it work running the script now?
Related
I was trying to upload a file using php to my local using XAMPP on MacOS. Whenever I tried to upload a file through script, I got thi error.
move_uploaded_file - Failed to open stream: Permission denied
So I tried some fixes I saw on internet, like chmod -R 777 , changing the user-group and also tried to give read&write permission to every user;
None of them worked.
This is the code I am using for uploading:
if(!empty($filename)){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = $slug.'_'.time().'.'.$ext;
if(!move_uploaded_file($_FILES['eventImage']['tmp_name'], '../assets/images/events/'.$new_filename))
{
$_SESSION['error']='<strong>Error!</strong> Uploading Image failed. Please try again.';
}
}
else{
$new_filename = '';
}
I wanna know how this error can be fixed. I think this is some security issues of MacOS.
like under nix, You can try (under test - not productive server):
chmod -R 755 ./.*
chmod +x ./.
where ./is your document root of HTML files (do not use it as root/admin !!!)
the Argument -R stands for "recursive operation" - so it will try to include all sub directories under ./
and:
chown -R www-data:www-data ./.
at Your document root.
www-data group, and user is need by Apache2.4 - see Settings in Manual.
The group, and user www-data is important under *nix like Operation Systems.
Under Windows, You have to start XAMPP with Administrator Rights.
So be warned !!!
And: You should never be use XAMPP productive !!!
I'm trying to create a secure way to handle file uploads by uploading the files to a directory outside of public_html.
I'm testing my script and my script works when I execute sudo php receive_files.php, but when I run receive_files.php without sudo I get failed to open stream: Permission denied.
Right now all permissions all the way back to var/www/html/mysite/public_html are set to 755. I tried changing all of them to 775 and the command still didn't work
Without sudo. And I'm pretty sure that would not be secure. How do I get around this problem?
My code:
$encoded_file = $_POST['file'];
$user_id = $_POST['user_id'];
$decoded_file = base64_decode($encoded_file);
$new_directory = "../../../../../../user_uploads/$user_id/";
if(!file_exists($new_directory)){
mkdir($new_directory, 0775, true);
file_put_contents($new_directory . $decoded_file);
}
Sudo is required when you are not the owner of something or you try to change permissions. So if you want php script to have access to upper directory. Change the group or user owner of upper directory same as php executor, in most cases its www-data
You can check owner of php script by using
ls -al
Then change owner using
chown -hR www-data:www-data directorypath
For more detail check manual of chown command
This will most probably fix your issue
Additional info
specifying absolute path instead of relative path solved the issue
How can I make a copy of a file on my server from a site either in PHP or JavaScript?
I tried the following:
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
But I keep getting the "failed to copy" message
I created a copy.php file in my var/www/html folder (Using Apache in Ubuntu) and I ran the copy.php file in my browser http://localhost/copy.php
My example.txt is sitting right in the same folder that copy.php
Thanks
You need to set destination folder permissions to 646. Use your domain cpanel or equivalent to do that
In ubuntu you would need to set your folder permissions so navigate to your project directory using cd in the command prompt 'cd /var/www/html/myproject'
var/www/html/myproject
Lets say the folder your want to copy to is located at
var/www/html/myproject/copied_files
now run the command 'sudo chown -R <your username on ubuntu>:www-data <folder name>
It should look like this
Lets say my user name is prince
prince#computer: var/www/html/myproject $ sudo chown -R prince:www-data copied_files
Essentially your giving the group "www-data" which is your server permission to write files to that directory while you still have ownership to it. The '-R' says that all subfolders should be writable by 'www-data' (your server) so you don't have to manually make it writeable and the 'copied_files' is the name of directory for your server to write too :)
echo shell_exec('ls');
works fine.
But trying to copy something:
echo shell_exec('cp index.php temp/index.php');
It doesnt show any error, but it doesnt copy.
I am using nginx with phpcgi.
Try these two commands.
chmod -R 777 /path/of/your/app
chown -R apache:apache path/of/your/app
Try to change the perms of the temp folder. Example:
chmod 777 temp
OR
Check the user you are running shell_exec with and grant him perms to write to temp folder:
echo shell_exec('whoami');
$handle = fopen('/Applications/XAMPP/xamppfiles/htdocs/test/file.txt', 'w');
I tried doing the above and every time I try it, the following statement appeared on my browser:
Warning:fopen(/Applications/XAMPP/xamppfiles/htdocs/test/file.txt)
[function.fopen]: failed to open stream: Permission denied in
/Applications/XAMPP/xamppfiles/htdocs/test/index.php on line 26.
I tried looking through answered questions with the same type of questions but most of the things I tried did not work. For example, writing the full directory...
Maybe, you have no premissions to acces the file. One of the answet, is that, you must change CHMOD to e.g. 777. You can co it with your ftp explorer or with PHP.
chmod("/somedir/somefile", 777);
By default when XAMPP is installed, the htdocs folder does not have any read permissions. You can change the permissions through the terminal like this.
cd /Application/XAMPP/xamppfiles/htdocs/test/
sudo chmod 0664 file.txt
Alternatively, you can recursively set all the permission level of all files and folders
cd /Application/XAMPP/xamppfiles/
sudo chmod -R 0664 htdocs/
You could chmod to 777, but that is risky security. What I'm guessing you really want is change ownership of the file. You can do this using chown. PHP usually runs as user www-data, so you'd run a command something like this.
sudo chown www-data:root path/to/file.ext
If you're file permission on the file was something normal like 664, that'd give PHP the 6 permission (Read and Write) instead of the 4 (just Read).