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]).
Related
How to access files with a $-sign in a network share?
\\server\data$\fileshere
Tried escaping the backslashes, but my guess is that it's the dollarsign that creates problems?
EDIT:
Okay - here goes. On the server there is this file :
\\server\data$\filetest.txt';
If I want to check for a wildcard match like 'file*' my code looks this this :
$filecheck = '\\server\data$\file*';
## (also tried escaping \ : $filecheck = '\\\\server\\data$\\file*';
$check = glob($filecheck);
It does not match.
You have not added enough backslashes for the UNC part, also its safest to escape the escape char so always use \\ for one backslash.
Use
$filecheck = '\\\\server\data$\file*';
It also works for me using
$filecheck = '\\\\servers\\data$\\file*';
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\\');
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");
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.
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.