Got error while trying to run localhost website on cPanel - php

I have been trying to switch my web app from localhost to a school server but it's telling me it cannot find the path:
Warning: require_once(/util/tags.php): failed to open stream: No such
file or directory in
/home/xiaoant/public_html/database_pizza/pizza/util/main.php on line
17
Fatal error: require_once(): Failed opening required '/util/tags.php'
(include_path='///') in
/home/xiaoant/public_html/database_pizza/pizza/util/main.php on line
17
<?php
// Start session to store user and cart data
session_start();
// Get the document root
$doc_root = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING);
// Get the application path
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING);
$dirs = explode('/', $uri);
$app_path = '/' . $dirs[1] . '/' . $dirs[2] . '/';
// Set the include path
set_include_path($doc_root . $app_path);
// Get common code
require_once('/util/tags.php');
require_once('/model/database.php');
// Define some common functions
function display_db_error($error_message) {
global $app_path;
include 'errors/db_error.php';
exit;
}
function display_error($error_message) {
global $app_path;
include 'errors/error.php';
exit;
}
?>

If you're using window and hosted it on a linux server Please check you path if it contain upper case letters directory names in linux is case sensitive unlike windows
Also you could list all the files in directory to check
'''$files1 = scandir($dir);
print_r($files1);
'''
And see if it there

Related

Normalize/Standard path root to CLI/WEB

I have this short script to detect the user usage environment and try to normalize the root path:
Located in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php
$env= '';
if (php_sapi_name() == 'cli') {
$env= 'cli';
if (!isset($_SERVER['PWD'])) {
$path = dirname(__DIR__).'\\';
} else {
$path = dirname($_SERVER['PWD']);
}
} else {
$env= 'web';
$path = $_SERVER['DOCUMENT_ROOT'];
}
echo $path;
echo PHP_EOL;
echo $env;
file_get_contents($path.'configs/const/client.xml')
i use it from 2 diferent files: index.php that wor well in root folder but trying to use it from a sub-directory like this /dev/cron.php
cron.php content:
$path = (!isset($_SERVER["PWD"]) ? dirname(__DIR__).'\\' : dirname($_SERVER["PWD"]));
require_once $path.'/configs/const/loader.php';
i get this output
//from Web environment
C:/xampp/htdocs/dev/t2/Last-Hammer/
web
and
//from CLI environment
C:\xampp\htdocs\dev\t2\Last-Hammer
cli
the problem is that this work correctly from Web environment but not work correctly in CLI, when i try to execute like: php cron.php code try to make a file_get_contents... like this using cli get this error:
PHP Warning: file_get_contents(C:\xampp\htdocs\dev\t2\Last-Hammer\configs\configs/const/client.xml): failed to open stream: No such file or directory in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php on line 24
Warning: file_get_contents(C:\xampp\htdocs\dev\t2\Last-Hammer\configs\configs/const/client.xml): failed to open stream: No such file or directory in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php on line 24
what is expected is that both for CLI or WEB, the root of the project is similar to: C:/xampp/htdocs/dev/t2/Last-Hammer/ and does not change constantly in the case of CLI depending on where it is executed the php file, as root could set in CLI. regardless of where it runs.
You are lacking trailing "\" on cli case. to uniform both, you could use str_replace():
$env= '';
if (php_sapi_name() == 'cli') {
$env= 'cli';
if (!isset($_SERVER['PWD'])) {
$path = dirname(__DIR__).'\\';
} else {
$path = dirname($_SERVER['PWD']).'\\';
}
} else {
$env= 'web';
$path = $_SERVER['DOCUMENT_ROOT'];
}
$path = str_replace( '\\', '/', $path );
echo $path;
echo PHP_EOL;
echo $env;
file_get_contents($path.'configs/const/client.xml')
define in your main scripts:
define('DOCROOT', '../'); // this in your cron.php
define('DOCROOT', './'); // this in your index.php
Just use relative paths:
$path = DOCROOT;
file_get_contents($path.'configs/const/client.xml')

Why do I get "Warning: require_once(config): failed to open stream: No such file or directory" when trying to run this code?

