Why can PHP 7.3 on Windows rename an open file? - php

I have this code snippet:
<?php
$fh = fopen(__DIR__.'/foo', "w");
fwrite($fh, 'one');
rename(__DIR__ . '/foo', __DIR__ . '/bar');
fwrite($fh, 'two');
echo file_get_contents(__DIR__ . '/bar');
Output when I run this with php-7.1.26-nts-Win32-VC14-x64:
Warning: rename(C:\test/foo,C:\test/bar): The process cannot access the file because it is being used by another process. (code: 32) in C:\test\test.php on line 5
Warning: file_get_contents(C:\test/bar): failed to open stream: No such file or directory in C:\test\test.php on line 8
Output when I run this with php-7.2.15-nts-Win32-VC15-x64:
Warning: rename(C:\test/foo,C:\test/bar): The process cannot access the file because it is being used by another process. (code: 32) in C:\test\test.php on line 5
Warning: file_get_contents(C:\test/bar): failed to open stream: No such file or directory in C:\test\test.php on line 8
Output when I run this with php-7.3.1-nts-Win32-VC15-x64:
onetwo
I think the responsible code is here and here, but I can't identify a change that might be responsible for this behavior.

Related

Why some access modes in fopen() throw a PHP warning on my new server?

I am doing a PHP script migration from php 5.6.40 (Apache 2.2.15 CentOS 6.10 server) to php 8.0.1 (Apache 2.4.37 CentOS 8.3).
I have troubles with fopen() function which throws a warning according to the mode option.
I searched the web without finding similar problem...
On each server, I have the same context and the same permissions :
$ pwd
/export/php/logs/aita
$ ll
-rw-r--r--. 1 apache apache 33 27 janv. 15:30 testFopen.txt
My test-script is very simple, it cheks all fopen modes :
<?php
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r'); // line 2
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r+'); // line 3
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w'); // line 4
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w+'); // line 5
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a'); // line 6
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a+'); // line 7
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x'); // line 8
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x+'); // line 9
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c'); // line 10
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c+'); // line 11
When running this script, I obtain :
On my old server PHP 5.6.40
[error] [client x.x.x.x] PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 8
[error] [client x.x.x.x] PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 9
Thoses warnings are logicals (file exists and exclusion).
On my new server PHP 8.0.1
[proxy_fcgi:error] [pid 37251:tid xxxx] [client x.x.x.x:x] AH01071: Got error '
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 3
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 4
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 5
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 8
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 9
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 10
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 11'
Warnings from lines 8 and 9 make sense here too.
Modes r+, w[+] and c[+] generate the "permission denied" warning.
Modes r, a[+] works well, without warnings.
I am able to read the file with "r" option.
I am able to write the file with "a" and "a+" options so it doesn't seem like a permission problem.
I would like to understand and not have to correct all the fopen() of my applications...
The administrator in charge of the servers has lowered the alert level of SELinux (Security-Enhanced Linux) and everything is now functioning normally.
The system was set up to only allow read/append on files for the Apache user as suggested by IMSoP

