I am trying to get crontab to run a php file, here is the cronjob
10 * * * * /usr/bin/php /var/www/update/ranks.php >> /var/www/update/log/ranks.txt
But I keep getting an error saying the required file does not exist
PHP Warning: require_once(../mws_products.php): failed to open stream: No such file or directory in /var/www/update/ranks.php on line 2
PHP Fatal error: require_once(): Failed opening required '../mws_products.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/update/ranks.php on line 2
I do not get this problem when I run the file from a browser or when I go into the directory of the file and execute the file e.g. cd /var/www/update/
ranks.php
<?php
require_once('../mws_products.php');
echo "-------------------------------------------------------------\n";
echo date('d-M-Y H:i:s',time())."\n";
echo "Update Ranks\n";
$products->updateRanks();
$database->addUpdate("ranks", time());
echo "\n\n\n";
?>
folder structure
[folder] update
|____ [file] ranks.php
[file] mws_products.php
What could be causing this problem? (note: I have tried restarting apache and the server)
As your are running in a crontab from the root directory ../mws_pruducts.php does not exists, relative to the given root.
There are multiple solutions, this is one of them:
define( 'ROOT', dirname(__FILE__) );
require_once(ROOT . '/../mws_products.php');
Try with the absolute path for the file...
require_once('/var/www/mws_products.php');
Related
Environment:
Laravel Version: 5.8.29
PHP Version $ php --version: PHP 7.2.24 (cli)
Problem Statement:
I'm unable to download file from s3 to local disk using put method in created directory.
ErrorException (E_WARNING) : file_put_contents(/path/storage/4804_1626): failed to open stream: Is a directory
However, I've found that directory has been created with below permission. I tried setting permission 777 as passing third parameter in makeDirectory() method but it didn't work.
ubuntu#ip:~/path/storage$ ll
drwxr-xr-x 2 www-data www-data 4096 Sep 20 07:07 4804_1626/
Files & Configuration:
$folderPath = Storage::disk('local')->makeDirectory($folderName, 0777);
$contents = Storage::disk('s3')->get('path/' . $fileName);
Storage::disk('local')->put($folderName, $contents); // Following line is throwing error
You need to specify full path to file, not only folder:
Storage::disk('local')->put($folderName . '/' . $fileName, $contents);
I have inherited a PHP application and need to investigate various issues. I am not a PHP programmer so bear with me.
I wrote a function to log messages to a custom log file.
<?php
function my_logger($log_msg)
{
error_log("USER INFO:::::",0);
error_log(get_current_user());
error_log(exec('whoami'));
file_put_contents('/var/www/html/myproject/ulogs/my-log.log', date('G:i:s') . ">>$ " . $log_msg . "\n", FILE_APPEND);
}
After a lot of 500 errors and no information I discovered that the following line is being written to the error_log.log
PHP Warning:
file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to
open stream: Permission denied in
/var/www/html/myproject/app/lib/log_fns.php on line 11
I have put in a call to get_current_user and whoami which shows me:
[07-Apr-2021 14:26:04 UTC] USER INFO::::
[07-Apr-2021 14:26:04 UTC] root
[07-Apr-2021 14:26:04 UTC] apache
I am calling this at the moment from a simple php page called at http:///test_log.php:
<?php
require("/var/www/html/myproject/app/lib/log_fns.php");
//phpinfo();
my_logger("This is a test message from test_log.php");
?>
I tried to chmod the files to 777 in the ./lib directory but was still getting this error. I have hard coded the path you can see above creating the ulogs directory.
I have tried combinations of chmod the ulogs directory to 777 and chown the directory to apache:apache but still get this error.
Any ideas where I can look next or how to solve?
TIA
Run this command
sudo chmod 777 /var/www/html/myproject/ulogs/
With this work you file_put_contents can generate or edit the file in this directory
This happened to me and no changing of file/folder permissions worked. The issue in the end was SELinux, which is setup as enforcing by default on CentOS.
To disable it, edit /etc/selinux/config, and change
SELINUX=enforcing
to
SELINUX=disabled
Reboot and it should work.
I'm getting a rather weird error
file_get_contents('/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt'): failed to open stream: No such file or directory
So I changed the code to:
if (file_exists($cookie_file)) {
echo file_get_contents($cookie_file);
} else {
echo "$cookie_file does not exist\n";
}
And it outputs:
/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt does not exist
In the command line if I do
cat /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
It cats the file just fine
Permissions are fine too:
ls -lrt /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
-rwxrwxrwx. 1 apache apache 391 Feb 11 16:27 /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
So yeah, I'm rather confused to why it won't read the file that clearly exists.
Any ideas?
Use realpath to expand relative path to absolute path. .. between config and runtime in path means its relative, while file_get_contents require absolute.
I am working on local server, but I am having problem including files, even with full path given. eg, I have a file /home/[user]/public_html/vt/test.php like this:
<?php
include_once('/home/<user>/public_html/vt/Menu.php');
print "included_once called.\n";
?>
I got error in error_log:
failed to open stream: No such file or directory
/home/[user]/public_html/vt/Menu.php exists, and access right is:
-rwxr-xr-x. 1 <user> apache 3906 Jul 5 08:43 <full/path/of/the/file>
The local documentRoot folder is set to (recursively):
drwxr-xr-x. 4 <user> apache 4096 Jul 26 14:06 public_html
So what is wrong?
try something like that: (DIR is a magic constant which contains the directory of the current file)
include_once dirname(__FILE__).'/Menu.php';
// PHP >= 5.3
include_once __DIR__.'/Menu.php';
I am trying to include a php file from the parent directory and I getting error:
admin#webby:~$ /usr/bin/php
/var/phpscripts/email_parser/tests/_email-test.php PHP Fatal error:
require_once(): Failed opening required '../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
Fatal error: require_once(): Failed opening required
'../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
PHP file
#!/usr/bin/php
<?php
error_reporting(E_ALL ^ E_NOTICE ^E_WARNING);
ini_set("display_errors" , 1);
require_once("../PlancakeEmailParser.php");
// etc
?>
Folder Structure
admin#webby:/var/phpscripts/email_parser$ find .
.
./composer.json
./README.txt
./LICENSE.txt
./PlancakeEmailParser.php
./tests
./tests/_email-test.php
For testing it works fine when I move PlancakeEmailParser.php into the tests directory and remove the "../" from the require
The line
require_once("../PlancakeEmailParser.php");
Is not using a fully qualified file-system path or PHP-URI-scheme. Therefore its outcome depends on PHP configuration, most often because of the include directory configuration.
The PHP CLI can use a different configuration file than with your webserver - or - the working directory is different as with your webserver (probably the later plays more of a role in your specific scenario).
What you want can be easily expressed fully qualified, too:
require_once(__DIR__ . "/../PlancakeEmailParser.php");
This is probably what you're looking for.
Quote from PHP CLI SAPI: Differences to other SAPIs:
It does not change the working directory to that of the script. (-C
and --no-chdir switches kept for compatibility)
In order to keep relative paths in your script working as-is, change directory to where the script resides, then execute the script:
cd /var/phpscripts/email_parser/tests/ && ./_email-test.php