Unable to read files with PHP - XAMPP - php

I generally work with WAMP but for some reason had to switch over to XAMPP.
Ran into this really weird problem. When i try to read a file using file_get_contents i get an error saying that the file/dir does not exist EVEN THOUGH IT DOES.
Even if something as simple as
file_get_contents('x.txt');
throws an error
Warning: file_get_contents(x.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\cryy\index.php on line 3
And yes, i've tried using the magic constant
__DIR__
to no avail.
I'm using XAMPP 3.2.2 and PHP 7.0.18, running on Windows 10.
I hope someone can help me figure this out.

Try providing full path of the you want to get content. If you want to access file from root of the folder, then you can use
file_get_contents($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."project_folder".DIRECTORY_SEPARATOR."filename.txt");
OR
file_get_contents(__DIR__.DIRECTORY_SEPARATOR."filename.txt");

Related

xampp php - link to directory not in root

I am running XAMPP on Win7 SP1 and I have encountered a problem.
I have a dir structure similar to this:
htdocs
_ head.php
runes
_ algiz.php
From inside of algiz.php I want to include the files head.php since I have a couple of includes in there like my style sheets, etc. If I run a file in the root that includes the head.php I have no issues.
However, if I try to run the algiz.php I get an error when it tries to access head.php. I am using require "../head.php";
When I try this I get the following errors.
Warning: require(head.php): Failed to open stream: No such file or directory in E:\XAMPP\htdocs\runes\algiz.php on line 3
Fatal error: Uncaught Error: Failed opening required 'head.php' (include_path='E:\XAMPP\php\PEAR') in E:\XAMPP\htdocs\runes\algiz.php:3 Stack trace: #0 {main} thrown in E:\XAMPP\htdocs\runes\algiz.php on line 3
I even tried to visit the dir htdocs/runes from my browser and I saw algiz.php listed. Then I clicked on the algiz.php and got the same errors.
EDIT: I thought that it may not be clear, but the runes dir is a sub in the htdocs folder.....
So, any insight or suggestions are greatly appreciated.
Jim
I saw the require(head.php) also... Not sure why it is showing that.
Just for the hell of it, opened the file in M$ Office and set show nonprintable characters. Only thing that showed was the paragraph mark at the end of the line.
Deleted the file off of my server. But, reloaded the local file and retyped the line anyway, same issue.
Opened the file using my FTP and retyped the line using Notepad, same.
Opened my site locally using XAMPP, same issue.
There are two things driving me completely nuts. One is the error message. Two is not being able to figure this out...
EDIT: I don't think this should make a difference, but I am using PHP version 8.0.15
Your warning only says require(head.php) not require(../head.php)
Are you sure you're actually using require "../head.php ?
Maybe you got some weird character in there? Try rewriting the line, just to be safe.

require_once in PHP giving errors on new server, but not on original

I am in the process of moving a PHP website from a shared hosting server to a DigitalOcean server. I have come across an issue that I'm not sure how to debug.
There is a page that has a log-in form with action=2/login.php. This form is located in /var/www/html/calendar.php (it is a Ubuntu 14.04 DO droplet). When submitting the form, there is a 500 Internal Server Error. I've found the error in question but I am not sure how to debug it (because it runs fine on the other server).
/var/www/html/2/login.php
<?php
require_once('../db_connect.php');
...
db_connect.php is located at /var/www/html/db_connect.php
When I $php 2/login.php I get the following error:
login_error.txt
PHP Warning: require_once(../db_connect.php): failed to open stream: No such file or directory in /var/www/html/2/login.php on line 4
PHP Fatal error: require_once(): Failed opening required '../db_connect.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/2/login.php on line 4
When I run $php login.php when in directory /var/www/html/2 it works fine (which I guess makes sense, as ../db_connect.php is a relative path).
I want to know why it is working on the other server and not the DO server?
The php.ini is the same (which could be a problem), but on Server #1 (Verve), it is shared hosting, so our root is /home/[username]/public_html and on the new server is is /var/www/html.
I can link to the live sites in question if necessary (but would prefer not to). The /var/www/html/2/login.php and the /var/www/html/calendar.php scripts are exactly the same, and while I understand this will definitely cause issues later as the absolute path to the root of the web application is different, I don't know what would be causing this specific issue.
I have also looked at this question which is very similar: PHP require_once resetting to current working directory. However, this code works on the other server, and not here, so I think there is something more than just a lack of a relative file path, and I would prefer to find the solution that explains why the script isn't working rather than patching the script, as I'm pretty sure I'd have to change every single other script that uses require_once('../db_connect.php') as well.
Thanks for any help!
Did you try using __DIR__?
require_once(__DIR__ . '../db_connect.php');

Deploying Laravel Over FTP

I am working on deploying a Laravel app to a bluehost server and I am running into some trouble.
Currently, I uploaded all of the Laravel files to the root of the server, then uploaded the contents of the "public" folder to the "public_html" folder. From here I opened the "bootstrap/paths.php" file and changed the public path to
'public' => __DIR__.'/../public_html',
After this, I am still unable to get content to display. Am I missing something? It looks like bluehost is running PHP 5.4.24.
Any help with this would be appreciated.
Thanks!
EDIT: The error log shows
[09-Apr-2014 09:04:02] PHP Fatal error: require() [function.require]: Failed opening required 'DIR/../bootstrap/autoload.php' (include_path='.:/usr/lib64/php:/usr/share/pear') in /home1/regmuel/public_html/index.php on line 21
Could anyone help me figure this error out. I can clearly see what the problem is, I'm just not sure how to go about fixing it.
It looks like __DIR__ isn't getting parsed, are you sure your host isn't running PHP 5.2.* if you run phpinfo() on your host what version does it give?
__DIR__ is only available from PHP 5.3.0 onwards
RMcLeod's answer is perfect, just wanted to add to it something that I ran into with my web host (specifically Bluehost). Even after the version of PHP running on the server was 5.4.40 (found using php -v via an SSH terminal), I was running into the same problem where it was picking up an older version of PHP when interpreting my index.php and wouldn't recognize the DIR constant.
I had to add the line
AddHandler application/x-httpd-php54s .php
to the top of the .htaccess file which is in the same folder as my index.php
Just for context, I was trying to set up my laravel 4.2 app on bluehost and it relies heavily on this constant.

Weird PHP error on server

All of a sudden, my SMF powered forum has stopped working.
In fact, PHP files no longer seem to want to open on my website. I tried uploading a blank PHP file with just a few characters of text, and it refuses to open giving the error below.
I also tried a PHPinfo file, which didnt work, giving the same error. Other sites on the same server are working.
Fatal error: Unknown: Failed opening required '/home/users/a/n/mysite/www/index1.php' (include_path='.') in Unknown on line 0
Any ideas about whats gone wrong?
Just an idea. As it happens to all php files that could to what i know only mean 2 things.
Either there is somekind of error on the php extension.
OR what it most likely is, is that SMF uses a .htaccess to make user friendly links. Which means all requests to the server no matter what (Unless specified in the .htaccess) will be sent to index.php and then be handled.
So if there is a error in the index.php or the library of some sort it would explain why no matter what php file you're looking at it gives the error.
-
One thing you could do is to take a backup of all your images in the system. Then try to install a fresh version on your local machine. Once done copy/past the images back and copy all the current php files for SMF and replace the current ones on your host with them.
-
Haven't used SMF much so I'm not sure this is the solution at all. But to me it sounds like a solution.
It seems you have broken your root document.
do chmod 755 index1.php
and it should solve the problem.
You should check the php.ini or php5.ini file. I don't believe the single ticks around '.' for the include_path are valid. Double quotes will work however.
It seems you have been hacked:
http://wordpress.org/support/topic/fatal-error-unknown-failed-opening-required-google_verifyphp
Ahh sorry reread the question. I guess this is what you may try.
Check your file permissions. by default if your web server can not read files thats what you are going to get. I could be wrong but worth having a look
so just chmod your root folder to say 755 atleast

PHP Suddenly stop searching for files in current directory

So I came up against something really strange today.
I've updated my PHP APC to the latest version and restarted apache. And then suddenly all scripts in apache starting complaining about not being able to find required files.
I would usually have:
require_once 'Abstract.php'
considering that Abstract.php is on the same directory as the other script.
The errors where:
PHP Warning:
require_once(Abstract.php) [function.require-once]:
failed to open stream: No such file or
directory in data.php on line 411 PHP
Fatal error: require_once() [function.require]:
Failed opening required 'Abstract.php'
(include_path='/var/www/application/../library:/var/www/library:.:/usr/share/php:/usr/share/pear')
in data.php on line 411
As you can see the current directory (.) is included in the include_path of the execution. Why did this happen? Has anyone seen it before?
AFAIK, the . denotes the directory where the PHP binary resides. require* and include* functions take into account the current working directory, which can be modified via chdir().
As was suggested by cwallenpoole, try using the __DIR__ constant:
require_once __DIR__ . '/Abstract.php';
If that also fails, it means there's a misconfiguration elsewhere. If it works, it probably means the current executing PHP thread has its working directory set to a different and you need to either change the current working directory or modify the require statements to use absolute paths.
Also, have you considered using an autoload callback? That way you have a single point of entry for searching for classes.
Ok, I figured it out eventually and though I should share in case anyone else gets in the same trouble as me.
I used to have APC 3.0.x and upgraded to 3.1.8. As it seems APC on 3.1.8 has a bug that breaks include/requires of PHP.
See relative link here: http://pecl.php.net/bugs/bug.php?id=22687
Thanks everyone for the quick feedback :)

Categories