简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM