PHP Include path specifically always at root document - php

I am vague at understanding the include paths and how they are written out, I know how to set them within the ini file and how to functionally do it set_include_path just not how to get it to be exact each time no matter what.
So I have an admin autoload file that I include in all my headers to register the spl_autoload_register function. I just keep getting errors in my error_log file. It says that
PHP Fatal error: Class 'Configurate' not found in
/home/~username~/public_html/testing_ini.php on line 5
So what I am looking for is how can I set the include path to always be the directory before the public_html directory no matter where I am?
I've tried setting the include path to such
.:/opt/alt/php5/usr/share/pear:/opt/alt/php5/usr/share/phphome/~username~/classes/Configurate.php
But I still get the error. Any help and some tips to understanding this entire thing? I suck at relative paths
As per requested spl_autoload_register function
<?php
$ini = parse_ini_file("configurations.ini",true);
foreach($ini as $section) {
foreach($section as $key=>$value ) {
define("__".strtoupper($key)."__",$value);
}
}
//if(__USERNAME__ == null) {
// header("Location: /setup.php?step=1");
// exit();
//}
spl_autoload_register(function($class) {
try {
if(!file_exists("../classes/{$class}.php")){
throw new Exception("/classes/{$class}.php does not exist error on line ". __LINE__." in file ". realpath(__FILE__));
} else
require_once "../classes/{$class}.php";
} catch (Exception $ex) {
echo $ex->getMessage()."<br>";
echo $ex->getCode();
}
});

getcwd() and many other methods did not work for me. Though I did find this one method that works great for me in any directory.
set_include_path(
dirname( $_SERVER["DOCUMENT_ROOT"] )
);
Hope this helps anyone else out there looking to get outside the document root and using it like I do.

Related

Best way to see if a view file (.ctp) exists in Cake PHP?

