setting function path in php? - php

I have gcm in folder (dirC)...here the paths
index.php
dirA/
dirB/
dirC/GCM.php
----/config.php
----/sendMsg.php
dirD/
dirE/dirE1/test.php //send from here
dirF/
gcm.php have code bellow
class GCM {
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
//other code here
echo 'test';
}
}
and i successfull send message from sendMsg.php,here the code
include_once './GCM.php';
$message="hello word";
$gcm = new GCM();
$registatoin_ids = array("SEJFOJOEUJFPUWPJR0923740JEU092308UPUPUAUOJLJLJJVPW634");
$message = array($message);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
my question is how to set the path to communicate with GCM.php using include_once from test.php ?
here test.php code
include_once './GCM.php'; //my proble is here ???????????
$message="hello word";
$gcm = new GCM();
$registatoin_ids = array("SEJFOJOEUJFPUWPJR0923740JEU092308UPUPUAUOJLJLJJVPW634");
$message = array($message);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
thanks in advance..

When you want to get to your desired directory, you need to use ../, which means 'move one directory up'.
Example:
we have a directory structure:
/website
/website/include
/website/template
Imagine we are in /website/template directory.
If we want to include /website/include/GCM.php
We can do it with an absolute path, / means the root directory:
include_once '/website/include/GCM.php';
We can do it with a relative path:
include_once '../include/GCM.php';
../ means 'move 1 directory up'.
For more information: http://www.geeksengine.com/article/absolute-relative-path.html

The question is not really clear. But i think the problem is that you make a folder, from which you include and you dont know what folder that is?
For that you have magic constants in php:
http://php.net/manual/en/language.constants.predefined.php

