0

I have a PHP file that will be accepting several URL parameters, one of which is a local file path (c:\users\etc..). Whenever this parameter is read in, PHP makes it into double backslashes (c:\users\etc..) which my upload code will not accept as a valid path. What can I do here?

Thanks

-Jesse

2
  • 1
    At which point does it make them into double backslashes and at which point is the data rejected? Please show some code Commented Jun 15, 2011 at 13:21
  • I guess this is just basic escaping of characters caused by addslashes() or mysql_real_escape_string() functions (or any similar). Try stripslashes() and see if they are being removed. Commented Jun 15, 2011 at 13:22

3 Answers 3

2

First of all, avoid accepting paths in query string for security reasons. You may want to set include path in php.ini file instead.

You can replace double backslashes with single ones like this:

$myvalue = str_replace('\\\\', '\\', $original_path);

You specify \ twice to actually escape it while replacing.

Working Example

Sign up to request clarification or add additional context in comments.

3 Comments

Yea, I tried doing this. But as you see, this statement is not properly escaped and is therefor not a valid statement. Check out the code highlighting on the code you posted.
@jomille: Fixed that before seeing your comment :)
as the file path is passed via URL ($_GET), the f*cking gpc_quotes are involved. @jomille, check the output of var_dump(get_magic_quotes_gpc()). If it's 1, then you should stripslashes() on anything that comes via $_GET, $_POST, $_COOKIE or $_REQUEST. If it's 0, then it's totally another issue so discard my comment.
1

This sounds like a typical Magic Quotes-problem. You can disable them in different ways. As of PHP 5.3 they are deprecated und will be removed in PHP 6.

Comments

1

you can do

$newpath = str_replace("\\\\","\\",$oldpath);

or similary

$newpath = stripslashes($olpath);

note that the second one works depending on the magin_quotes setting in your php.ini

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.