I am using CakePHP 3, and would like to change the behaviour of the handy PagesController which comes with the installation.
The current solution they use when trying to find and render a view file (.ctp) is using a try{} block, which is working well.
Actual code:
try {
$this->render(implode('/', $path));
} catch (MissingTemplateException $e) {
But in my case the most common situation will be that the .ctp file does not exist. (If it does not exist, it will go on with a default view and try fetch content from database, but it is not my problem here.)
In my modified version the most normal case will be that the MissingTemplaceException is thrown, which seem a bit overkill.
Why can I not simply check if the file exists?
Am I thinking right here? And if I am, how do I check for the file's existence?
After some fiddling around, I found the APP constant. This works:
$path = func_get_args();
$file = APP.'Template'.DS.'Pages'.DS.implode('/', $path).'.ctp';
if (file_exists($file))
{
// Render the file.
}
else
{
// Render some default file.
}
Why can I not simply check if the file exists?
I don't know why you can't. Just use file_exists()?
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

Using require() doesn't load php file

//index.php
if(isset($_GET["action"])){
if($_GET["action"]=="add_admin"){
require("../gbl_admin/admin_header.php");
require("../gbl_admin/admin_add.php");
}
}
//admin_header.php
require("../gbl_admin/db/db_ini.php");
require("../gbl_admin/classes/dbmgmt.php");
require("../gbl_admin/configuration/passwordhash.php");
I have created a in dbmgmt to handle database but after calling it through index.php and submitting the form it gives me "Creating default object from empty value " error as well as "Call to undefined function create_hash()" error from passwordhash.php file. This means that the files are not being loaded by index.php. How do i fix this??
I would suggest you always use absolute paths by using __DIR__, or for less than PHP 5.3, use dirname(__FILE__)
This will mean that your include should be along the lines of
require(__DIR__."/../gbl_admin/admin_header.php");
However, as you are using require, and you are saying the files are not being required, the only logical explanation is that your if condition is not being satisfied.
Try:
if(isset($_GET["action"])){
if($_GET["action"]=="add_admin"){
require("../gbl_admin/admin_header.php");
require("../gbl_admin/admin_add.php");
}else{
die('error 1');
}
}else{
die('error 2');
}

Check if directory is created (Wordpress and/or PHP)

I'm relatively new to PHP and I'm trying to write my own plugin. Upon plugin activation it will run the following function:
function kb_create_uploadfolder () {
global $wpdp;
$upload_dir = wp_upload_dir();
$upload_dir = $upload_dir['basedir'] . "/plugin_uploads";
$upload_dircheck = wp_mkdir_p($upload_dir);
}
I didn't bother to check whether the directory already exists before creating it since I figured it won't overwrite anything or delete the contents if it does. Correct me if I'm wrong.
The thing is however, I would like to check if the creation of the directory was succesful or not but I can't figure out how to get this information.
Use is_dir():
if(is_dir($upload_dircheck))
{
echo "It is a dir";
}
else
{
echo "Sorry, non-existent or not a dir";
}
Also, mkdir() doesn't delete or overwrite existing contents, it just creates a directory if it does not yet exist.
If you're using PHP 4 or newer then you can use the is_dir() function.
Try is_dir().

Deleting files in higher directory

I'm having problems deleting a file from a higher directory, I found this post and tried it but no luck....:
gotdalife at gmail dot com 25-Sep-2008
02:04
To anyone who's had a problem with the
permissions denied error, it's
sometimes caused when you try to
delete a file that's in a folder
higher in the hierarchy to your
working directory (i.e. when trying to
delete a path that starts with "../").
So to work around this problem, you
can use chdir() to change the working
directory to the folder where the file
you want to unlink is located.
<?php
> $old = getcwd(); // Save the current directory
> chdir($path_to_file);
> unlink($filename);
> chdir($old); // Restore the old working directory ?>
here is the code that I currently have:
session_start();
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] !=md5($_SERVER['HTTP_USER_AGENT']))){
require_once ('includes/login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit();
}
$folder = $_GET['folder'];
$filename = $_GET['name'];
$path = "../gallery/photos/$folder";
if (isset($_POST['submitted'])) {
if ($_POST['sure'] == 'Yes') {
$old = getcwd(); // Save the current directory
chdir($path);
unlink($filename);
chdir($old); // Restore the old working directory
}
else{
echo '<p>The photo has NOT been deleted.</p>';
}
}
I'm getting the error message :
Warning: unlink() [function.unlink]:
No error in
J:\xampp\htdocs\bunker\admin\delete_file.php
on line 37
line 37 being:
unlink($filename);
can anybody see what I've done wrong?
I always use absolute filepath names.
I'd define the filedir as a constant in your config, then concatenate so you have an absolute filepath, then make a call to unlink().
Btw: I hope you know your code is highly insecure.
See here:
http://bugs.php.net/bug.php?id=43511
and here
http://php.bigresource.com/Track-php-03TimDKO/
http://www.phpbuilder.com/board/showthread.php?t=10357994
Though I wouldnt recommend doing this, as per the comments above. Is there the option for a different approach?

PHP: Can include a file that file_exists() says doesn't exist

In my script, I set the include path (so another part of the application can include files too), check that a file exists, and include it.
However, after I set the include path, file_exists() reports that the file does not exist, yet I can still include the same file.
<?php
$include_path = realpath('path/to/some/directory');
if(!is_string($include_path) || !is_dir($include_path))
{
return false;
}
set_include_path(
implode(PATH_SEPARATOR, array(
$include_path,
get_include_path()
))
);
// Bootstrap file is located at: "path/to/some/directory/bootstrap.php".
$bootstrap = 'bootstrap.php';
// Returns "bool(true)".
var_dump(file_exists($include_path . '/' . $bootstrap));
// Returns "bool(false)".
var_dump(file_exists($bootstrap));
// This led me to believe that the include path was not being set properly.
// But it is. The next thing is what puzzles me.
require_once $bootstrap;
// Not only are there no errors, but the file is included successfully!
I can edit the include path and include files without providing the absolute filepath, but I cannot check whether they exist or not. This is really annoying as every time a file that does not exist is called, my application results in a fatal error, or at best a warning (using include_once()).
Turning errors and warnings off is not an option, unfortunately.
Can anyone explain what is causing this behaviour?
file_exists does nothing more than say whether a file exists (and the script is allowed to know it exists), resolving the path relative to the cwd. It does not care about the include path.
Yes Here is the Simplest way to implement this
$file_name = //Pass File name
if ( file_exists($file_name) )
{
echo "Exist";
}
else
{
echo "Not Exist";
}

Categories