From php documentation you can do something like that :
$path = '/dirC';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
In your index.php (if it's your controller), or in a conf/prepend file, which is included in all your script.
Then a simple include('GCM.php'); will work.

Related

How to use wordpress shortcodes in custom php file folder?

I want to display wp formcraft (plugin) in my custom PHP file so I wanted to use "do_shortcode()" function but nothing works it just shows white screen.
my php file code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
define('WP_USE_THEMES', false);
echo apply_filters( "the_content","[fc id='1'][/fc]");
?>
I don't really get what your doing, but i guess you want to do something like this:
function xyz() {
do_shortcode("[fc id='1'][/fc]");
}
echo apply_filters("the_content", "xyz");

include class files from another directory, php

Trying to make a file structure for class, api and etc. But when I try to include a class into api/ file, problem happens... it's look like I can't give the file path's correctly. But couldn't find what is wrong.
the directory and file structure is like image below.
Insert class
class Insert extends Dbc {
public function getProjects(){
$sql = "SELECT * FROM projects";
$stmt = $this->connect()->query($sql);
while($row = $stmt->fetch()){
$row["project_name"] . "<br>";
}
}
}
autoload inc
spl_autoload_register('autoloader');
function autoloader($className){
$path = "classes/";
$extension = ".class.php";
$fullPath = $path . $className . $extension;
if (!file_exists($fullPath)) {
return false;
}
include_once $fullPath;
}
and finally, /api file projects.api
<?php
include "../inc/autoload.inc.php";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$projects = new Insert();
echo $projects->getProjects();
?>
</body>
</html>
But here is the thing I am getting a 500 error. AND when I use try and catch on $projects = new Insert(); in api file. It says can't find the class... It means include "../inc/autoload.inc.php"; not working correctly?
I solved the issue. The mistake was I didn't include the class inside the autoload file. So it doesn't find the class.. Here is the new autoload.inc.php
spl_autoload_register('autoloader');
include "../classes/insert.class.php";
function autoloader($className){
$path = "../classes/";
$extension = ".class.php";
$fullPath = $path . $className . $extension;
if (!file_exists($fullPath)) {
return false;
}
include_once $fullPath;
}

php ews connection to Exchange 2010

I downloaded the PHP ews database from https://github.com/jamesiarmes/php-ews.
Autoloader:
function __autoload ($className){
preg_match ("/^(([a-zA-Z]{5})_)?(.+)$/",$className,&$treffer); # die ersten 5 Stellen=Verzeichnisname, Weitere Zeichen=Dateiname
if(file_exists(PROJEKT_DIR.$className.".class.php")) include_once(PROJEKT_DIR.$className.".class.php");
else{
$pfad=SCRIPT_DIR."include/";
if($treffer[2]) $pfad.="classes/".$treffer[2]."/";
if(file_exists($pfad.$treffer[3].".class.php"))
include_once($pfad.$treffer[3].".class.php");
elseif(substr($treffer[3],-7)!="_bvstnd" and class_exists($className."_bvstnd")){
eval("class $className extends ".$className."_bvstnd {} ");
}
else{
// Start from the base path and determine the location from the class name,
$pfad=SCRIPT_DIR."include/php-ews";
$include_file = $pfad . '/' . str_replace('_', '/', $className) . '.php';
return (file_exists($include_file) ? require_once $include_file : false);
}
}
#if(file_exists(SCRIPT_DIR."include/".$className.".class.php"))
# include_once(SCRIPT_DIR."include/".$className.".class.php");
}
it also load some other files.
Then I started doing the Guide from his site, I started doing this:
<?php
$host = "*********";
$username="**********";
$password="***********";
$version= "***********";
$ews = new ExchangeWebServices($host, $username, $password, $version);
$request = new EWSType_FindFolderType();
$request->Traversal = EWSType_FolderQueryTraversalType::SHALLOW;
$request->FolderShape = new EWSType_FolderResponseShapeType();
$request->FolderShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
// configure the view
$request->IndexedPageFolderView = new EWSType_IndexedPageViewType();
$request->IndexedPageFolderView->BasePoint = 'Beginning';
$request->IndexedPageFolderView->Offset = 0;
// set the starting folder as the inbox
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
// make the actual call
$response = $ews->FindFolder($request);
?>
At first the Site at the browser just load very long but then tell me something like this: class Exception is undefined. I can't tell the correct message because now this Message doesn´t even show up if I load the script.
The Browser just load infinitely. After this I can't even connect to my server with my PHP files. I have to open my other Browser to connect again.
If I open up the script in my other Browser then I can run the script again but it´s again loading infinity. (I include all my files I need with autoloader so that's not the Problem)
Does anybody have a Problem like that and found a solution?
You have an issue with your autoloader. The default files of that library are loaded like this:
$pfad=SCRIPT_DIR."include/php-ews";
$include_file = $pfad . '/' . str_replace('_', '/', $className) . '.php';
If your autoloader is the first to try and load that exception, it will replace the '_'. If you put an error_log in that autoloader function you will probably see the result of $inlcude_file to be something like
include/php-ews/EWS/Exception
And that file doesn't exist.
So you should fix your autoloader so it can actually find the file.
To be aboslutely clear:
you (the code) are looking for the class EWS_Exception
this is in the file EWS_Exception.php (in the root of the project)
your autoloader cannot find that file as you replace all _
so the sollution is to either fix your autoloader, or just include that EWS_Exception.php file somewhere.

Error shown even if directory already exists

I am making a intranet customer manager in PHP. For each customer a directory is created for the shop to add files into. What my script is supposed do is if no directory exists create it, if it does exists dont create it.
What is actually happening is if the directory already exists I am getting the following error :
Warning: mkdir() [function.mkdir]: File exists in C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo m_code\custom_code.php(18) : eval()'d code on line 14
So what is happening it is trying to create it anyway, even though the if statement should stop it ?, im confused on what I am doing wrong :-S .
<?php
$customerID = $_GET['cfid'];
$directory = "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
$thisdir = getcwd();
mkdir($thisdir ."/customer-files/$customerID" , 0777); }
?>
Replace:
if(file_exists($directory) && is_dir($directory)) {
with:
$thisdir = getcwd();
if(file_exists($thisdir.$directory) && is_dir($thisdir.$directory)) {
or better:
<?php
$customerID = $_GET['cfid'];
$directory = "./customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
mkdir($directory , 0777); }
?>
Just took a short look but i would try this:
$directory = $thisdir . "/customer-files/$customerID";
and remove $thisdir from mkdir();
also you should move your $thisdir before the $directory declaration
The function file_exists() does not use relative paths, where is_dir() can. So instead, use the common denominator and pass an absolute path to these functions. Additionally you can move the call to getcwd() into the $directory assignment and reuse $directory later for creating the directory.
<?php
$customerID = $_GET['cfid'];
// Get full path to directory
$directory = getcwd() . "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
// Do nothing
}
else {
// Directory doesn't exist, make it
mkdir($directory , 0777); }
}
?>

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.

Categories