require_once file not found in php eclipse project - php

I have a php script that has the following requirement command: require_once 'HTTP/OAuth.php'; the file HTTP/OAuth.php is in php's include_path that is .:/usr/lib/php.
Nevertheless in Eclipse the require_once line is marked with the following warning: Include filename: 'HTTP/OAuth.php' doesn't exist in project:
How can I make my project see the include_path so it can find the require_once file?

I honestly think you can't access any folder outside the root of your web application, basically you can't go any further than /. Imagine you use a shared web hosting service and someone access your files at will, it's just not safe at all.
You can, however, copy the file and put in inside your application scope. E.g: require('/includes/OAuth.php');.

Related

Server PHP includes search Shared folder before local folder

I am a reseller on cPanel with Apache and PHP 5.5, and my server owner has just installed the latest PHPMailer on the shared folder, so it can be accessed from:
require "PHPMailerAutoload.php";
but, all of my current sites have PHPmailer in my class folder, with an extended class to customise the mailer details to each site.
So, my PHP class has:
require_once "class.phpmailer.php";
class ChoiceMailerSMTP extends PHPMailer {
/**
* Mailer
*
**/
My issue is that since my server guy has put on the shared folder, the new PHPMailer, that now all of my require calls are searching the Shared folder BEFORE searching the local folder, and therefore loading the wrong phpmailer.class.php.
This is really frustrating, although in this instance it's (only) a hassle - if he puts other files in the shared folder for himself or for others, how am I meant to ensure that my PHP includes and requires run the local rather than shared files?
Is there a line in php.ini I can use? Or perhaps a cPanel setting?
require "./file" doesn't work.
EDIT:
I've been doing some research on my system and found that set include path is already set to:
include_path = ".:/usr/lib/php:/usr/local/lib/php"
So any help why the local directory is only searched after the include path directories?
The include order of relative paths depends on the set include path, see set_include_path. You can either modify that include path to prioritise your local folder, or you can use an explicit absolute path for includes to begin with:
require_once __DIR__ . '/class.phpmailer.php';
What's wrong with the shared version? Is it older than yours?
You can adjust the order that directories are searched by changing the include_path directive in php.ini.
You should be loading the autoloader anyway, as it always looks for class files relative to itself, so, similar to #deceze's suggestion, you should do:
require_once __DIR__ . '/PHPMailerAutoload.php';

PHP File Include

I have searched the forums already, but none of the solutions have seemed to help me.
Basically I have just installed and created a package using composer. I need to autoload the classes, sounds pretty standard.
I followed all the instructions, and have added this line of code to my script:
require_once 'vendor/autoload.php';
The vendor folder is located in the root folder of my server, here:
/root/vendor/autoload.php
So, I added
:/root
To my PHP ini file so that PHP searches in the root folder when looking for includes. I thought that should work but it's not :(
My PHP ini file now looks like this:
.:/usr/lib/php:/usr/local/lib/php:/root
The error message I am getting is this:
[14-Jul-2014 16:46:29 Europe/London] PHP Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/root') in /home/owned/public_html/trythis/ow_plugins/oftokbox/bol/service.php on line 38
Any ideas?
You implicitly state you are using Composer for a project. By doing so you must have a composer.json file somewhere. And Composer will create a vendor folder directly in the folder containing this file.
So if you also have a file index.php in the folder containing the composer.json, to include the autoloader you would use require 'vendor/autoload.php';.
If however you follow some security guidelines and have a dedicated folder containing public files, then the file would for example be called public/index.php, and for this file to reach the autoloader, the relative path would be require '../vendor/autoload.php';.
Composer cannot give a one-instructions-fits-all direction because it depends on which folder structure you have. But including the composer autoloader is just the same task as including any other file with a relative path.

Using PHP's $_SERVER["DOCUMENT_ROOT"] under an open_basedir restriction

I've spent the past few months building a website on localhost. Throughout the site, instead of using relative paths, I used $_SERVER["DOCUMENT_ROOT"] to access the root of my server so I could reference files from there. However, I just decided to upload my site to 000webhost. As I soon found out, and for those of you who use them, you are probably aware, that their server root is different than your actual directory in which you upload your files. I believe it's virtual hosting... anyway, my $_SERVER["DOCUMENT_ROOT"] now throws errors along the lines of this on the site:
Warning: include_once() [function.include-once]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/mypath) is not within the allowed path(s)
Every other site I looked at said that you should just replace $_SERVER["DOCUMENT_ROOT"] with the home directory provided to you by 000webhost. However, if I want to change hosting services in the future, I'm screwed. Is there any way to use $_SERVER to access a set virtual directory or use htaccess or something to make my code work? I tried using DocumentRoot in a htaccess file in my root directory, but it threw a 404 error when trying to access the page.
I would never recommend using DOCUMENT_ROOT. For example, it is useless if you ever want to run scripts via the command line or cron.
I would instead, set the include_path via one of your scripts. Something that is included in every page (like some sort of config or bootstrap script).
For example, say you have config.php in your app's root directory (could be doc root but it's not important) that is included on every page
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__, // this is the directory of this file, config.php
get_include_path()
)));
Then, you can safely use relative paths from this location in your include / require statements
include 'foo.php'; // APP_ROOT/foo.php
include 'somedir/bar.php'; // APP_ROOT/somedir/bar.php
When setting the include path, it doesn't really matter where the script resides, just construct the appropriate path. Say config.php lived in APP_ROOT/configs/config.php, the include path would be
realpath(__DIR__ . '/..') // realpath just resolves the path traversal
Decided to go with the following method:
http://www.000webhost.com/forum/customer-assistance/4857-document_root-environment-variable.html
Works!

