PHP replace double backslashes "\\" to a single backslash "\" - php

Okay so im working on this php image upload system but for some reason internet explorer turns my basepath into the same path, but with double backslashes instead of one; ie:
C:\\Documents and Settings\\kasper\\Bureaublad\\24.jpg
This needs to become C:\Documents and Settings\kasper\Bureaublad\24.jpg.

Note that you may be running into PHP's Magic Quotes "feature" where incoming backslashes are turned to \\.
See
http://us2.php.net/magic_quotes

Use the stripslashes function.
That should make them all single slashes.

Have you considered the stripslashes() function?
http://www.php.net/stripslashes

Related

Double backslash in front of local URL

Is there a way to navigate to a file on a local location which uses a double backslash at the start like this: \192.168.1.1\folder\file.xml ?
I have tried several ways like this:
file_get_contents("\\192.168.1.1\folder\file.xml");
fopen("\\192.168.1.1\folder\file.xml", '');
But it keeps failing to open the file. while navigating to it locally seems to work.
you need a backslash to escape every backslash (a backslash is the escape char in coding)
So you would have:
$addr = "\\\\192.168.1.1\\folder\\file.xml";
#Craig B - Beat me to the answer by few seconds, you should use double double
"\\\\192.168.1.1\\folder\\file.xml"

PHP glob can't read tmp

I am using wamp in win7, and I try to read c:\wamp\tmp by glob with php, but the result is an empty array
print_r(glob("C:\wamp\tmp\*"));
I can see any folder in c:\wamp except c:\wamp\tmp, and I make sure that folders has the same setting about read write...
\t is an escape sequence that means the Tab character. You have several ways to deal with it:
Escape it with another backslash.
print_r(glob("C:\wamp\\tmp\*"));
Use single quotes instead of double quotes, since escape sequences aren't processed in single quotes.
print_r(glob('C:\wamp\tmp\*'));
Use forward slashes instead of backslashes, since Windows allows either as a directory separator.
print_r(glob("C:/wamp/tmp/*"));

unlink() doesn't work with double backslashes

I'm trying to delete a file this way:
define('DESTINATION_FOLDER','c:\temp\\');
unlink(DESTINATION_FOLDER.$dest_filename);
And $dest_filename is something like this: 2jfioj23488hgh83hr.zip
But this doesn't work. When I put the path in a variable and echo it, it prints:
c:\temp\2jfioj23488hgh83hr.zip
Then I copy/paste it in unlink():
unlink('c:\temp\2jfioj23488hgh83hr.zip')
And it works. I think the problem is with the double backslashes. But how should I define the path without using the double backslashes? I don't know why it doesn't work. The line just above this code is:
copy((DESTINATION_FOLDER.$dest_filename),($extract.'\\'.$dest_filename));
And it works fine, but unlink doesn't work with the same syntax.
Putting my comment as an answer to the question
Try using forward slashes instead of backslash :)
If for some reason you must use backslashes, then you need to use c:\\ instead of c:\
define('DESTINATION_FOLDER','c:\\temp\\');

require path in php

I have the following code:
require("\create_form\view.php");
and receiving the following error:
Warning: require(\create_formiew.php) [function.require]: failed to open stream: Invalid argument in C:\xampp\htdocs\training\school\STU001_MAIN.php on line 67
You can see from the error message that "require(\create_formiew.php)" has a missing "\v", whereas I wrote "\create_form\view.php".
What's the problem?
Either escape the backslashes in double quotes, or just use forward slashes. Forward slashes work on Windows too.
Also you probably don't want to use an absolute path.
require("./create_form/view.php");
Or make it relative to the document root with:
require("$_SERVER[DOCUMENT_ROOT]/create_form/view.php");
// Note: Use without key quotes only in double quoted string context!
You can use 'DIRECTORY_SEPARATOR' to avoid errors with system specific file paths.
\v is the escape sequence for a vertical tab.
Either use single quotes in your strings or escape the backslashes (\\) to avoid ambiguities.
try using
require("/create_form/view.php");
Try using double (\) backslashes between directorys:
require("\create_form\view.php");

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.

Categories