From a similar question I used this code to debug the failure of opening a file
echo'<pre>';
error_reporting(E_ALL);
ini_set('display_errors', true);
echo 'phpversion: ', phpversion(), "\n";
echo 'uname: ', php_uname("s r"), "\n"; // name/release of the operating system
//echo 'sapi: ', php_sapi(), "\n";
echo $file, file_exists($file) ? ' exists' : ' does not exist', "\n";
echo $file, is_readable($file) ? ' is readable' : ' is NOT readable', "\n";
echo $file, is_writable($file) ? ' is writable' : ' is NOT readable', "\n";
$fp = #fopen($file, 'x');
if (! $fp) {
echo 'last error: ';
var_dump(error_get_last());
}
echo '</pre>
I get this helpful message
D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0
phpversion: 5.3.1
uname: Windows NT
D:\data\openid\nonces\4d895...FSRy0 exists
D:\data\openid\nonces\4d895...FSRy0 is readable
D:\data\openid\nonces\4d895...FSRy0 is writable
last error: array(4) {
["type"]=>
int(2)
["message"]=>
string(188) "fopen(D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0) [function.fopen]: failed to open stream: File exists"
["file"]=>
string(38) "D:\web\library\Utils.php"
["line"]=>
int(179)
}
"failed to open stream: File exists" - What could that mean?
This happens because you used the 'x' flag to fopen(). The PHP manual says about it:
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
You probably want to use fopen($file, 'c') or just the ordinary w mode.
Related
Here is the error message that my code is giving me:
Warning: fopen(users.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/assignment3/form.php on line 79
Unable to open file.
Here is my code:
$file = fopen("users.txt", "a") or die("Unable to open file.");
I've just started coding with mac so I don't know very much about it. thank you in advance for your help
Run this code to find correct folder easily
<?php
$txt = 'users.txt';
echo '<br> your php file is here : ' . __FILE__;
echo '<br> your text file should exists in : ' . __DIR__ . '/' . $txt;
echo '<br> your text file '.( file_exists($txt)?'exists':'not exists' );
echo '<br> your text file is '.( is_readable($txt)?'readable':'not readable' );
echo '<br> your text file is '.( is_writable($txt)?'writable':'not writable' );
echo '<br> your dir is '.(
is_writable(__DIR__)?
'writable, then you can create new files':
'not writable, then you can not create any files'
);
Demo
I'm trying to write to file, but PHP tells me that it's forbidden.
I tried to change permissions, assigned files to group 'apache' (according to <?php echo whoami(); ?>), but it didn't work.
Here is result of my file:
phpversion: 5.4.16
uname: Linux
exists // echo file_exists($file) ? ' exists' : ' does not exist', "\n";
is readable // echo is_readable($file) ? ' is readable' : ' is NOT readable', "\n";
is NOT writable // echo is_writable($file) ? ' is writable' : ' is NOT writable', "\n";
Warning: fopen(system/cache/cache.currency.1417623063): failed to open stream: Permission denied in /var/www/html/test/test.php on line 23
last error: array(4) {
["type"]=>
int(2)
["message"]=>
string(87) "fopen(system/cache/cache.currency.1417623063): failed to open stream: Permission denied"
["file"]=>
string(30) "/var/www/html/test/test.php"
["line"]=>
int(23)
}
Can you help me, please?
Which fopen mode you set? to read file enought fopen("/var/www/html/test/test.php", "r");
It was SELinux. It blocked all file changes, so I was able to read, but not write.
ANSWER I WENT WITH BELOW!
So I have a PHP script that works prefect via the web. I would like to set it up on a scheduled task on the server that is running the web hosting. It is a windows 2008 R2 server. I seems to run fine minus the fact it won't make the output files. Do I need to have the full path name? Ex C:\logs.... or will below work? I would like to keep the script working both in web and command line.
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen($File3, 'a+');
$Data3 = "blah";
fwrite($Handle3, $Data3);
more info:
here are the errors from the log file:
fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 686
PHP Warning: file_get_contents(bins/bins-10-09-2013.txt): failed to open stream: No such file or directory in C:\Websites\wordpress\win\import.php on line 692
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 699
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 709
WORKING!! Well, this is what I went with and it seems to work prefect.
$File = "" . $uploads_dir . "/import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($file1, 'a+');
if ( $Handle === false ) {
$File = "C:\Websites\wordpress\win\\".$uploads_dir."\import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($File, 'a+');
}
This way I can Use the php script both via web and on a scheduled task via windows.
It seems fopen is failing to open the file . Make sure the file is successfully opened :
$Handle3 = fopen($File3, 'a+');
if( $Handle3 === false ) echo 'Unable to open file';
If it fails , check below items one at a time :
Check file / folder permissions .
Check with different slashes "/" , "\" in the path .
Check if it works with absolute path ( full path ) .
Check by changing directory before fopen : chdir('path\to\php\script')
etc .
If the issue is with the path , one way make it work both cases is :
// First try with the path that works from web
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen( $File3, 'a+' );
if( $Handle3 === false )
{
// It failed so may be scheduled task invoked the script
// So use the path that works in invoked by scheduled task
$File3 = "path\that\works\from\command\line";
$Handle3 = fopen( $File3 ,'a+' );
}
if( $Handle3 === false )
{
echo 'Failed to open file';
exit;
}
else
{
// What ever
}
I've never used the full url for an fopen or fwrite. These are the things I would look at first:
What is the output when you echo $log_dir?
Does the directory exist?
Are permissions set correctly?
If you create the file before running the script, does it then successfully write to the file?
What is the value of $Handle3 after the fopen?
What is the return value of the fwrite that you call on the last line?
Is error handling set to all? (it should be for debugging)
Since the title of this post is pretty much self-explanatory, I'll just jump to the code :
echo sprintf('%o', fileperms('test.txt'))."<br/>";
fopen("test.txt", "w");
And with this I get :
100777
fopen(test.txt): failed to open stream: Permission denied
Any ideas ?
Edit : Problem solved : there were access control lists on the server that were not configured correctly.
Thanks !
I think its possible that you have write/read permissions on the file but not on the folder. Try this in the public root of your website and see if you can read or write the file.
For safe mode (http://php.net/manual/en/function.fopen.php), php doc's say the following:
Note: When safe mode is enabled, PHP checks whether the directory in
which the script is operating has the same UID (owner) as the script
that is being executed.
Last you also need to be sure that php has access to the folder you are trying to write to.
I had same issue: folder was 777, but fopen does not worked. fopen said permission deny. Make sure your script have a 'good' permissions. maybe it will help you:
echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
var_dump(error_get_last());
}
I think the problem you are having is file ownership issue ... you can use this to find out the problem
error_reporting(E_ALL);
ini_set('display_errors','On');
$file = "a.jpg";
echo sprintf ( '%o', fileperms ( $file ) ), PHP_EOL;
echo posix_getpwuid ( fileowner ( $file ) ), PHP_EOL; // Get Owner
echo posix_getpwuid ( posix_getuid () ), PHP_EOL; // Get User
if (is_file ( $file )) {
echo "is_file", PHP_EOL;
;
}
if (is_readable ( $file )) {
echo "is_readable", PHP_EOL;
;
}
if (is_writable ( $file )) {
echo "is_readable", PHP_EOL;
}
fopen ( $file, "w" );
I just ran into this issue, and unfortunately the error message provided no clue to the actual reason. I had to give 777 to the file of the class included in the file that gave the error message. The error message only said the php file that calls that class, which already had 777.
So, check the file that is mentioned in the error message (let's say index.php), and then check which classes are instantiated within that file (class-file.php, class-writer.php, etc). Then check the permissions of those files (class-file.php, class-writer.php).
Once I gave permissions for the class file, it worked normally. Perhaps the webhost changed something in their config, since everything worked until a few days ago.
I'm creating a php script that connects to a 3G modem connected via serial connection on COM5.
I'm getting the following error and I believe it is because php does not have r/w access to COM5:
Warning: fopen(COM5:) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\SMStest\test2.php on line 9
// mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off
$fp = fopen ("COM5:", "w+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
$e = chr(27);
$string = $e . "A" . $e . "H300";
$string .= $e . "V100" . $e . "XL1SATO";
$string .= $e . "Q1" . $e . "Z";
echo $string;
fputs ($fp, $string );
fclose ($fp);
}
There are many ways to access COM ports on windows, alternatives to your method are opening it with the following paths:
\Device\00000123 (You can find the correct value in device manager, properties, details, physical device object name)
\\.\com5 (This is how I would open the port as a file if I was writing a program in C or something)