Including file with backward slash is won't work in PHP? - php

Am running scripts using command-line .
Backward include path is doesn't work for me
eg: require("../../../../test.php");
(But in browser it works)

try
dirname(__FILE__); will give you real path to current directory and you're then about le to include your file
require(dirname(__FILE__)."/../../../../test.php");

From the command line, the base path should be the directory from which you are calling php. When running in the browser (through apache, etc) the calling path should be the webroot directory.
So you should specify the directory of the current script or call it from its own directory.
For instance:
require(__DIR__."../../../../test.php");
DIR is a PHP Magic Constant (http://php.net/manual/en/language.constants.predefined.php) which returns the directory of the currently running file.

Related

crontab cannot read dir '../' in php script

Why can't crontab read the '../' dir in the script, but if I access the script through a browser it can and there are no problems.
example: file.php
<?php
include('../config.php');
?>
If I run the script through the terminal config.php it can't be read '/usr/bin/php /home/example.com/public_html/cron/file.php',
whereas if I open it through a browser it runs smoothly
https://example.com/cron/file.php
I use php7.4 and os centos7
This is a perfect use case for why you should avoid using relative paths and instead stick to using absolute paths when including other scripts/files. PHP resolves relative paths according to the CWD (Current Working Directory) which will be different between your web server and your terminal. For the web server it depends on which script invoked the interpreter (in your case this is /home/example.com/public_html/cron/file.php). In the terminal it depends on from which directory the interpreter is loaded in the shell. In the case of a crontab it is the $PWD variable set in the home directory of the user that owns the crontab (so most likely /home/{username}). Of course that those two resolve to completely different paths.
So instead of all this guess work and instead of trying to change the CWD adhoc (which involves further guess work), you could simply use an absolute path at all times.
<?php
include __DIR__ . '/../config.php';
What this allows you to do is set the absolute path to the magic __DIR__ constant, which is a compile-time constant set to the path of the script in which it is used (in your case this is /home/example.com/public_html/cron). Then you can relatively go down one directory from that absolute path in order to get to /home/example.com/public_html from which you can now safely access config.php regardless of how you invoke the script and the given CWD.
.. references the parent directory of wherever the script is executing. It's presumably different in the two scenarios. Try cd /home/example.com/public_html/cron && /usr/bin/php file.php in your crontab, so that you're executing that in the same directory as your web page is.

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.

PHP: How to set current working directory to be same as directory executing the script

I'm in the process of transferring my website from one server to another. I have some php scripts that use the is_readable function which uses the current working directory.
On the old server, when I call getcwd(), it outputs the folder in which the script is being executed. On the new server it outputs the root directory '/'.
I would like to know how I can configure PHP to use the current folder instead of '/'. I don't want to have to change any PHP code that already works on the old server. I can configure the new server, but don't know what settings to change. I'm using apache2, if that helps.
EDIT: It seems as though my working directory is not root like I thought. When I create a testFile.php and echo getcwd() it shows the directory the php file is in. But in my problem file, in the same directory, getcwd() shows up as '/'
chdir(__DIR__);
or
chdir(dirname(__FILE__));
(see chdir and magic constants).
But that should be by default.
This is normal in CLI mode:
It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)
a quick workaround would be
chdir(dirname(__FILE__));
You can get the current directory a script is in with dirname(__FILE__) or __DIR__ if >= PHP 5.3.

php mkdir windows relative path

I want to create a directory on windows from a PHP script.
My script is in the www/Test directory of Apache and I want to create a folder (fold1) inside www/downloads directory.
Inside the script, I'm using:
$dirName = "../downloads/fold1";
mkdir("{$dirName}");
If I use the full path of dirName like C:\Apache\www\downloads\fold1, it works fine.
But I want to use a relative path since this code will be sent to the client.
I would guess your current directory is different from your files folder, so you have to use a trick:
mkdir(dirname(__FILE__) . "/" . $relative_path);
dirname(__FILE___) returns the absolute path of your current php file. With this you can build an absolut path.

Properly navigating relative local paths in PHP

I'm having trouble specifically with the getimagesize function. I'm making the function call from /item/ajax/image.php relative to the domain's HTTP root. I'm trying to get the dimensions of an image stored at /portfolio/15/image.jpg. From what I understand, the function takes a filename as an argument, so I tried the following:
getimagesize('/portfolio/15/image.jpg')
And
getimagesize('../../portfolio/15/image.jpg')
But both of them just threw PHP errors.
try prefixing below to path:
$_SERVER['DOCUMENT_ROOT']
Relative paths always start from the file that is executed, which is most likely index.php. This is true for included files as well. This means in any file within you project relative paths start from your index.php. (Except a chdir() is done before)
I think it is really bad code to have paths like "../../file.ext" or the like. Define a Constant that has the full path to your application (eg: $_SERVER['DOCUMENT_ROOT']) and prepend it to any path you're using.
Example:
# somewhere in your index.php
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
# in any included file
$my_path = ROOT_PATH."/portfolio/14/image.jpg"
This is imho the cleanest and most readable way to define paths.
In PHP "/" is not the same as the Apache "/" (web root). In PHP "/" refers to the system root. You should use paths relative to your PHP script location ('portfolio/15/image.jpg' if your script and the 'portfolio' folder are in the same location)
The filename you enter is not related to the http root but should be an existing path in the file system of your web server.
To see what goes wrong you could enter:
realpath('../../portfolio/15/image.jpg')
To see what directory you end up at.
Or use:
imagesize(dirname(__FILE__) . '/../../portfolio/15/image.jpg')
to get the full directory qualification.
As an alternative you can use the web address, but you should specify the full url:
getimagesize('http://yoursite.com/portfolio/15/image.jpg')
However, this is a slower option.

Categories