cannot includ a existing file - failed to open stream: Permission denied - php

this is not a duplicate question, I read all related questions, and didn't find my answer.
I want to include a file that exists :
/var/www/html/monitor/protected/extensions/curl/curl.php
and my code is :
include('/monitor/protected/extensions/curl/curl.php');
and I'm getting this error :
include(/var/www/html/monitor/protected/extensions/curl/curl.php): failed to open stream: Permission denied
and the file has 777 permission.
My question is :
is it possible that a file exists and have a 777 permission and a proper chown AND still give this error?
Update : I had used all three possible ways :
include('/monitor/protected/extensions/curl/curl.php');
include('monitor/protected/extensions/curl/curl.php');
include('/var/www/html/monitor/protected/extensions/curl/curl.php');
Im using php 5.3
Note: when including another file in the same directory, it includes without any problem

I know this is late, but I just want to confirm Alireza Fallah's answer. This also worked for me. It looks like it has to be the original owner. So unzip/unrar the php files on the same machine you want to use it on. Hope this helps!

You're using the wrong path. If the file exists at
/var/www/html/monitor/protected/extensions/curl/curl.php
Then you should include that exact string.
include('/var/www/html/monitor/protected/extensions/curl/curl.php');
/ means the root of the file system. You get permission denied because you most certainly don't have permission to / and /monitor doesn't exist
Alternatively you can include it relatively. By dropping the first / you would do:
include('monitor/protected/extensions/curl/curl.php');
This will work if the running script is also in /var/www/html.
I think you're getting confused at the difference between the HTTP path and the system path. PHP files are included from the system path and should be referenced by the system path.

I deleted the file, and created again, and my problem solved.
the original file was downloaded from the internet and unzipped from a zipped file.
I think there was a problem with creating the file by original author.
This is very odd! but it worked

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.

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!

PHP read file using fopen on windows

What's problem in this code :
$handle = fopen("c:\DIGIPROG\welcome.txt", "rt");
OR
Something problem with another?
I set DIGIPROG folder Full access to everyone but still found error like this :
Warning: fopen(c:\DIGIPROG\welcome.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\digi\test.php on line 1
Then, I tried to open the address directly from run : c:\DIGIPROG\welcome.txt
and it's opened.
My question, is it possible to read using fopen in folder on windows OS ?
Please help with example :)
Thanks
thats surely because the USER running your php executable has no read access to that path.
is a webservice running the php.exe process ? if so, what is the user (use your taskbar) surely it is SYSTEM (by default).
are you running the php script by hand ? then, your user (that used when you login on windows) must have READ permissions in that directory.
in both cases, right click the folder, go to security tab, add read permission to the SYSTEM user (or your user). To make a test only tell ALL USERS have read permission, remember to remove this entry later.

move_uploaded_file() gives a Owner/Group error

