Problems with accessing files - php

Hello stackoverflow community, how can i solve this problem, here it is my error:
Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
the that query.class.php is in the folder:
$_SERVER['DOCUMENT_ROOT']/classes/query.class.php
Im trying to access it from:
$_SERVER['DOCUMENT_ROOT']/modules/tasks/ajax/file.php here.
When i try to use:
require_once($_SERVER['DOCUMENT_ROOT'].'/classes/query.class.php'):
It works like a charm. But i need to acces that file using DP_BASE_DIR. So when i write:
require_once DP_BASE_DIR."/classes/query.class.php";
I get an error. How can i solve this problem?

Try to see if this makes sense to you, I have tested this out using:
test.php
<?php
define('DP_BASE_DIR', '/var/www/devspace');
if (!(defined('DP_BASE_DIR'))) { die('This file should not be called directly.'); }
require DP_BASE_DIR."/test2.php";
var_dump($string);exit;
// outputs string(4) "test"
?>
test2.php
<?php
$string = 'test';
?>
Isn't that exactly what you're doing? I feel that something else is coming into play here and being left out of the info that has been provided.
The only real issue that I could spot here is that maybe $_SERVER['DOCUMENT_ROOT'] did not hold the exact same value as your DP_BASE_DIR but as you have confirmed these to be the same I am scratching my head about other possibilities as this should just work.
EDIT:
do this right before the require:
$test = DP_BASE_DIR."/classes/query.class.php";
var_dump($test);exit;
Post the result?

Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
Seems DP_BASE_DIR constant does not exists. If constant does not exists will use the name of constant, and you get a notice.
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.

Related

Link a js on a PHP

Hi I'm a Amateur programer and I need help for my web's programer subject. I'm trying to link this js but on the page show me this on the top Warning: file_get_contents(script404.js): Failed to open stream: No such file or directory in C:\xampp\apps\wordpress\htdocs\wp-content\themes\villanueva-s_theme-brown_nights-\404.php on line 58
<?php
$script = file_get_contents('script404.js');
echo "<script>".$script."</script>";
?>
You must have an invalid path.
You could use __DIR__.'/script404.js' if this file is in the same folder as script you running.
See Evert's answer in Why include __DIR__ in the require_once?
You can just use:
echo "<script src=\"script404.js\"></script>";
Or, you should point to the right directory, like:
$script = file_get_contents('C:/path/to/script404.js');
echo "<script>".$script."</script>";
Or, I wouldn't advice you to do this, but if it is in the same directory:
$script = file_get_contents('./script404.js');
echo "<script>".$script."</script>";

Can't reference POST variable at parts of the code in PHP

I have a bizarre problem that I believe should have a very simple solution but, for the life of me, I can't seem to understand what's going on.
I'm grabbing a POST variable and I first check if it is posted and echo it. It is.
However, when I try to form a variable that is a full path to a filename, it doesn't recognize it and I obviously can't open the file...
I do get a warning for an Undefined variable so that's where the issue is but I can't understand why I can check the value of that variable anywhere and it's properly set.
Here's the code:
if (isset($_POST['fid'])) {
echo 'fid = ' . $_POST['fid'];
}
I form a whole path (which I echo and it is fine) to the file I'd like to open:
$fnameonly = getfilename($_POST['fid']);
echo $fnameonly; //echos fine, getfilename() worked fine
$fname = $_SERVER['DOCUMENT_ROOT'].'/uploads/' . $_SESSION["username"] . "/" . $fnameonly;
echo $fname; //also echos fine, $fname should be properly set
Now in looking at the resulting HTML, I get 2 warnings:
Notice: Undefined variable: fid in C:\web\www\csv\translatebs.php on line <i>23</i>
This is refers to the following line from the code above:
$fnameonly = getfilename($_POST['fid']);
The second warning refers to simplexml not being able to open the file since the path is incomplete.
Warning: simplexml_load_file(C:/web/www/uploads/paulohgo/): failed to open stream: No such file or directory in C:\web\www\csv\translatebs.php on line <i>31</i>
I can echo $fnameonly just fine but the warning is there.
When I try to open the file the value from $filenameonly is empty, so the I only get the path to a folder but miss the actual file name.
I don't get what I'm missing here... thanks for any help.

file_get_contents on local wamp server not working

I simply cant wrap my head around why this simple code won't work on my local wamp server where i have other sites running.
<?php
$file = file_get_contents('C:\Users\Computer\input.txt', true);
echo $file;
?>
The file is where it should be.
I get the following error:
Warning: file_get_contents(C:\Users\Computer\input.txt): failed to open stream: No such file or directory in C:\Users\Computer\Dropbox\htdocs\ny\www\php\day2\code.php on line 3
But the file is exactly where it should be.
The file has all possible permissions to true (read, write ect.) in windows except for "special permissions".
What is not working correctly?
Regards,
Patrick
The solution was the extension. It was not correctly parsed. This was checked by using #VolkerK suggesting to check
Does the output of var_export(glob('C:\Users\Computer*')); contain
input.txt? Or is it false/null?
Thanks to #VolkerK for the help solving the problem.
Since I incorrectly said that it might be because of backslash escaping, I guess I need to redeem by saying something useful :)
So try:
$file = 'C:\Users\Computer\input.txt';
if(is_file($file))
{
$string = file_get_contents($file);
}
else
{
die("Uh oh! Seems like $file has got problems!");
}
Also, you are passing the second argument as true for searching the include path. Since your filepath is absolute, I suspect if you need this argument to be passed at all.

file_get_contents() not able to fetch data?

I searched several posts similar to this but could not find an answer. I am probably missing out something small.
I am trying to read JSON contents from a URL as shown by my code:
$uri = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
echo "URI: [$uri] <br/>";
$file = file_get_contents($uri);
$error = error_get_last();
echo $error['message'];
If I open the URL in a browser, I can see the JSON contents of it. But the above mentioned code does not work. $file has a value of false. The error message is:
file_get_contents(http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710) [function.file-get-contents]: failed to open stream: No such file or directory.
Any suggestions? NOTE: my allow_url_fopen is set to 1 if it matters.
Edit:
Here is the entire PHP file.
http://pastebin.com/zCKEP9kG
Also, there is an fopen() in the code which opens an http file just fine.
I don't see anything wrong with the code, mate - it runs fine locally on my setup and I can get the file, and can do whatever I like with it.
Try it running on a different host - may be your php.ini is messed up and is messing up the script as well.. Or may be you've got problem with some other spot of the source.. may be if you shared the entire file source, or at least a bigger chunk of it..
Cheers!
Check this code, i think you can proceed further with this now --
<?php
$json_url = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
#ascii-lime
its for #ascii-lime--
Check this (Edit#2)
Try This :
<?php
header('Content-Type: application/json');
$json = file_get_contents('http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710');
echo $json;
?>

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

Categories