Ali
Ali

Reputation: 7493

Accessing a defined constant returns messed up characters

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.

Upvotes: 0

Views: 68

Answers (3)

amphetamachine
amphetamachine

Reputation: 30595

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 \'

Upvotes: 1

coolkid
coolkid

Reputation: 543

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.

Upvotes: 0

Amber
Amber

Reputation: 526643

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

Upvotes: 3

Related Questions