I'm using move_uploaded_file() to upload images to the server, however it gives the usual error of:
Warning: move_uploaded_file(upload/file.png) [function.move-uploaded-file]:
failed to open stream: No such file or directory in
/home/newuser/public_html/model/account.class.php on line 39
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move
'/tmp/phpuLkUgE' to 'upload/file.png' in
/home/newuser/public_html/model/account.class.php on line 39
This is not permission based as I have set the folder to 777 with root access and ls -l displays it correctly as this.
755 /home/newuser/public_html/model
755 /home/newuser/public_html/model/account.class.php
777 /home/newuser/public_html/upload
PHP Line
move_uploaded_file($_FILES["photo"]["tmp_name"], "../upload/file.png");
The problem I think is down to the Owner/Group setting being configured incorrectly .. a while back I had all of my sites as subdomains in one account:
/home/olduser/public_html/subdomains/index.html
I then changed this and created a new user account to manage a separate website easier and just moved the files across ...
/home/olduser/public_html/subdomains
/home/newuser/public_html/index.html
The new folders in /home/newuser are now owned and grouped as newuser newuser but I think php may be running as nobody olduser so this could be causing the issue?
What can I try to fix this?
Permissions to a particular file / directory don't just apply on the directory itself, but on the whole path leading up to it.
Example:
/home/ - needs 'x' permission (execute)
/home/newuser/ - needs 'x' permission
/home/newuser/public_html/ - needs 'x' permission
/home/newuser/public_html/avatar/ - needs 'wx' permission (execute + write)
It was kind of touched on indirectly in Silver89's feedback under Jack's Answer, but not outright stated - so I wanted to provide an answer to what helped me with this issue which had me scratching my head for a long time. ;)
The best approach that I have found for the destination of move_uploaded_file() is to use the full absolute path. It can vary based on whether you are on a Unix\Linux server or Windows server, but this should give you the basic idea.
On my Unix server at work, you cannot use "../anything" but have to use the full absolute internal file path of /var/www/html/uploads/imagename.jpg.
So that is why your last test worked for you, Silver89 - because your server was probably trying to upload the image to http://yourservername.com/upload/file.png instead of http://www.yourservername.com/yoursubfolder/upload/file.png. It probably threw out the "../" part altogether and that folder didn't exist on the server.
You can find out what that full path name is by logging onto the server (terminal/ssh etc.) and issuing the 'pwd' command or by using PHP code and echoing the getcwd() command in a stripped php file in the folder where your images will go.
This site is helpful in figuring this out based on your server using different PHP Server Config Checking Functions - See the table midway down. You can simply echo these out to the screen such as:
echo $_SERVER["SCRIPT_FILENAME"].
This was a tough one for me so I hope this makes it a little easier for the next person to find - even if this is 8 months old. ;)

Can't Upload File on IIS

I am on a shared-hosting account with GoDaddy it's a windows Server and I am getting this error when attempting to upload a file:
Warning: move_uploaded_file(D:\Hosting\6903\html\pdfs\ALDOmypdfAP.pdf) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\6903\html\back.php on line 436
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php98C.tmp' to 'D:\Hosting\6903\html\pdfs\ALDOmypdfAP.pdf
I have heard that I can set my php.ini file to change the directory that it's immediately being uploaded to, which would work, however I can't access my php.ini file.
I have tried to create my own php.ini file in the root of my directory, and it causes all sorts of problems, such as not finding the correct MySQL configuration files, goDaddy's support on this was to remove the custom php.ini file, ridiculous, I know.
I have tried to use ini_set like this
ini_set('upload_tmp_dir', 'D:/Hosting/6903/html/pdfs/');
But it hasn't made any effect. Do I have any other options here? Thank you!
UPDATE: From Coda the Octal permissions read 777 of the destination directory.
As far as I know, you need to give the IUSR_MACHINENAME account write permissions specifically on the folder that you are trying to write to, or else it will fail. I don't know what Coda is, but can it allow you to set permissions for specific users?
Had the similar problem.
Fixed it by giving all permissions to usergroup "Authenticated User".
It took me couple of hours to realize it. This post kind of gave me the hint.
I hope my share will help others.
Maybe this can help...
Find and open php.ini in PHP folder (In my case c:\Program files(86)\PHP)
Search for upload_tmp_dir word and do one of two posibilities
--Posibility 1
3.1 Set upload_tmp_dir = "[Some folder with full permission for IUSR user]"
--Posibility 2
3.2 Search in windows explorer for the folder that is setted in upload_tmp_dir (In my case C:\WINDOWS\Temp)
3.2.1 Right click on C:\WINDOWS\Temp -> Properties -> Security -> Edit button -> Add, then add IUSR user and set full permissions to this -> Accept button.
I think this can help someone.
The problem probably comes from the final destination directory more than the temporary directory.
Normally, if the file cannot be uploaded at all, $_FILES['yourfile']['error'] will be set to UPLOAD_ERR_CANT_WRITE (7). From the error message, it tells us the problem is that move_uploaded_file can't either read from the temporary directory (unlikely), or write to the destination directory (most likely).
Check and maybe try changing the permissions on the destination directory.

Categories