I am trying to read the contents of a file in a subdirectory
here is my attempt :
$entityBody = "123456"
$vval= file_get_contents(__DIR__."/../data/names/$entityBody");
echo("$vval");
here is the directory outlook:
Issue: issue is when i run the script echo("$vval"); it doesnt echo anything on the screen.
what am I doing wrong ?
If you set
ini_set('display_errors', '1');
error_reporting(E_ALL);
In your PHP script you will see something like
PHP Warning: file_get_contents(/var/www/html/myData/../data/names/123456):
Failed to open stream: No such file or directory
Now since both your PHP script and data dir are sharing the same __DIR__ you don't need ..
So
$vval = file_get_contents(__DIR__ . "/data/names/$entityBody");
Will solve the problem for you.
Related
I have this strange error, when I try to delete a file inside a compressed directory :
ZipArchive::close(): Renaming temporary file failed: Permission denied in /MyDirectory/myphpscript.php
Here is my code :
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$compressedDirectoryPath = '/Users/Shared/SampleZip.zip';
$zip = new ZipArchive();
if ($zip->open($compressedDirectoryPath) === true) {
if ($zip->deleteName('SampleZip/samplefile.txt') === true) {
echo 'File deleted';
}
}
$zip->close(); // the error is pointing here
?>
The echo executes successfully and prints File deleted. I am running a Mac and the permissions on the compressed directory is read & write for all users. What could be the issue?
As the error tells you, this is a permission problem. Make sure the apache user (www-data) has the write permission on the directory where the zip archive is.
After that, your code will work as expected.
Good luck !
This can also happen when you open the output file on server itself and keep it open while trying to run the script again.
I just switched a database over to a Plesk server on GoDaddy. Now, my connection script include fails with this error:
PHP Warning: include(/rev/scripts/connection.php): failed to
open stream: No such file or directory in
G:\PleskVhosts\mysite.com\httpdocs\rev\db_administration\complete_backup.php
on line 5 PHP Warning: include(): Failed opening
'/rev/scripts/connection.php' for inclusion
(include_path='.;.\includes;.\pear')
I'm not sure why it is prepending 'G:\PleskVhosts\' to the url or even if this is what is crashing it. Can anyone tell me what I am doing wrong? This code has worked for a long time, so I don't know why it needs to change with the new server.
What is strange to me is that if I run this from a browser (including the edit below), it works. But I need this to run from scheduled tasks, and there is where I get the error.
Here is the code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$connection = $path . '/rev/scripts/connection.php';
include ($connection);
echo "success";
?>
EDIT: Per suggestion below, I changed my code to this, which allows it run from the browser. But I still can't get it to run from scheduled tasks:
$path = !empty($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'];
Looking at the include path in your error message ... include(/rev/scripts/connection.php) ..., it looks like $path is an empty string. A bit of googling shows that GoDaddy doesn't always translate $_SERVER['DOCUMENT_ROOT']; correctly for some of its add-on domains and subdomains. Try replace $_SERVER['DOCUMENT_ROOT'] with $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];.
So essentially adding,
$path = !empty($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'];
or
$path = dirname(__FILE__); // this might or might not work.
UPDATE
$_SERVER won't work when attempting to run this from a scheduled task (presumably, via cron). Try this:
define('DOCUMENT_ROOT', substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1));
It's supposed to get you the same data as $_SERVER['DOCUMENT_ROOT'] for cron jobs.
All I want to do is fopen() a text file in my PHP script. It's the most important part of the module that I'm building. Unfortunately, while testing the script, I found that the script cannot see the text file that I'd like to open. I have the script in the same directory as the text file, but I keep getting met with the same error. I'm posting the script code snippet and the command line prompt/response below.
Code
$membersfile = fopen('./members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
Command Line
507 elersong:~$ php /Users/elersong/Desktop/SS2014/register_member.php
Warning: fopen(./members.txt): failed to open stream: No such file
or directory in
/Users/elersong/Desktop/SS2014/register_member.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in
/Users/elersong/Desktop/SS2014/register_member.php on line 13
I have no clue why PHP can't see the file since it most certainly exists, and it's in the same directory as the script. Please help me make sense of this.
Some PHP setups have problems with relative parts, so try this instead:
fopen(__DIR__ . '/members.txt', 'r');
__DIR__ contains the path to the folder where the currently executing PHP file is.
Your are currently doing:
$membersfile = fopen(__DIR__ .'/members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
But $membersfile contains just the handle, a number so that PHP nows which opened file you are meaning. If you print that you will just see the handle number. You need to tell pHP to read the file using:
$file = __DIR__ .'/members.txt';
$membersfile = fopen($file, 'r');
echo fread($membersfile, filesize($file))
fclose($membersfile);
or use the php shorthand function:
echo file_get_contents( __DIR__ .'/members.txt');
After trying a number of the suggestions posted here, I was finally able to figure out that the PHP script isn't using a relative path to the text file based on the directory that holds the PHP script.
User #andrew suggested that I add the following line to my script in order to learn which directory the script uses as its starting point.
echo '<pre>';var_dump(glob('*'));
From the output of this line, I was able to see that the script uses the user's root folder as the starting point in fopen() on my OSX machine. I simply needed to preface the filename with the remainder of the file path, and PHP could read it. The amended script is as follows:
$membersfile = fopen('Desktop/SS2014/members.txt', 'r');
Thanks so much, #andrew and everyone else who submitted suggestions!
The PHP file_put_contents function works perfectly fine when the file exists. If the file does not exist I receive the error "failed to open stream: No such file or directory".
$file = '../templates/stuff.xml';
if (!file_exists($file)) {$file = '../'.$file;}
$var['xhtml'] = $_POST['post_xhtml'];
$file_contents = serialize($var);
file_put_contents($file,$file_contents);
I tried the same thing with fopen and fwrite using the correct flags (w, w+ and tried the others) yet still had the same problem: if the file already existed it worked just fine, otherwise it would give me the same error message.
I know the file path is correct. I'm using Windows 7 for local development.
When the file doesn't exist, you are prepending ../ to the path, thus you are trying to write to:
../../templates/stuff.xml
Are you sure that the folder ../../templates exists (and that PHP can write to it)?
Before you write to a file, you need to check that the folder exists. Try using is_dir():
if(is_dir(dirname($file))){
file_put_contents($file, $file_contents);
}
I've been trying to debug this error for over three hours now, changing filenames, trying to use GeoIP Lite instead of GeoCity (the latter has a 27mb file to be included, so did this thinking fopen() had a max), etc.
Here's my structure file structure: index.php -> include("configuration/config.php") - config.php -> include("inc/geo_text.php") -> geo_text.php
The contents of geo_text.php is:
$ip = $_SERVER['REMOTE_ADDR'];
include("GeoIP/geoip.inc");
$gi = geoip_open("GeoIP/GeoIP.dat",GEOIP_STANDARD);
$count_name = geoip_country_name_by_addr($gi, $ip);
geoip_close($gi);
echo($count_name);
Now, if I access geo_text.php no errors are given, and just to make sure I placed echo($count_name) in geo_text.php and it returned, as it should, my country.
However, when I run config.php it returns the error:
Warning: fopen(GeoIP/GeoIP.dat) [function.fopen]: failed to open stream: No such file or directory in /nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/geoip.inc on line 399
Can not open GeoIP/GeoIP.dat
Has anyone got any ideas why this could be?
SSH into your server and run the following command (assuming it's a Linux server):
cd /nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/
ls -lah
Then paste the output here for us to see. My guess is that that path doesn't exist.
That's very strange. As a test, try moving both geo files into the same directory as your code files and then alter the paths in your code accordingly.
It's checking for the files in domain.com, but you just cd'd into themeplated.com, that's the problem. Your code needs to point to the themeplated.com directory.
/nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/
/nfs/c09/h02/mnt/127878/domains/themeplated.com/html/labs/final/configuration/inc/GeoIP/
It's a path issue.
geoip_open("/absolute/path/to/GeoIP/GeoIP.dat",GEOIP_STANDARD);
should work.