Using require() doesn't load php file - php

//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');
}

Related

PHP Include path specifically always at root document

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.

"ifelse" command in the "include"

where in error?
<?
if($_GET['data'])
{
print 'atmam';
include ('http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1');
}
else {
print 'fail to download'; }
?>
Written on the screen error:
Warning: Unexpected character in input: '' (ASCII=1) state=1 in http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 on line 515
Parse error: syntax error, unexpected T_STRING in http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 on line 515
PS: http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 = direct file download link
Can you help?
Best regards
See if this does something like what you want:
<?php
if ($_GET['data']) {
// Some headers that indicate a generic download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
// Try and read the file directly to the client
if (!#readfile('http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1')) {
// Try and clear the header and print a message. This may not work depending on the result of the readfile() attempt.
#header('Content-Disposition:');
print 'fail to download';
}
exit;
}
?>
An alternative (and many would argue better) approach is this:
<?php
if ($_GET['data']) {
// Redirect the client to the actual location
header('HTTP/1.1 302 Found');
header('Location: http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1');
exit;
}
?>
PHP provides a function called file_exists for this purpose. $_GET['data'] is for getting information out of the html element with name="data" attached to it. If you do not have this in your document the script will NEVER run as its looking for something that doesn't exist. Even if the element does exist, this is not a necessary nor recommended use of $_GET.
To find out more about what you're trying to do check this link, and look at my examples.
http://php.net/manual/en/function.file-exists.php
To use it you simply do this:
$filename = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
if (file_exists($filename)) {
echo 'atmam';
include ('$filename');
}
else
echo 'Failed to download file';
I'm assuming you are going to use this for any files your site has to access, to save you time you can use it in a function.
function testfile(filename) {
if (file_exists(filename)) {
echo 'atmam';
include ('filename');
}
else
echo 'Failed to download file';
}
Call the function like this:
$filename1 = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
$filename2 = 'something.txt';
function testfile($filename1);
Using the function you can check as many file names as you want using variables for each file name.
Edit: To solve the syntax error that is coming up, you must make sure the file being included has no errors. Please post it here for us to look at. Removing echos and prints will change nothing, in fact you want those in there in order to debug. First try using the small bit of code I've put here, this is the correct method to check if a file exists, and if it does, do something. Once you are using the correct code to check the file and include it, then you will be sure that once you fix any issues in the include file you are going to have the functionality you want.
Hope this helps you out!
-Sean

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().

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

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