iam getting errors as follow Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory

Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/header.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(C:/xampp/htdocs/timetable/footer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/footer.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
When including files that are in the same directory or to several levels higher or lower than the current file, use the __DIR__ constant to get the path to these files.
If header.php and footer.php are in the same directory as index.php, use this code:
include_once(__DIR__ . "/header.php");
include_once(__DIR__ . "/footer.php");
If they are in a subdirectory, e.g. includes then use the code:
include_once(__DIR__ . "/includes/header.php");
include_once(__DIR__ . "/includes/footer.php");
However, if they were in another place, you use such code:
include_once(__DIR__ . "/../../includes/another.php");
The __DIR__ contains the name of the current directory where the php file is located in which the constant is used.

require_once: failed to open stream: Permission denied(lampp)

I need help solving , what says to be a permission error in the htdocs folder, because i needed to alter them to even add folders in the first place.
This is my init.php file:
<?php
//Start Session
session_start();
//Include Configuration
require_once('config/config.php');
//Helper Function Files
require_once('helpers/system_helper.php');
require_once('helpers/format_helper.php');
require_once('helpers/db_helper.php');
//Autoload Classes
function __autoload($class_name){
require_once('libraries/'.$class_name . '.php');
}
?>
I try to include it via `
When i run my index.php file i get this error:
Warning: require_once(../../htdocs/PHP-Wizard/helpers/system_helper.php): failed to open stream: Permission denied in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 9
Fatal error: require_once(): Failed opening required '../../htdocs/PHP-Wizard/helpers/system_helper.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 9
I tried going one folder up with ../ , but it doesn't work.
I looked around for a similar error to mine, but no luck. They all say No such file or directory in (path).
Could it be that it is the same error, or do i really need to change my permissions, if so, how can i do that?
Edit: When i use include_once('helpers/system_helper.php'); i get this error:
Warning: include_once(helpers/system_helper.php): failed to open stream: Permission denied in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 9
Warning: include_once(): Failed opening 'helpers/system_helper.php' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 9
Warning: include_once(helpers/format_helper.php): failed to open stream: Permission denied in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 10
Warning: include_once(): Failed opening 'helpers/format_helper.php' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 10
Warning: include_once(helpers/db_helper.php): failed to open stream: Permission denied in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 11
Warning: include_once(): Failed opening 'helpers/db_helper.php' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/PHP-Wizard/core/init.php on line 11
The problem was indeed the permissions, i'm guessing because i copied the folder.
I fixed it by chmod 777 on all the files in that folder, and now it works fine.
Thank you for your time attempting to help me.
I tested using relative paths and it works fine.
<?php
require_once '../test.php';
echo ' you';
Where test.php just contains echo "hello"; resulted in the expected "hello you".
I would guess your problem is with file permissions. Can you check what permissions are on your system_helper.php? It should be at least executable by the user php is running as (usually www-data). I could reproduce your error message by doing a chown root test.php and chmod 600 test.php so I would guess something like
chown www-data: system_helper.php
or
chmod g+rwx system_helper.php
should give you permission to run the script.

PHP Stop warnings for 1 line of code

I have the following Warnings which SHOULD be there, but is there a way to stop them writting to the page without actually disabling Warnings globally?
Warnings:
Warning: file_get_contents(E:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(F:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(G:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(H:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(I:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(J:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(K:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(L:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(M:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(N:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(O:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(P:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
PHP Function:
//Check if the connection file is present on each drive
function scanDrives() {
//Possible drives
$letters = "DEFGHIJKLMNOP";
$letters = str_split($letters);
$code = md5("FMbHSBTMTXhu3TWp");
//Check for a certain file on each device
foreach($letters as $x) {
$file = file_get_contents($x.":/connected.txt"); //This is what causes the error as the file can't be found.
if ($file != false) {
$file_code = split(":", $file);
//Check if the file has the correct pass code
if($file_code[0] == $code) {
//Successful, return pass value and device letter and set name
echo (1).",".$x.",".$file_code[1];
}
}
}
}
Thanks.
Don't just blindly open the file with file_get_contents. Check to see if it exists first.
if(file_exists($x.":/connected.txt")){
$file = file_get_contents($x.":/connected.txt");
// ...
}
I agree with #Rocket Hazmat,
but to answer your question - in PHP you can use Error Control operators .. Specifically you can use the # sign to ignore errors.
Simple Example
$file = #file_get_contents("file_doesnt_exist.php"); // wont throw any errors.
Try to put "#" before the function that gives you the warning!
http://us3.php.net/manual/en/language.operators.errorcontrol.php

PHP warning from root directory

I have a problem with a php code.
Folder name what-counter This folder contains a file with the following php code named counter.php also the hitcount.txt file.
<?php
$filename = '../what-counter/hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
// Uncomment the next line (remove //) to display the number of hits on your page.
echo $hits;
?>
The following php code is used in root directory files also in files from folders which echo's the hits.
<?php include("../what-counter/counter.php"); ?>
The problem
The code works in the files in folders but not in the files that are directly in the root directory.
Example: index.php is in the root directory
Using this code i get this Warning
<?php include("what-counter/counter.php"); ?>
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 4
Warning: fgets(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 5
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 6
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 10
And using this code i get this Warning
<?php include("../what-counter/counter.php"); ?>
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include() [function.include]: Failed opening '../what-counter/counter.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/what/public_html
/include/footer.php on line 31
What can i do to the $filename url and <?php include("../what-counter/counter.php"); ?> to get it to work in files in root directory as well as folders?
Either:
Include relative the the file: __DIR__.'/../somefile';/__DIR__.'/somepath/somefile';
Include relative to a known dir: $_SERVER['DOCUMENT_ROOT'] is often used, as is a PROJECT_DIR-like define() in your bootstrap code.
Change the working directory to a known/static dir chdir('/some/dir');. Not recommended as calling code may not expect this.
Sounds like absolute vs relative file problems...
Try replacing:
$filename = '../what-counter/hitcount.txt';
With, depending on where the file is located exactly, either of:
$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';
Or (if you're on an old php install) either of:
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';

Categories