How to create or edit file content on root - php

I have one website, not i want when i upload this site on live server it should ask for database username, password, site url..etc. Something like website installer. It is working fine in LOCAL system.
Now my problem is: i need to change RewriteBase in .htaccess file. But this file is located at root of site. So i am getting permission issue to create new file or edit/copy file which is located at root.
Warning: rename(generate.htaccess,.htaccess): Permission denied
Warning: copy(.htaccess): failed to open stream: Permission denied
What should i do. Please help.
Thanks.

To Create/Modify file on root directory you must have to give 0777 permission to your root directory. This is not a good practice.
I suggest create empty .htaccess or other required file with 777 permission initially. Before writing content in that file first check write permission after that copy content from source file (in your case from where you trying to copy).
Hope this may help.

Related

Running a PHP program in Ubuntu - fopen("filename", "w"): Failed to open stream: Permission denied

I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:
fopen("colors_new.csv", "w");
From the manual,
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
But instead of the file being created, I am getting
Warning: fopen(colors_new.csv): failed to open stream: Permission denied...
And when I look into the project folder, the colors_new.csv file is NOT created.
When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.
How do I fix this problem in Ubuntu?
WHAT I TRIED:
I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.
NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.
This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work
give filewriting permissions to the directory.
Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.
create a subdirectory inside your directory and give that sufficient rights (755) to do what you want.
F.i. Fopen('./files/colors_new.csv)
Provide the write permission on the folder in which you want to write the file. Your problem will be solved.

PHP - Issue with move_uploaded_file

I've used move_uploaded_file successfully in the past, however I'm having an issue with permissions I believe with a new server I'm testing this on. I've created a folder named "uploads" for the uploads to be moved to via FTP. Here's a screenshot showing the permissions:
Everyone has read/write access, but when I upload a file I'm getting an error on the line that moves the file to the "uploads" directory:
if (move_uploaded_file($_FILES['photo1']['tmp_name'], $upload_directory.$uploadedFile)) {
The file is not moved to the "uploads" folder and looking at the server log I can see entries like this:
php[1344]
PHP Warning: move_uploaded_file(uploads/2.jpg): failed to open stream: Permission denied in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\test_upload\index.php on line 206
PHP Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php2D93.tmp' to 'uploads/2.jpg' in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\ test_upload\index.php on line 206
I can't see how it can be a permissions issue if all users have read/write/execute on the "uploads" folder? Here are the variables:
$upload_directory='uploads/';
$uploadedFile= $_FILES['photo1']['name'];
It appears that uploads/2.jpg is $_FILES['photo1']['tmp_name'] and should therefore have been uploaded to the server's /tmp/ directory. On most systems, because /tmp is a system dir, it has restricted permissions on it.
See if you can alter the perms on /tmp or find out exactly which dir the tmp uploaded file is being moved to first, before being "uploaded" (and then alter the perms on that dir).
Regardless, the path to the file as passed to move_uploaded_file() doesn't look right. It needs to be a full-path. Possibly the server-error you're seeing is not happening at precisely the same time as the upload operation takes place.
I wasn't able to fix this via FTP settings. I had to adjust the settings by logging in to the Windows Server and changing the security settings on the folder I was trying to move files into so that the "Users" had read/write access.

Error Uploading images in drupal

I was trying to upload a custom logo to my drupal site which runs on IIS 8 with PHP.
I got the following upload error :
" Warning: move_uploaded_file(temporary://logo.png): failed to open stream: "DrupalTemporaryStreamWrapper::stream_open" call failed in drupal_move_uploaded_file() (line 1642 of C:\inetpub\wwwroot\drupalfull\includes\file.inc).
Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php25C3.tmp' to 'temporary://logo.png' in drupal_move_uploaded_file() (line 1642 of C:\inetpub\wwwroot\drupalfull\includes\file.inc).
File upload error. Could not move uploaded file."
Please help me. I have set both read and write permissions on IIS but still it does not work.
Create sites/default/files/tmp folder and give full permissions (777) and change 'Temporary directory' at admin/config/media/file-system.
I think it fixes yout problem.
Regards.
If you are running ISS8 (web server) then the file path is different then the default for Unix.
Make sure you have the temporary folder setup correctly on your settings in Drupal and the folder has write permissions.
You can follow this article at step 5 and 6.
Create the folder:
Drupal uses the Sites\Default\Files directory to store temporary files and therefore must be able to write and modify files in this folder.
Create the folder:
C:\inetpub\wwwroot\Drupal>md sites\default\files
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/install-drupal-on-iis
Your problem is most likely the permission problem inside drupal. Since you are using windows system, you need to make sure the folder sites/default/files have writable permission for the IIS server. Config the permission to be writable for all user and see if it is working and then limit the permission step by step until you feel comfortable.
I'm going to put another answer in here because I guarantee I'm going to forget how I fixed it!
Basically, on Windows, the default upload folder for PHP is C:\Windows\Temp. As part of the Drupal upload, it moves files from the PHP upload folder to the Drupal Temporary//: folder. There is an issue however in PHP whereby the PHP executable needs read permission to the full path to the uploaded files, so C:\Windows\Temp. If this is not given, it will error out and the file will not be moved.
Details are at http://php.net/manual/en/function.realpath.php which is a function call made deep in Drupal's upload file code.
My fix was to create a folder elsewhere, say C:\PHPTemp, and modify PHP's settings (session.save_path and upload_tmp_dir) to point to this folder instead. After giving IIS_IUSRS full control (overkill? probably!), I was then able to upload images!

apache php file_put_contents permission denied

The php script is unable to create file owing to denial of permissions.
PHP Script:
<?
echo exec('whoami');
file_put_contents('/var/www/html/sample.txt','Some random Text');
?>
Output :
apache
**Warning**: file_put_contents(/var/www/html/test/samds.txt): failed to open stream: Permission denied in **/var/www/html/index.php** on line **3**
The directory permissions are all 777
I have had same issue and i misplaced some configuration.Firstly remeber that the user who created the php file can read the file and if the user who created php file is different than the apache user in your case it is apache in that case you will get this exception if you try to write from different users.So what you have to do is simply,and i would suggest you to revoke the 777 permissions as i do not like idea of your files being publicity available on read and write to the world,so get it back to 755 and do the next on the command line of your server.
cd /var/www
change the user of the HTML only as we need it to be writable only,so do not touch the var and www,so basically as soon as you have navigated to he www directory you are going to change the HTML directory owner to the one who is currently running the apache server,and you got that info already,in your case it is apache so we are going to do the next,
chown apache:apache html
At this stage the html file owner will be apache so you will be able to write onto it.Anyway and meanwhile i would strongly suggest you to create separate directory inside html lets say testDir and try there,i would not suggest to change the root folder permissions,i had bad experience with them.Anyway i hope it will help as i suffer more then hour and just now got the right way to do it.This got my issue fixed,hope it will fix your's too.!

How to write a text file

test.php opens test.txt and writes to it. It works fine on one hosting server.
After migrating the code to another host, test.php now can't write out test.txt. The error shows:
Warning: fopen(/home/username/public_html/test.txt)
[function.fopen]: failed to open stream: Permission denied
It will only work when the test.txt file is set to 777. On the original host, test.txt was set to 755.
What am I missing to give permission for test.php to write to the file without 777 on this new host?
Also, file upload PHP scripts aren't working either.
It is not just the permission that is important, but also who owns the files. 755 is enough if the folder/file is owned by the same user that is used for running the web server.
Writing directly to the public_html directory is a huge security risk. I suggest creating a subdirectory (probably best outside the public_html) folder and give it the appropriate permissions / ownership.
It is safer to have the folder owned by the user that runs apache (or whichever web server is being used) - usually something like apache or www-data instead of giving world write permissions.

Categories