I'm using PHP for my website. In one of my configuration files I'm defining a constant as follows:
define('CORE_PATH', BASE_PATH.'\core');
In another file I'm using this constant in a filepath configuration as follows:
require_once( CORE_PATH."\functions\common.php" );
But it's not working it's taking as
C:\wamp\www\smart-rebate-web\coreunctions\common.php instead of
C:\wamp\www\smart-rebate-web\core\functions\common.php
I'm not getting mhy this is happening and how to make this thing correct so that the proper path should set in confgiuration file?
The value of CORE_PATH constant is as below:
C:\wamp\www\smart-rebate-web\core
Than ks in advance.
I'd start with replacing this:
require_once( CORE_PATH."\functions\common.php" );
with this:
require_once( CORE_PATH.'\functions\common.php' );
The reason is that your string is with double quotes, and inside double quotes, \f means formfeed (hex 0C), according to the PHP manual. At the same time, \c isn't a special sequence, so the end result is unctions\common.php. Combined with your CORE_PATH, it results in C:\wamp\www\smart-rebate-web\coreunctions\common.php.
Tip: Use double-quotes strings only when you are sure you need them - interpolating variables, special sequences, SQL code, etc. They can ruin your day if you miss something unwanted in them. Also, when dealing with system paths, it's best to use DIRECTORY_SEPARATOR instead of / or \. This way you can easily migrate from one platform to another without having to refactor your code.
Cheers.
Related
I know there're plenty of topics regarding escaping characters but I just can't find the solution for my problem.
It's very easy. This is string I have:
$path = "C:\Users\Me\Desktop\14409238.jpg";
Howver, no matter how many escaping techniques I use, I can't manage to display the correct path without destroying it. In all cases the \14 will be replaced with
C:\Users\Me\Desktopd09238.jpg
How do I solve this?
Don't use backslashes in PHP for windows paths. It's smart enough to convert for you:
$path = "c:/users/me/desktop/...";
Using backslashes runs into the exact problem you have - backslashing certain characters turns them into metacharacters, not regular characters.
try to change, the Physical path to access the image, stored on Desktop can be written as,
$path = "C:\Users\Me\Desktop\14409238.jpg";
to
$path = "C:\\Users\\Me\\Desktop\\14409238.jpg";
Avoid the situation entirely, PHP under Windows allows you to submit paths with the backslash
c://Users/Me/Desktop/file.jpg
This also avoids interoperability headaches when a script must run within .nix and Windows.
I am running PHP on an IIS and I am having trouble with magic constants
I wish to use a framework(YII) which has alot of the following:
require(__DIR__ . '\..\vendor\autoload.php');
Problem is that this returns:
G:\PleskVhosts\***.com\api\web/../vendor/autoload.php
Which causes obvious problems
So I was wondering if there was a way to configure PHP so that the magic constants always return with / (To my understanding IIS/Windows accepts both back-slash and forward-slash)
I don't think it matters if you use / or \ in file paths, Windows usually works with both. It won't work with a single backslash though, you need to double backslash it in your code to escape the backslash if that makes sense. Try running...
var_dump(file_exists(__DIR__ . '/../vendor/autoload.php'));
If it returns true then there shouldn't be a problem. Otherwise double check the file path is actually correct. You can also use realpath to translate the /../ into a full path first.
I've never even considered this before, but one of my clients is trying to name a file starting with a $; $template.php.
Is this allowed?
I'd like to be able to give him a reason if it's no allowed?
Thanks!
That depends entirely on what filesystem you're using. You can start a filename with $ on all of the major filesystems I know of — FAT, FAT32, NTFS, ext2, ext3, ext4, ... (Huh. My experience of file systems is actually quite limited. Interesting.)
Remember that if you're doing this from PHP, you need to be sure you're not using a string literal in double quotes, because within a string literal in double quotes, $ introduces a variable. If you're using a literal $, either use single quotes or escape it with a backslash.
This is strange I have a constant defined as such:
define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\assets\files\\");
However when I try to access the constant in code I get this weird result on my localhost..
C:\wamp\www\my.app\assetsiles\2688
The \f is replaced by a square indicating it as an unrecognised character. Whats happening because of this I am unable to save files to the folder the constant is supposed to point to.
You need to escape the backslashes before the a and the f:
define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\\assets\\files\\");
Otherwise they're interpreted as escape codes, since they're within a string. (Just like \n is a newline, et cetera.)
You could—and probably should—just use forward slashes (/) in your file/directory paths. PHP will automatically convert them to the value of the built-in system-dependent constant DIRECTORY_SEPARATOR when using the string as a file path. This is by far the most cross-platform method of doing it.
Alternatively, you could use single quotes. They interpolate backslashes (\) differently in that most escapes are ignored and just interpreted literally (the exceptions being \\ and \').
# *
define('RJ_FILES_PATH', RJ_SITE_DIRECTORY.'\assets\files\\');
# * still need an escape here because of \'
You should escape the backlash by double it: \
In my opinion, you always should use '/', because it work fine in windows and linux. In php, there's a constant DIRECTORY_SEPARATOR but it's uneccesary because '/' work fine.
Is there any difference between typing:
<?php echo $_SERVER[REQUEST_URI] ?>
or
<?php echo $_SERVER['REQUEST_URI'] ?>
or
<?php echo $_SERVER["REQUEST_URI"] ?>
?
They all work... I use the first one.
Maybe one is faster than the other?
Without quotes PHP interprets the REQUEST_URI as a constant but corrects your typo error if there is no such constant and interprets it as string.
When error_reporting includes E_NOTICE, you would probably get an error such as:
Notice: Use of undefined constant REQUEST_URI - assumed 'REQUEST_URI' in <file path> on line <line number>
But if there is a constant with this name, PHP will use the constant’s value instead. (See also Array do's and don'ts)
So always use quotes when you mean a string. Otherwise it can have unwanted side effects.
And for the difference of single and double quoted strings, see the PHP manual about strings.
The first one is wrong - you're actually looking for a constant REQUEST_URI that doesn't exist. This will generate a notice-level warning.
There's no difference between the other two.
There is a difference between single and double quotes in PHP string handling. A string enclosed in double quotes will be evaluated for embedded variables and escape characters (e.g. \n); a string enclosed in single quotes won't (or not as much).
So, for example,
$hello = "world";
echo "Hello $hello!\n";
echo 'Hello $hello!\n';
echo 'Done';
will output
Hello world!Hello $hello!\nDone
In situations where you have no escape characters or embedded variables, it is slightly more efficient to use single quotes as it requires less processing of the string by the runtime. However, many people (me included) prefer to use double quotes for all strings to save confusion.
As a caveat to Gumbo's answer the third representation - double quotes - actually makes PHP look for variables inside that string. Thus that method might be a little slower (although in a string of 11 characters it'll be negligible - it's better practice not to make PHP do that however).
When PHP comes across plain strings being used as array keys it checks if there is a constant with that name and if there isn't it defaults it back to an array key. Therefore, not using quote marks causes a slight performance hit and there is a possibility that the result will not be what you expect.
$_SERVER[REQUEST_URI]
is syntatically incorrect and AFAIK will not run on a default installation of PHP5. The array index is a string so it needs to be passed on strings. I know PHP4 converted undefined constants to strings inside the square brackets but it's still not good practice.
EDIT: Well unless you define a constant called REQUEST_URI, which you haven't in your example script.
$_SERVER['REQUEST_URI']
is the standard method and what you should be using.
$_SERVER["REQUEST_URI"]
also works and while not wrong is slightly more work for the PHP interpreter so unless you need to parse it for variables should not be used. (and if you need to do so, you need to rethink that part of your program.