0

I have a php script working when ran through a browser https://mywebsite/cron.php but not through ssh. I'm running php -f /path/to/the/script/cron.php and I receive errors like Fatal error: Uncaught Error: Class 'MYCLASS' not found.

The cron.php contains code like that:

<?php
require_once("fonctions.php");
$var=new MYCLASS();

The fonctions.php :

<?
require_once("MYCLASS.php");

I tried to use cd /path/to/the/wwwroot before php -f ... but that doesn't work. I also tried chdir(__DIR__) in the cron.php.

1
  • This looks quite 'foo-bar'ish to me. Class 'MYCLASS" not found suggests that the require worked, but the php file contains no 'MYCLASS' class. Did this really work via browser ? Commented Oct 5, 2019 at 10:32

1 Answer 1

0

Add __DIR__.'/'

cron.php

<?php
require_once(__DIR__."/fonctions.php");
$var=new MYCLASS();

fonctions.php

<?php
require_once(__DIR__."/MYCLASS.php");
Sign up to request clarification or add additional context in comments.

3 Comments

A good answer includes an explanation about what the actual issue was and how/why does this would solve it.
Are you calling the script via ssh or cronjob ? If you are calling it via cron, make sure you specify full path of php. You can find where php is by calling whereis php in shell. Then set cron job like /usr/bin/php /path/to/the/script/cron.php Also this might be a related issue - stackoverflow.com/questions/12377719/…
I first tried from cron but as it didn't work I tried from ssh command line, and had that issue...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.