PHP fopen() returns NULL, always - php

I'm attempting to open a file for parsing (a binary file), however no matter what fopen() is always returning NULL.
I've ruled out nearly everything to the point where I have a test script with simply:
<?php
$idx = fopen('/usr/home/username/web/appname/dev/www/debug/18194001.idx','r');
trigger_error(var_export($idx,true));
exit();
The output from trigger_error(var_export()); is:
[31-Mar-2013 16:30:34 UTC] PHP Notice: NULL in /usr/home/username/web/appname/dev/www/debug/ajax.idx.php on line 3
No matter what flags I specify for the second fopen() option, I get the same result.
Now, the obvious question is whether or not the file exists, and do I have permissions to read it? The answer to both of those is yes. I've used the relative path and absolute path, both read the file correct. file_get_contents() also reads the file with no issues.
is_readable() and file_exists() both return true
The output of ls -lah for that file is:
-rwxrwxrwx 1 username username 2.0K Mar 30 15:02 18194001.idx
Where 'username' matches the username the web server process and PHP (lighttpd and php-fpm) are running under. The parent directory also has read/read/read rights for user/group/all.
I've tried other files, and I've noticed pretty much anything I throw at fopen is returning a NULL value.
Help?
PHP info:
PHP 5.4.6 (cli) (built: Oct 10 2012 10:43:19)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
with XCache v2.0.1, Copyright (c) 2005-2012, by mOo
Lighttpd info:
lighttpd/1.4.31 (ssl) - a light and fast webserver
Build-Date: Sep 7 2012 15:38:20
OS:
FreeBSD hostname.hostname.hostname 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Thu Feb 17 02:41:51 UTC 2011 root#mason.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64

You cannot meaningfully var_export a fopened resource, you can however var_dump it:
$ php -r 'var_export(fopen("/tmp/a","w+"));'
NULL
$ php -r 'var_dump(fopen("/tmp/a","w+"));'
resource(5) of type (stream)
... because var_export() is meant to 'restore' a variable in PHP script, and resources require more setup then possible in instantiating a simple variable. If you want to know whether the fopen succeeded, just check it is not false.
$ php -r 'var_export(fopen("/this/does/not/exist","w+"));'
... some errors...
false
In other words, the fact you get NULL from a var_export means the fopen actually was successful.

Related

Local IIS installation doesn't show phpinfo page

I've installed PHP on my windows 10 computer, and started IIS.
But when I browse to http://localhost/phpinfo.hml the page is completely blank.
And if I look at the file view in IIS, it seems that it's not aware that the file exists.
I've verified that the phpinfo.html file is in the right place:
ls -l C:\inetpub\wwwroot\phpinfo.html
Directory: C:\inetpub\wwwroot
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 7/16/2020 8:54 AM 72 phpinfo.html
And has the right contents:
PS C:\inetpub\wwwroot> cat .\phpinfo.html
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
PHP is installed:
php --version
PHP 7.4.1 (cli) (built: Dec 17 2019 19:23:59) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
I gave my windows user full access to that directory. What am I doing wrong? How can I get this working?
If you want to show phpinfo page, you need to change phpinfo.html to phpinfo.php.
And you also need to handle PHP requests by using IIS Manager, you can set this in Handler Mappings.
More information about how to configure IIS to Handle PHP Requests you can refer to this link: Configure IIS to Handle PHP Requests

how to change the php from the default folder to the php 71 folder

this my /usr/local/ folder
this my php version showing`
PHP -v
PHP 5.5.38 (cli) (built: Mar 30 2017 12:11:07)
Copyright (c) 1997-2015 The PHP Group Zend Engine v2.5.0,
Copyright (c) 1998-2015 Zend Technologies`
this is my PATH
echo $PATH
/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
What i want to do is switch to my php72 folder so when i type php -v to show me php 7.2
I have searched a lot and tryed many solutions but it is not working.
any help would be appreciated
Go in the Finder and press ⌘⌥P to turn on the display of the path at the bottom of the screen - no idea why Apple leaves this turned off (rant over).
Now navigate to /usr/local/php5-7.2ORC.... and navigate around in there till you find the executable called php. Note the path at the bottom of the screen when you have that file selected. Remove the word php from the right end of that path, now set your PATH like this:
export PATH=<WHATEVER-YOU_GOT_ABOVE>:$PATH
In essence, you will end up doing something like:
export PATH=/usr/local/php5-7.20ORC.../bin:$PATH

Can't execute a php script without using "php" command before

I need to use a php script without "php" command.
For example:
$ ./test.php
Permissions are sets to 755.
This is the script
#!/usr/bin/php -q
<?php
echo "hello world";
?>
/usr/bin/php -v (so path exists)
returns
PHP 7.0.15-1+deb.sury.org~xenial+1 (cli) (built: Jan 20 2017 08:53:13) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.15-1+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
This is the error I'll get everytime:
Exception: Zend Extension ./test.php does not exist
Also calling script with fullpath I'll get same error.
Calling this it works properly
$ php ./test.php
Any idea?
NOTE: The author found the solution and put it up in the comments but never posted an actual answer, so this answer is just clarifying what the author already said above so as to make the answer more obvious.
I was also getting the Exception: Zend Extension does not exist when I was trying to pipe an email via cpanel forwarder into a php script.
I opened the file in my editor (Komodo Edit on Windows) and went to EDIT > CURRENT FILE PREFERENCES and noticed that LINE ENCODINGS was set to DOS/Windows (\r\n)
I changed the LINE ENCODING to UNIX (\n) and saved it and re-uploded it and the error went away and all is good now.
Obviously the steps will vary depending on what editor you use, but the solution is to make sure your Line Encodings are UNIX and not DOS/Windows.
Just run dos2unix on the file
# ./database.php
Exception: Zend Extension ./database.php does not exist
# apt install dos2unix
# dos2unix database.php
dos2unix: converting file database.php to Unix format...
# ./database.php
Yeah!!! It work's!!!!

