Writing a file in PHP with a cron - php

I am trying to write a file to a sub folder of the directory my cron script is in using file_ put_ contents. However, I keep getting a warning "failed to open stream: No such file or directory."
I have this directory structure:
httpdocs/scripts/fileDirectory
The cron script lives in the scripts folder. I call it with the cron command:
php httpdocs/scripts/cron_writeFile.php
In the cron_writeFile file, I first tried:
file_put_contents('fileDirectory/', $fileName, $fileContents);
which works when I load the page in a browser, but not when the cron executes.
When I require_once a file in a cron, I have to put the 'absolute' path to it:
require_once('httpdocs/scripts/requiredFile.php');
So, I tried that:
file_put_contents('httpdocs/scripts/fileDirectory/', $fileName, $fileContents);
No luck. I'm pretty sure it's getting to the right folder because the warning is:
"Warning: file_ put_ contents(httpdocs/scripts/fileDirectory/4.txt):
failed to open stream: No such file or directory in
/var/www/vhosts/myDomain.com/httpdocs/scripts/cron_writeFile.php
on line 93"
The both directories have write permissions.
I am using a VPS running (I know it sucks and I need to upgrade, but I don't have the authority)
Parallels Plesk Panel version 9.2.1
with PHP 5.0.4
The file does not exist and I need a new file each time the script runs.
I am not sure if there is a certain way to define the file path or some other thing I am missing.
Thanks for your help!

What if you use dirname(__FILE__) (see) to get an absolute path to your "fileDirectory" directory ?
Something like this in your script, I guess, if your fileDirectory is a child of the directory where your file resides :
file_put_contents(dirname(__FILE__) . '/fileDirectory/' . $fileName, $fileContents);
That way, you don't depend on relative path, which can be wrong if your script is not launched from the "right" directory.

Related

Cannot reference another php file in another folder [duplicate]

I have the following directory structure.
/var/www/base/controller/detail.php
/var/www/base/validate/edit.json
/var/www/html
Within /var/www/base/controller/detail.php, how do I use file_get_contents() with a relative path to read /var/www/base/validate/edit.json? I've tried the following:
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('detail.php');
//No error, but I don't want this file and was just testing
$json=file_get_contents('detail.php', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('./validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('././validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../../validate/edit.json', FILE_USE_INCLUDE_PATH);
//This works, but I want to use a relative path
$json=file_get_contents(dirname(dirname(__FILE__)).'/validate/edit.json');
Have you tried:
$json = file_get_contents(__DIR__ . '/../validate/edit.json');
__DIR__ is a useful magic constant.
For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.
When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.
PHP will often report that it is unable to find the third file, but why?
Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.
In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.
So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.
try using this
$json = file_get_contents("/path/to/your/file/edit.json", true);
As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. This is not possible if strict typing is enabled since FILE_USE_INCLUDE_PATH is an int. Use TRUE instead.
Of cause there is the possibility to use the include_path parameter. Set this parameter to '1' then a search for the file in the include_path (in php.ini.) will be done. You have to do editing in the php.ini!
When using $json=file_get_contents('detail.php'); a call is done from a php file.
Use file_get_contents('detail.php'); to have the detail.php get executed. the file have to be in the root directory (same as the calling php file in which the file_get_contents() is situated). If detail.php is in a subdirectory i can not see any workaround than using the include_path parameter
You can always try to do the opposite first to find out:
file_put_contents('FINDME', 'smth');
exit();
$json=file_get_contents('detail.php');
...
Then run something like this from your terminal on *nix system (or search for a file named FINDME using GUI on Windows)
find <root-dir-of-the-project> -name 'FINDME'
The command will output something like this:
<root-dir-of-the-project>/<directories>/FINDME
Now you know the root dir (from which the relative path is being taken) for the file where you are attempting to read the other files and you can construct the relative path easily

"File not found" When running on heroku, but it works on local

I want to include a file that is in a parent folder in my php code using:
include('../defines.php');
However when I deploy to heroku from CLI and run my php file it gives me this error.
PHP Warning: include(): Failed opening '../defines.php' for inclusion (include_path='.:/app/.heroku/php/lib/php') in /app/workers/test.php on line 2
If I run locally from my installed Wamp 3.1.4 server it run fines without issues.
I can make it work if I remove the `../? from the path. But that seems wrong.
Another way I've found on github is doing
include __DIR__ . '/../defines.php';
But i think doing it that way would be using absolute, not relative path.
Is there something i'm missing?
<?php
include('../defines.php');
?>
Folder tree:
www/
defines.php
workers/
test.php
It's depending how you classified your folder.
Maybe you can try this :
require("/../yourFolder.php");

Even if the csv file is in the directory, it returns PHP: failed to open stream: No such file or directory.

May I ask a dumb question? I have already checked the answers for the same error such as. PHP - Failed to open stream : No such file or directory. and couple of others.
However, this one seems more elmentary problem. I am using XAMPP and I have checked the the property of csv file and it says the location. C:\xampp\htdocs
Then, I checked http://localhost/Untitled-2.php and it reutrns:
Warning: file(Book 1.csv 1): failed to open stream: No such file or directory in C:\xampp\htdocs\Untitled-2.php on line 2
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Untitled-2.php on line 3
Why am I getting these error? My code is the following script:
<?php
$read = file("Book 1.csv 1");
foreach($read as $line){
echo $line .",";
}
?>
So, I think you have a problem with relative and absolute paths.
file("foo.bar") will try to open the file foo.bar relative to your current path. If you ran your php script from the command line, it will try to open the file in the directory you were in when you ran php.
So if you have the following directory structure:
/foo/
/foo/test.php
/foo/bar.csv
/bar.txt
And you are running php from your root directory:
/ $ php /foo/test.php
The working directory of your file will be /, even though the php file itself was in /foo. So if you do
file('bar.csv');
What php will try to open is in fact /bar.csv which doesn't exist.
Please note that what will be the relative working directory of your script can greatly differ in how your script were ran. If it is running from php-fpm mode, it will depend on your config.
To test what your current working directory is, do
echo getcwd();
And then you can list the contents of your working directory with
$files = glob(getcwd() . DIRECTORY_SEPARATOR, GLOB_NOSORT);
var_dump($files);
Or you could do something similar with readdir
All in all, I think you should try to avoid using relative file paths, as it can open up all sorts of problems. If possible, try to use absolute paths instead, like:
file('/var/www/file.csv');
I'm assuming your filename is Book 1.csv because it makes more sense. Your code might have a typo, as the file source is currently Book 1.csv 1 and this could be the cause of your problem.
Try this:
$read = file("Book 1.csv");

file_get_contents with relative path

I have the following directory structure.
/var/www/base/controller/detail.php
/var/www/base/validate/edit.json
/var/www/html
Within /var/www/base/controller/detail.php, how do I use file_get_contents() with a relative path to read /var/www/base/validate/edit.json? I've tried the following:
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('detail.php');
//No error, but I don't want this file and was just testing
$json=file_get_contents('detail.php', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('./validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('././validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../../validate/edit.json', FILE_USE_INCLUDE_PATH);
//This works, but I want to use a relative path
$json=file_get_contents(dirname(dirname(__FILE__)).'/validate/edit.json');
Have you tried:
$json = file_get_contents(__DIR__ . '/../validate/edit.json');
__DIR__ is a useful magic constant.
For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.
When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.
PHP will often report that it is unable to find the third file, but why?
Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.
In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.
So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.
try using this
$json = file_get_contents("/path/to/your/file/edit.json", true);
As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. This is not possible if strict typing is enabled since FILE_USE_INCLUDE_PATH is an int. Use TRUE instead.
Of cause there is the possibility to use the include_path parameter. Set this parameter to '1' then a search for the file in the include_path (in php.ini.) will be done. You have to do editing in the php.ini!
When using $json=file_get_contents('detail.php'); a call is done from a php file.
Use file_get_contents('detail.php'); to have the detail.php get executed. the file have to be in the root directory (same as the calling php file in which the file_get_contents() is situated). If detail.php is in a subdirectory i can not see any workaround than using the include_path parameter
You can always try to do the opposite first to find out:
file_put_contents('FINDME', 'smth');
exit();
$json=file_get_contents('detail.php');
...
Then run something like this from your terminal on *nix system (or search for a file named FINDME using GUI on Windows)
find <root-dir-of-the-project> -name 'FINDME'
The command will output something like this:
<root-dir-of-the-project>/<directories>/FINDME
Now you know the root dir (from which the relative path is being taken) for the file where you are attempting to read the other files and you can construct the relative path easily

PHP Cron Job: Including file not working?

i run a cron job every night, but for some reason, it is saying that the file i try to include is inexistant:
Warning: require(../includes/common.php): failed to open stream: No such file or directory in /home/fini7463/public_html/cron/journeyNotifications.php on line 2
Fatal error: require(): Failed opening required '../includes/common.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/fini7463/public_html/cron/journeyNotifications.php on line 2
here's the code:
set_include_path('/home/fini7463/public_html/includes/');
require 'common.php';
the file 'common.php' is located as follows
public_html => cron => journeyNotifications.php
=> includes => common.php
i even set the include path (as shown in the code), but i am still getting this error. what could the problem be?
thanks!
If you do require('../includes/common.php'), the path is traversed relative to the current working directory.
If you do require('common.php'), the file is searched in the include path, and in the directory of the script which calls the require().
To solve this, first change directory in your crontab:
cd /home/fini7463/public_html; php -f cronjob.php
Calling set_include_path() as you do trashes the previous path. The call replaces the previous path with whatever you're passing as an argument, so if any of your code loads other libraries (ie: PEAR/PECL modules), they'll no longer be accessible as you've trashed the include path. You should use:
set_include_path(get_include_path() . PATH_SEPARATOR . '/home/fini7463/public_html/includes/');
That will append your new path to the include path.
As well, you can never quite tell what the working directory will be when cron fires up your script. It could be the home directory of the user you're running the script as, could be /tmp, or some other directory entirely. If you want to use relative paths in the script for anything, you'll have to make sure the working directory is set to a known value. Either by using a 'cd' in the crontab, or using a 'chdir' inside the script before doing anything involving the relative paths.

Categories