PHP -- Directory installation

I want to play with excel sheet in PHP. So i found out PHPExcel provides that option.
But i am having a problem in setting up of the PHPExcel in my directory.
It says:
Extract and copy Classes to your includes/libraries directory. On your script, the minimum code you need to do is:
require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php';
I am not running PHP locally on my computer, So i do not have access to PHP remote directories.
What should i do?
This assumes you have a folder to upload to on a webserver that is running PHP:
I think you're a little confused by the way you have worded your question.
Your includes / libraries directory is one of your choosing, you can place the scripts anywhere and then use require() / require_once() to include the classes in the script you would like to use their objects in.
To break down the example:
require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php';
dirname(__FILE__) - FILE is a constant for use with any script. i.e. it is declared by the base PHP files not any scripts you create. It returns the absolute path to the current PHP script running (in relation to the document root). using dirname() evaluates FILE and returns the path without the script file location.
i.e.
dirname(__FILE__) = c:/docs/www/root/index.php
would be evaluated as:
c:/docs/www/root
So to wrap this up, place the documents in a directory above your web root folder. And require them from there.
Hope that helps.
Supposing you are in the root directory / of your host.
You have to upload PHPExcel's classes in a directory, usualy /PHPExcel/
Then, if your script is at the root of your host, you just have to add require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php'; just after your php opening tag.

Shared hosting - How to php include file below web root?

I am having trouble trying to get a file to be included into a php script. This file is stored in the root folder, which is not the actual web root, but the root directory that I have access to via the shared hosting account.
I have created a folder in this directory that stores php session data, the .htaccess configuration regarding this would be the following line:
php_value session.save_path "/usr/home/accountname/sessions"
The web root would be stored in this directory:
/usr/home/accountname/public_html/domainname/
What I am trying to do is secure the scipt that holds database connection details, in a folder that would be something like this:
/usr/home/accountname/includes/
This is how i currently include the file:
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
I have tried this:
require_once $_SERVER['PATH'].'/usr/home/accountname/includes/config.php';
As $_SERVER['PATH'] points to /bin
This is not working as the page comes out blank, this would mean that the variables defined in config.php are not active as the page has not been included.
Could anyone provide some information regarding this matter, or point me in the direction of solving the problem. Also, any tutorials or other info regarding this would be greatly appreciated.
Thank you!
If the script including config.php is stored under your Web root, you can write:
require_once dirname(__FILE__) . '/../../includes/config.php';
Have you already tried?
If your script is in /usr/home/accountname/public_html/domainname/something.php, you could use this:
require_once(dirname(dirname(dirname(__FILE__))).PATH_SEPARATOR."includes".PATH_SEPARATOR."config.php");

Categories