Use PHP classes on a ma - php

I'm new to PHP and I need to use some classes from an external library in my project.
My .php file, SMMain.php is in /Library/WebServer/Documents directory on my Mac. I placed the classes from the outside library right there next to it. All of these files are from the namespace Parse;. Some of the classes are in a directory called Internal.
No matter what require_once I try I get errors that the files are not found. Here's what I tried:
require_once('\Internal\Encodable.php');
produces this error:
Fatal error: require_once(): Failed opening required '\Internal\Encodable.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 13
Next try:
require_once('/Internal/Encodable.php');
produces this:
Fatal error: require_once(): Failed opening required '.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 7
I also tried:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) .'/Internal/Encodable.php');
Gives no error for the /Internal/Encodable file, but this produces another error:
Fatal error: Class 'ParseObject' not found in /Library/WebServer/Documents/SMMain.php on line 24
ParseObject.php is in the root directory together with my SMMain.php. So I tried requiring it like this:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) . '/ParseObject.php');
which gives this error:
Fatal error: Class 'ParseObject' not found in /Library/WebServer/Documents/SMMain.php on line 24
and if I try this:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) . 'ParseObject.php');
the error is:
Fatal error: require_once(): Failed opening required '/Library/WebServer/DocumentsParseObject.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 17
So what's the solution?

Use require_once($_SERVER["DOCUMENT_ROOT"] .'/Internal/Encodable.php'); if the Internal directory is under the server document root and the Encodable.php is in there.
You can use the echo getcwd(); to see, where is the current working directory.
I am always doing that, when i call my index.php file (it handles all other files), to determinate, where is my base path, and store it in a constant. So lately, i will always include files with that path as an absolute path.

Related

PHP fatal error in require_once() can n

I have a php file in other directory and i wanna use some data of it,but when i require_once() the file i see this error:
Fatal error: require_once(): Failed opening required '/Controller/UserController' (include_path='.;C:\php\pear') in E:\wamp\www\newmvc\index.php on line 20
newmvc is my root
and i'm sure there is no misspelling and i have the file.
$Controller="user";
$param=array();
$className=ucfirst($Controller)."Controller";
$controllerfilepath="/Controller/".$className;
require_once "$controllerfilepath";
I suppose you forgot to add an extension .php for class filename:
$controllerfilepath="/Controller/" . $className . ".php";

How to have relative path (../) in a pipe script?

I have [root]/includes/helpdesk/pipe.php with this code:
#!/opt/cpanel/ea-php55/root/usr/bin/php
<?php
require_once("pipeprocess.php");
In the same location in pipeprocess.php in first line I include another file still in the same location: helpdesk.php
then in [root]/includes/helpdesk/helpdesk.php in the first line I have:
require_once ("../../config.php");
config.php is in [root], this is why I have twice ../../, helpdesk.php works fine with direct access, but if run pipe.php via pipe command setup in cPanel I get a bounce error that config. php is not found:
PHP Warning: require_once(../../config.php): failed to open stream:
No such file or directory in
/home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line
21
Warning: require_once(../../config.php): failed to open stream: No
such file or directory in
/home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line
21 PHP Fatal error: require_once(): Failed opening required
'../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on
line 21
Fatal error: require_once(): Failed opening required
'../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on
line 21
Why I am getting this pipe error as direct access to helpdesk.php works fine? does pipe not accept ../ or ../../ as parth of the path? or is there an EasyApache4 issue? I did several tests and noticed this is because of ../ as part of including path. Does pipe/EasyApache4 have problem with this?
What if you used __DIR__, based on where your currently running script file's folder?
As of PHP 5.3.0, you could use something like this:
require_once (__DIR__ . "/../../config.php");

Trouble with loading phar file

I’m interested in trying cloudconvert with php, but I can’t get it to work. I put the phar file in the same folder as index.php but when I’m running this code I get the error message below.
What’s wrong?
<?php
require 'phar://cloudconvert-php.phar/vendor/autoload.php';
use \CloudConvert\Api;
$api = new Api(“my_api_key”);
Warning: require(phar://cloudconvert-php.phar/vendor/autoload.php):
failed to open stream: phar error: invalid url or non-existent phar
"phar://cloudconvert-php.phar/vendor/autoload.php" in
/Library/WebServer/Documents/index.php on line 2
Fatal error: require(): Failed opening required
'phar://cloudconvert-php.phar/vendor/autoload.php' (include_path='.:')
in /Library/WebServer/Documents/index.php on line 2
I'm not into cloudconvert but this is what I read ...
You actually not loading a phar ... You try to load the autoload.php...
Assuming cloudconvert-php.phar/vendor/autoload.php is your full path
your should do:
require('cloudconvert-php.phar/vendor/autoload.php')

PHP fatal error opening required file

I'm having problems debugging this php error. '/app/bootstrap.php' exists so not sure why I'm getting this error? Can anyone help? Thanks
[24-Mar-2017 10:42:50 Europe/London] PHP Warning: require_once(/app/bootstrap.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/example.com/index.php on line 2
[24-Mar-2017 10:42:50 Europe/London] PHP Fatal error: require_once(): Failed opening required '/app/bootstrap.php' (include_path='.:/Applications/MAMP/bin/php/php5.6.30/lib/php') in /Applications/MAMP/htdocs/example.com/index.php on line 2
The path /app/bootstrap.php looks like a root path ( and the root isn't your website root, but your Mac root directory).
Change require path to __DIR__ . '/app/bootstrap.php', it means to require a sccript that in /app/bootstrap.php relative to CURRENT directory.

require_once() cant find the include path

Hi i am trying to indlude a file by using require_once(). i am using xampp apache php server.
the file test.php is in htdocs drupal folder is also in the same directory where the include files is.
the code in line 5 is
require_once ‘drupal7/includes/bootstrap.inc’;
i am getting error
Warning: Division by zero in C:\xampp\htdocs\import.php on line 5
Warning: require_once(inc’) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\import.php on line 5
Fatal error: require_once() [function.require]: Failed opening required 'inc’' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\import.php on line 5
someone please guide me how to include a local file.
Looks like you have "fancy" single quotes in line 5. (Did you copy this from somewhere?) Change them to regular single quotes, like these:
require_once 'drupal7/includes/bootstrap.inc';

Categories