Warning: proc_open(): Unable to create temporary file - php

I am working on a local server and getting two warnings:
Warning: proc_open(): Unable to create temporary file. in C:\xampp\htdocs\ *** on line 159
and
Warning: proc_open(): cannot represent a stream of type TEMP as a File Descriptor in C:\xampp\htdocs\ *** on line 159
My code is:
$err_stream = fopen('php://temp','rw');
$stdout = fopen('php://stdout','w');
$process = proc_open(self::PHP_8_0_COMMAND,[
0 => array("pipe", "r"),
1 => $stdout,
2 => $err_stream
], $pipes);
I've searched for the solution but I found nothing. I tried to change
$err_stream = fopen('php://temp','rw');
to
$err_stream = fopen('php://memory','rw');
but then I'm still getting the second warning.

Ok, I found out the answer.
The temporary directory was actually full and I just had to delete most of the files stored there.
I'll try to implement a function deleting old temporary files.

Related

Why can PHP 7.3 on Windows rename an open file?

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.

Can't open anything with fopen

I was trying to set up some fopen function
$path = 'php://testdir';
$h = fopen($path, "rw+") or die("Error");
fwrite($h, "test");
fseek($h, 0);
echo stream_get_contents($h);
For some reason there's still an error. I've done following steps to fix this:
checked php.ini and switched on allow_url_fopen, save_mode + restarted apache
added even chmod 777 (that's test passwd protected server, so done it temporarily)
was even try to fopen some existing file in the same location but still getting an error
error_reporting doesn't show anything (when removed die statement to test) but logs are showing:
[error] [xxx] PHP Warning: fopen(): Invalid php:// URL specified in /var/www/xxx/xxx/all.php on line 24, referer: http://xxxxxxxxxx/xxxx/all.php
[error] [xxx] PHP Warning: fopen(php://testdir): failed to open stream: operation failed in /var/www/xxx/xxx/all.php on line 24, referer: http://xxxxx/xxxx/all.php
'xxx' changed by myself here.
That's not what php:// is for. It allows access to specialized streams such has stdin/stdout/etc, not regular file access.
Don't bother using php:// - your fopen() call should just have the path to the file. Something like fopen('/path/to/testfile.txt')

PHP Can't find file

Problem 1:
I got this code
<?php
$count_my_page = ("adminpanel/hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
?>
Its in a file called /beta/ but i need it to find the folder /adminpanel/ i tried using /../ and // it did not work. I created the error reporting file and this is the output:
Warning: file(//adminpanel/hitcounter.txt): failed to open stream: No such file or directory in /home/u463251352/public_html/beta/index.php on line 3
Warning: fopen(//adminpanel/hitcounter.txt): failed to open stream: No such file or directory in /home/u463251352/public_html/beta/index.php on line 5
Warning: fputs() expects parameter 1 to be resource, boolean given in /home/u463251352/public_html/beta/index.php on line 6
Warning: fclose() expects parameter 1 to be resource, boolean given in /home/u463251352/public_html/beta/index.php on line 7
How do i fix this?
You should use an absolute path based on magic constants.
For 5.3.0 or higher, you can use:
__DIR__
For PHP lower than 5.3.0, use:
dirname(__FILE__)
Example:
$file = __DIR__ . '/dir/file.txt';
Will take:
Root
dir
file.txt
or you can use
$_SERVER['DOCUMENT_ROOT'].'/adminpanel/hitcounter.txt';

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';

PHP fopen() does not create a file

According to fopen documentation, in some modes it will create the file if it does not exist, but in my situation, I've checked all 'w', 'w+', 'x' and 'x+' modes but it's just throwing warnings at me and it cannot create the file.
It's my code:
$this->handle = fopen($this->log_name, 'w');
and what I get:
Warning: fopen(D:\xampp\htdocs\farid\logs\error.php) [function.fopen]: failed to open stream: No such file or directory in D:\xampp\htdocs\farid\libraries\error\log.php on line 34
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 66
Warning: fclose() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 27
And I'm working in windows environment.
Check if the path to the logfile exists, it creates a file, not a directory.
Also check if the user, which runs xampp, has access to the folder you specified.

Categories