Moved Wordpress Website, Now it Cannot modify header information

I moved my Wordpress-based website from an Apache server to a Nginx server, and something, somewhere went wrong.
I'm getting a mangled website (as in: CSS isn't being loaded so the lay-out is completely wrong). When I try to access /wp-admin to log in, I get:
Warning: Cannot modify header information - headers already sent by (output started at /var/hpwsites/u_potgieter/website/html/webroot/wp-config.php:1) in /var/hpwsites/u_potgieter/website/html/webroot/wp-login.php on line 390
Warning: Cannot modify header information - headers already sent by (output started at /var/hpwsites/u_potgieter/website/html/webroot/wp-config.php:1) in /var/hpwsites/u_potgieter/website/html/webroot/wp-login.php on line 403
I was also getting error messages from WP Super Cache plugin, but I was able to disable that from PHPmyAdmin.
Looking at the website (www.potgieter.nl), and checking firebug, I get the impression the PHP isn't being executed properly. As far as I understand Wordpress, it uses PHP to call things like posts, pages and CSS. Now the text, Javascript and images are being called correctly and are displayed. Even the slider works. However, the sizing, positioning and color are all horribly wrong.
I ran ps -ef from the SSH, and it showed PHP is running:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jun17 ? 00:00:30 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
u_potgi+ 19752 19744 0 16:29 ? 00:00:00 sshd: u_potgieter#pts/0
u_potgi+ 19753 19752 0 16:29 pts/0 00:00:00 -bash
u_potgi+ 19924 19919 0 16:44 ? 00:00:00 sshd: u_potgieter#notty
u_potgi+ 19925 19924 0 16:44 ? 00:00:00 /usr/libexec/openssh/sftp-server
u_potgi+ 20774 19753 0 17:08 pts/0 00:00:00 php
u_potgi+ 20781 20776 0 17:10 ? 00:00:00 sshd: u_potgieter#pts/1
u_potgi+ 20782 20781 0 17:10 pts/1 00:00:00 -bash
u_potgi+ 20804 20782 0 17:10 pts/1 00:00:00 ps -ef
I'm runnning PHP7
~]$ php -v
PHP 7.0.5 (cli) (built: Apr 19 2016 12:44:23) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
I have seen the question linked to below, and I've tried to follow the steps outlined there as best I could, but to no avail.
How to fix "Headers already sent" error in PHP
I am looking for the reason the website isn't loading properly, so I can start looking for a way to solve the issue.
Any and all help would be greatly appreciated.
Edit: this is the first bit of my wp-config.php. There is no whitespace on line one, nor is there any other HTML before the <?php tag.
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {#link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* #package WordPress
*/
I'm not sure how well you checked the question you linked because it's answers are quite clear.
The error message is also clear wp-config.php:1
You are looking for something that will cause output to be produced on line 1 of wp-config.php
You probably have whitespace before the opening php tag
eg
<!-- empty line here -->
<?php
...
..

PHP SDK won't run my php-cgi

I'm trying get the helloworld example for the Google Appengine PHP SDK working, but I can't seem to get the dev_appserver to even run. This is the command line I use and the error I get:
$ /opt/google-appengine-php/dev_appserver.py --php_executable_path=/usr/bin/php-cgi src
INFO 2013-06-14 02:50:09,070 sdk_update_checker.py:244] Checking for updates to the SDK.
INFO 2013-06-14 02:50:09,331 sdk_update_checker.py:260] Update check failed: HTTP Error 404: Not Found
INFO 2013-06-14 02:50:09,458 api_server.py:138] Starting API server at: http://localhost:39069
INFO 2013-06-14 02:50:09,647 dispatcher.py:164] Starting server "default" running at: http://localhost:8080
INFO 2013-06-14 02:50:09,650 admin_server.py:117] Starting admin server at: http://localhost:8000
ERROR 2013-06-14 02:50:09,717 php_runtime.py:199] The PHP runtime is not available because: No input file specified.
The output from php-cgi -v is:
PHP 5.4.16 (cgi-fcgi) (built: Jun 7 2013 05:55:42)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
The output from whereis php-cgi is:
php-cgi: /usr/bin/php-cgi
I looked at the php_runtime.py file but I can't even find the string No input file specified so I'm stumped!
Anyone have any idea what the problem is?
EDIT: I just tried running fgrep -r 'No input file specified' * in the App Engine directory and nothing shows up!!!
EDIT 2: It seems the message No input file specified comes from the PHP interpreter, no the App Engine SDK. But I still have no idea why?! the app.yaml and helloworld.php files are in the same directory and are copied from the tutorial page.
It seems the problem is the way I installed my system. My /home directory is actually a symbolic-link to /var/home (I have two partitions: / and /var) but PHP has a directive to limit where it loads files from, open_basedir. By default it includes /home but PHP resolves symlinks so all my files were coming from /var/home and /var (or anything below) are not included.
So the solution is to replace /home with /var/home in the open_basedir directive of my /etc/php/php.ini file.
EDIT: Also, since I installed the AppEngine PHP SDK in /opt/google-appengine-php I had to add that directory to open_basedir too.

Categories