unlink() doesn't work with double backslashes - php

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

Related

unlink() Function invalid argument

I have a file with directory: PDF\9783790820874-c1.pdf
I would like to delete this file with unlink() funciton. But it seems like not working if I set the directory into a variable and unlink it.
For example:
$FileToDelete = "PDF\9783790820874-c1.pdf";
unlink($FileToDelete);
The code is logic isn't it? but why when I execute it, it show me error message:
Warning: unlink(PDF\9783790820874-c1.pdf ): Invalid argument on line 36
I have to save file directory into a variable to work well with my program, is there any way to solve it?
Your backslash is being interpreted as an Escape Sequence.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Either change it to a forward slash (which does work for paths on Windows):
$FileToDelete = "PDF/9783790820874-c1.pdf";
Or use single quotes:
$FileToDelete = 'PDF\9783790820874-c1.pdf';
Or just escape the backslash:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
I presume you are on Windows based on the backslash path separator.
I think the problem is due to escaping backslash causing problems, try:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
unlink($FileToDelete);
Hopefully that will fix it for you.
Remember that '' and "" behave differently in relation to escaping within the string. So while 'a\b.pdf' might work "a\b.pdf" could mean something completely different (where a and b are [some string]).

Removing Characters from a string with PHP

I would like to know if there is any way to just remove backslashes '\' from a string with PHP? For some reason, when I use mysqli_real_escape_string() the output text has a '\' before any apostrophes. I know I could you use a function to strip the slashes but I want to keep all forward slashes '/'.
Btw, I've already made sure magic quotes is turned off. The problem still persists.
Thanks
There is a function for that and it is surprisingly called stripslashes
Here it goes: http://php.net/manual/en/function.stripslashes.php
It will keep your forward slashes if that's what you're worrying about
QUERY:-remove backslashes '\' from a string but I want to keep all forward slashes '/'.
After hard working and googled if find result of your query:-
$path="uploads\\folder_name\\ankur/like you"; /////string
echo str_replace("\\"," ",$path);
output:-uploads folder_name ankur/like you

PHP can't read filename which has special character !

i have problem with file() in php. The function can't read a file start with ! or $ like !textfile.txt or $textfile.txt, i try with fopen() and it happen as a same way. I have no idea how to solve this. I hope someone can help
Appreciate any help.
The filename "$textfile.txt" will not work as expected because variable interpolation happens in double quotes as a result value of variable $textfile will be appended with .txt and the result will be used as filename. If $textfile is undefined (which mostly is the case), .txt will be used as the filename.
To fix this use single quotes around the filename as '$textfile.txt' or if you have to use double quotes, escape the $ as: "\$textfile.txt"
But I see no problem with "!textfile.txt"
echo file_get_contents("\$test.txt");
Works.
You need to escape special characters or use single quotes.

How to call a function using multiple lines in php?

I want to do something like this:
$db->store($arg1,$arg2,$arg3,$arg4,$arg5, \
$arg6,$arg7,$arg8,$arg9,$arg10, \
$arg11,$arg12,$arg13,$arg14,$arg15)
But that doesn't work, how would I go about doing this??
Just remove the slashes. PHP ignores EOLs (end-of-line).
$db->store($arg1,$arg2,$arg3,$arg4,$arg5,
$arg6,$arg7,$arg8,$arg9,$arg10,
$arg11,$arg12,$arg13,$arg14,$arg15)
;
You do not need the \, since PHP ignores all whitespace characters in your source code (except for the ones in strings of course). Just leave it out.
You don't need the slashes, or in fact, anything at all.
You do need to end with a semi-colon though, which you've missed out.

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

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

Categories