I'm currently working on programming my very own online store with NetBeans IDE 8.0.2 using PHP. My system is Windows 7 32bit and my localhost is powered by WampServer 2.5. I'm following THC Courses: https://www.youtube.com/playlist?list=PLbXVpMmmrntAvOYgkqhHW0hVu8dWUNyfz
So far everything was going great but I got stock at this video: S2 {Building Framework} Class and method (p6). The guy is asking to echo a sample text on the screen to test the code, but I get these two error messages when running the project on localhost:
Warning: require_once(config): failed to open stream: No such file or directory in C:\wamp\www\ecommerce\inc\autoload.php on line 2
Fatal error: require_once(): Failed opening required 'config' (include_path='.;C:\php\pear') in C:\wamp\www\ecommerce\inc\autoload.php on line 2
autoload.php:
<?php
require_once('config');
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
Core.php:
<?php
class Core {
public function run() {
echo "Hello this is a print test";
}
}
index.php:
<?php
require_once'inc/autoload.php';
$core = new Core();
$core->run();
config.php:
<?php
if(!isset($_SESSION)) {
session_start();
}
//site domain name with http
defined("SITE_URL")
||define("SITE_URL", "http://".$_SERVER['SERVER_NAME']);
//directory seperator
defined("DS")
||define("DS", DIRECTORY_SEPERATOR);
//root path
defined("ROOT_PATH")
||define("ROOT_PATH", realpath(dirname(__FILE__) .DS.".." .DS));
//classes folder
defined("CLASSES_DIR")
||define("CLASSES_DIR", classes);
//pages folder
defined("PAGES_DIR")
||define("PAGES_DIR", pages);
//modules folder
defined("MOD_DIR")
||define("MOD_DIR", "mod");
//inc folder
defined("INC_DIR")
||define("INC_DIR", "inc");
//templates folder
defined("TEMPLATE_DIR")
||define("TEMPLATE_DIR", "template");
//emails path
defined("EMAILS_PATH")
||define("EMAILS_PATH", ROOTH_PATH.DS. "emails");
//catalogue images path
defined("CATALOGUE_PATH")
||define("CATALOGUE_PATH", ROOTH_PATH.DS. "media" .DS."catalogue");
//add all above directories to the include path
set_include_path(implode(PATH_SEPERATOR, array(
realpath(ROOTH_PATH.DS.CLASSES_DIR),
realpath(ROOTH_PATH.DS.PAGES_DIR),
realpath(ROOTH_PATH.DS.MOD_DIR),
realpath(ROOTH_PATH.DS.INC_DIR),
realpath(ROOTH_PATH.DS.TEMPLATE_DIR).
get_include_path()
)));
Change this:
require_once('config');
to:
require_once('config.php');
//^^^See here file extension
(Also make sure it's in the same directory with autoload.php, otherwise change the path)
EDIT:
Or try i with a absolute path like this:
require_once(dirname(__FILE__) . "/config.php");
EDIT 2:
Since you now get error messages from the config file, means that it got included, but still has some errors in it!
The first would be this:
//directory seperator
defined("DS")
||define("DS", DIRECTORY_SEPERATOR);
//^^^^^^^^^^^^^^^^^^^ Typo must be: DIRECTORY_SEPARATOR
Next one is here:
//classes folder
defined("CLASSES_DIR")
||define("CLASSES_DIR", classes);
//^^^^^^^ This isn't a constant so if it is a string put quotes around it
Same error here:
//pages folder
defined("PAGES_DIR")
||define("PAGES_DIR", pages);
//^^^^^
Next error here:
//emails path
defined("EMAILS_PATH")
||define("EMAILS_PATH", ROOTH_PATH . DS . "emails");
//^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much
Same here:
//catalogue images path
defined("CATALOGUE_PATH")
||define("CATALOGUE_PATH", ROOTH_PATH.DS. "media" .DS."catalogue");
//^^^^^^^^^^
And all over the palce you have 6 typos here:
//add all above directories to the include path
set_include_path(implode(PATH_SEPERATOR, array(
//^^^^^^^^^^^^^^ Typo must be: PATH_SEPARATOR
realpath(ROOTH_PATH.DS.CLASSES_DIR),
//^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much
realpath(ROOTH_PATH.DS.PAGES_DIR),
//^^^^^^^^^^
realpath(ROOTH_PATH.DS.MOD_DIR),
//^^^^^^^^^^
realpath(ROOTH_PATH.DS.INC_DIR),
//^^^^^^^^^^
realpath(ROOTH_PATH.DS.TEMPLATE_DIR).
//^^^^^^^^^^
get_include_path()
)));
EDIT 3:
Here you can simplify these 2 lines and i would change the require, so it works even if you include the file itself into another one! Like this:
autoload.php:
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
to this:
function __autoload($class_name) {
$path = str_replace("_", "/", $class_name) . ".php";
require_once(dirname(__FILE__) . "/" . $path);
}

Getting the base path/URL

I am trying to get the base path of the documents through a function as I do not want to find the paths like ../folder1/folder2/mypage.php or ../../../folder1/folder2/somepage.php.
Therefore I tried...
function getBaseUrl() {
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
// return: http://localhost/myproject/
return $protocol.$hostName.$pathInfo['dirname']."/";
}
Then i give write the code...
$base = getBaseUrl();
require_once $base.'_include/db/qry.php';
require_once $base.'_include/db/functions.php';
Both the files qry.php & functions.php is in http://localhost/mysite/_include/db/
While i run the page, error shows ...
Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\mysite\_include\header.php on line 9
Warning: require_once(http://localhost/mysite/_include/db/qry.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\mysite\_include\header.php on line 9
Fatal error: require_once(): Failed opening required 'http://localhost/mysite/_include/db/qry.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\mysite\_include\header.php on line 9
I tried by echoing the getBaseUrl() like echo $base; and it is showing the right path i.e. http://localhost/mysite/.
What should I do ?
you can use $_SERVER['DOCUMENT_ROOT']
You should just use the absolute path on the server instead of url.
You could get the base path by using __DIR__.
For example:
// just example, change to fit your real path.
$base = __DIR__ . '/../';
require_once $base.'_include/db/qry.php';
require_once $base.'_include/db/functions.php';

Working on localhost but server returns “Failed to open stream: No such file or directory”

I'm a newbie that really needs help, been trying to make this php script work with no luck, when I'm running the site locally it works but after I move the files to the server Im getting the following error, hope someone can help me:
Warning: include(/var/chroot/home/content/16/5976816/html/inc/header.php) [function.include]: failed to open stream: No such file or directory in /home/content/16/5976816/html/vtr/test/index.php on line 49
This is the scrip that I'm using:
<?php
// Include the header:
include($_SERVER['DOCUMENT_ROOT'] . '/inc/header.php');
?>
<div id="contents">
<div id="content">
<?php
// Define our array of allowed $_GET values
$pass = array('intro','about','vincent-tran','philip-roggeveen','estate-planning','criminal-case','personal-injuries','bankruptcy','inmigration','deportation','family-law','litigation','corporations-and-llcs', 'payments','consultation','request-callback','contact-us');
// If the page is allowed, include it:
if (in_array($_GET['id'], $pass)) {
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/' . $_GET['id'] . '.php');
}
// If there is no $_GET['id'] defined, then serve the homepage:
elseif (!isset($_GET['id'])) {
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/intro.php');
}
// If the page is not allowed, send them to an error page:
else {
// This send the 404 header
header("HTTP/1.0 404 Not Found");
// This includes the error page
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/error.php');
}
?>
</div>
</div>
<!-- end OutsideWrap-->
<?php
// Include the footer:
include($_SERVER['DOCUMENT_ROOT'] . '/inc/footer.php');
?>
The error shows that the file you want to include could not be found.
Make sure that:
you are specifying the correct path
directory has right permissions, chmod to 755
You can check the resolved path by echoing it:
echo $_SERVER['DOCUMENT_ROOT'] . '/inc/header.php';
And as far as I can remember, you don't need / after $_SERVER['DOCUMENT_ROOT']
if (strpos($system_folder, '/') === FALSE)
{
if (function_exists('realpath') AND #realpath(dirname(__FILE__)) !== FALSE)
{
$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
}
}
else
{
// Swap directory separators to Unix style for consistency
$system_folder = str_replace("\\", "/", $system_folder);
}
here's a function that gets the base directory of your application
just replace $system_folder with your site's folder name like if that is on htdocs/somewebapp
$systemfolder = "somewebapp";
Thank you guys for all your help, I wasn't using the correct path.
<?php
// Define our array of allowed $_GET values
$pass = array('intro','about','vincent-tran','philip-roggeveen','estate-planning','criminal-case','personal-injuries','bankruptcy','inmigration','deportation','family-law','litigation','corporations-and-llcs', 'payments','consultation','request-callback','contact-us');
// If the page is allowed, include it:
if (in_array($_GET['id'], $pass)) {
include ($_SERVER['DOCUMENT_ROOT'] . '/vtr/today/inc/' . $_GET['id'] . '.php');
}
// If there is no $_GET['id'] defined, then serve the homepage:
elseif (!isset($_GET['id'])) {
include ($_SERVER['DOCUMENT_ROOT'] . '/vtr/today/inc/intro.php');
}
// If the page is not allowed, send them to an error page:
else {
// This send the 404 header
header("HTTP/1.0 404 Not Found");
// This includes the error page
include ($_SERVER['DOCUMENT_ROOT'] . '/vtr/today/inc/error.php');
}
?>
Now is working just fine.

PHP ftp_put - file not Found or no access

How is one supposed to handle files that aren't in the current directory when using ftp_put? This piece of code is trying to upload a file that I know exists, but it always gives the following error:
"Warning: ftp_put() [function.ftp-put]: Requested action not taken, file not found or no access. in /path/to/files/domains/mydomain.com/html/scriptfile.php on line 1337"
Here's the snip:
$file_name = $this->GetFileName();
if ($file_name)
{
$resource = ftp_connect('ftp.remoteftpserver.com');
if ($resource && ftp_login($resource, $username, $pass))
{
ftp_pasv($resource, true);
//UPLOAD_DIRECTORY == '/IN' (it really exists, I'm sure)
//ORDER_DIRECTORY == /home/domains/mydomain.com/orders (came from $_SERVER['DOCUMENT_ROOT']
ftp_put($resource, UPLOAD_DIRECTORY . '/' . $file_name, ORDER_DIRECTORY . '/' . $file_name, FTP_ASCII);
ftp_close($resource);
}
else
{
echo "FTP Connection Failed!";
}
}
Check the permissions of the remote file. Make sure $username has write access to the file. Make sure you have execute access on the parent directory.

Categories