I accidentally have created some folders on server with a function and instead of using "/" I used this "\". Now the folders are like this : "folder\MartonMihaiBusinessCard" .
I cannot delete them anymore. What can I do about this ?
Because you used PHP to create them, they're likely owned by some funky user like "nobody" or "apache". So, you'll have to use PHP to delete them, too.
This doesn't work out of the box with filenames containing backslashes, as you've found. This is because the backslash is treated as an "escape" character in PHP. Something like "my\new\file" will actually contain a linebreak and a carriage feed.
However, if you escape the backslash-- with another backslash!-- you'll be able to point PHP at the file.
rmdir("/path/to/the/folder\\MartonMihaiBusinessCard");
(or unlink() if it's actually a file, I wasn't clear on that) should do what you seek.
Related
No matter how hard I try, mkvextract doesn't work properly. I'm aware that there is a problem with the file path, but I tried hundreds of times, but I still could not succeed. How can I run this correctly?
shell_exec("mkvextract tracks /home/movies/R-12/X-1 ÇĞŞZ.mkv");
or
$filename = "/home/movies/R-12/X-1 ÇĞŞZ.mkv"
echo shell_exec("mkvextract tracks \"$filename\"");
I am aware that you cannot access the file path due to special characters
There may be several issues:
A file read permision issue: the file exists, but PHP (and the mkvextract it runs) don't have the permission to open it. In the rest of my answer I assume this is not happening, because you haven't added any error message containg the word permission or access to your question.
A shell argument escaping issue: correcly passing a command argument containing whitespace and/or shell metacharacters (e.g. ", \, $). I address this with escapeshellarg below.
A filename encoding issue: correctly specifying non-ASCII characters in filenames. I address this with mb_convert_encoding below.
For testing purposes, make a copy of the input file to /home/movies/t.mkv, and then try echo shell_exec("mkvextract tracks /home/movies/t.mkv").
If that works, then rename the copy to /home/movies/t t.mkv, and then try echo shell_exec("mkvextract tracks " . escapeshellarg("/home/movies/t t.mkv")). Without the escapeshellarg call, it wouldn't work, because the filename contains a space.
If that works, then the problem is with non-ASCII characters in the filename. To investigate it further, examine the output of var_dump(scandir("/home/movies/R-12")), and see how the letters with accents appear there. Pass it the same way to shell_exec. Don't forget about escapeshellarg.
If that works, use encoding conversion (with mb_convert_encoding) for the remaining filenames. You may want to ask a separate question about that, specifying the output of var_dump(scandir("/home/movies/R-12")) and var_dump("X-1 ÇĞŞZ.mkv") in your question.
$filename = "/home/movies/R-12/X-1 ÇĞŞZ.mkv"
echo shell_exec("sudo mkvextract tracks \"$filename\"");
I guess the whole problem was not adding sudo per :)
In PHP, the function urlencode() allows to encode a string as a url fragment, encoding all characters that might have a special meaning in a url. The encoding is reversible with urldecode().
I am looking for something similar for filesystem paths.
urlencode() might already work here, but it seems more aggressive than needed. E.g. there is no reason to escape "()" parentheses. On the other hand, a dot, especially double dot, can have special meaning in a filesystem path (one level up), so perhaps this would need to be encoded.
Is there anything available in PHP, or what would be the closest to do manually?
Is urlencode() safe for this purpose, or do we need to be careful indeed about the dot? Perhaps it is sufficient to exclude the dot as a first character, but allow it in the middle of a path.
My current use case would be Linux filesystem paths, but in the end I might want to put this in a software package that should be usable on different OSes.
Like urlencode(), I am looking for something that is reversible. So, not just sanitization, but actually encoding so it can be decoded.
Ideally, encoded paths should still be readable by humans. So, most characters that are allowed in a filesystem paths should preferably remain unchanged.
Use case
The question above should already be complete without this use case, but some people prefer to see one.
I have a scenario where I want to export a huge amount of data to distinct php files, and interpret the file names / paths as array keys when reading / writing the data.
E.g.
foreach ($addresses as $name => $address) {
file_put_contents($dir . '/' . FILEPATH_ENCODE($name), '<?php return ' . var_export($address, TRUE);');
}
The benefit of storing this in separate files is to track them easily using git, with minimal potential for git conflicts (compared to lumping them all in one file). Encoding the array key in the file name / path means I no longer need to store it in the file itself, thus reducing redundancy.
Extended example: Return closures instead of values.
foreach ($addresses as $name => $closureBodyPhp) {
file_put_contents($dir . '/' . FILEPATH_ENCODE($name), '<?php return function () {' . $closureBodyPhp . '};');
}
The data or closures I want to export are provided by an existing system, it is not up to me.
If you really want to know: I want to replace Drupal 7 features, that is, export existing features-generated php in a centralized directory instead of in separate feature modules.
But please, don't get too distracted with the use case.
I know there're plenty of topics regarding escaping characters but I just can't find the solution for my problem.
It's very easy. This is string I have:
$path = "C:\Users\Me\Desktop\14409238.jpg";
Howver, no matter how many escaping techniques I use, I can't manage to display the correct path without destroying it. In all cases the \14 will be replaced with
C:\Users\Me\Desktopd09238.jpg
How do I solve this?
Don't use backslashes in PHP for windows paths. It's smart enough to convert for you:
$path = "c:/users/me/desktop/...";
Using backslashes runs into the exact problem you have - backslashing certain characters turns them into metacharacters, not regular characters.
try to change, the Physical path to access the image, stored on Desktop can be written as,
$path = "C:\Users\Me\Desktop\14409238.jpg";
to
$path = "C:\\Users\\Me\\Desktop\\14409238.jpg";
Avoid the situation entirely, PHP under Windows allows you to submit paths with the backslash
c://Users/Me/Desktop/file.jpg
This also avoids interoperability headaches when a script must run within .nix and Windows.
I have PHP script from here http://css-tricks.com/snippets/php/display-styled-directory-contents/ , everything works fine except when I have file or folder with #,",' sign it can't be display in web browsers. So I must rename that file or folder to get it works. How to fix this?
You have to escape those characters, with a backslash \. I don't know why you would have folders with special characters in the name anyways.
For example if you have a folder named #Folder you would need to escape the # but doing \#Folder.
I use a script to get an image from another server and store it in the db, the problem is that when the url has a space in it, the function grabs nothing.
I tried to encode the url and to simply replace all spaces with %20 but with no success.
I'm running out of options, if any of you could give me some ideas would be great!
Thanks!
$thumb=imagecreatefromjpeg(http://www.dummysite.ca/imageone.jpg); //->WORKS
$thumb=imagecreatefromjpeg(http://www.dummysite.ca/image one.jpg); //->DOESN'T WORK
EDIT: more info: I'm running a CentOS machine, php 5.2.17
EDIT: found the answer, replacing spaces with %20 actually WORKS but I was foolish and only replace it before the imagecreatefromjpeg call, it turns out getimagesize needs it as well
So for those who will have a similar problem
replacing spaces with %20 actually WORKS but I was foolish and only replace it before the imagecreatefromjpeg call, it turns out getimagesize needs it as well
I would do everything in my power to keep spaces out of filenames. At whatever point the file enters your server it should be renamed to something with underscores. Personally For file uploads I rename every file to a combination of timestamp and the uploader's ip address. Grabbing from another server could use the same logic. If you need to save the original filename just save it as a text string associated with the DB entry.