PHP: imagecreatefromjpeg($url) doesn't work if $url contains spaces? - php

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.

Related

Argument error when running mkvextract with PHP

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 :)

What is the point of rawurldecode() and urldecode() when the browser apparently does it automatically?

I can't tell you how many hours of my life I've wasted on these kinds of idiotic errors.
I'm basically constructing a URL such as: https://example.com/?test=' . urlencode('meow+foo#gmail.com');
Then, I display it from the URL, like this: echo urldecode($_GET['test']);
And then it shows: meow foo#gmail.com.
Ugh.
If I instead fo this: echo $_GET['test'];
I get: meow+foo#gmail.com.
(Naturally, echoing a GET variable like that is insanity, so I would of course do htmlspecialchars around it in reality. But that's not the point I'm making here.)
So, since browsers (or something) is clearly making this "translation" or "decoding" automatically, doing it again messes it up by removing certain characters, in this case the "+" (plus). Which leads me to believe that I'm not supposed to use urldecode/rawurldecode at all.
But then why do they exist?
So when would one ever want to use them
I recently had a case where we added triggers to an S3 bucket which were being picked up by a Lambda function and sent via a HTTP request to an API endpoint.
If the path of the file on S3 was multiword, it would replace the space with a + at which point it would break our code because tecnically the path is incorrect.
Once you run it through urldecode it becomes a valid path because as per the docs:
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
That would be a valid use case for this function as no browser is involved. Just background processes/requests.

Load file name with spaces from FTP

I'm trying to load a xml file from an external ftp server. Sadly the filename contains a space between two words.
Homepage-Filename Statistics-170210.xml
I'm able to load the file with simplexml_load_file, if there is an underscore a dash. For example:
Homepage-Filename_Statistics-170210.xml
simplexml_load_file('ftp://username:password#ftp.domain.com/Homepage_Filename_Statistics-170210.xml');
But I'm not able to change the file name, so I have to load the file with spacing inside.
I have tried to replace the space with %20 or backslash / , but it isn't working either. For example:
Homepage_Filename%20Statistics-170210.xml
or
Homepage_Filename/ Statistics-170210.xml
Someone has an idea, how load something like this?
Thanks!
I can also reproduce this problem with the simplexml_load_file.
Interestingly the file_get_contents works:
$url = 'ftp://username:password#ftp.domain.com/Homepage_Filename/ Statistics-170210.xml';
simplexml_load_string(file_get_contents($url));

Escaping white space : %20 or + - smarty

I am using smarty as a template engine. I have to escape an image file path {$filepath|urlencode}, the problem is that the white space are converted into a '+', which prevent the image to be reached on the server : %20 would work, how to escape correctly my path ?
Edit : more precisely, I use the facebook share link
I use a facebook share as so and it doesn't display the image when shared :
``
The final code looks like for my specific usage :
<a href="http://www.facebook.com/dialog/feed?app_id=...&link=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18&picture=http%3A%2F%2Fmysite.org%2Ffiles%2Fcampaign%2Fimage%2Foriginals%2F18%2FSans+titre-3.jpg&name=Some text "Text d'Text", Text&description=Rejoignez%20la%20campagne%21&redirect_uri=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18"onclick="window.open(this.href);return false;">
on the same site, all the facebook share link works perfectly and the image displays well ! Reason why I thought it was the link of that specific image that is not working
escape is what you're searching for. Take a look at:
http://www.smarty.net/docsv2/en/language.modifier.escape.tpl
{$filepath|escape:"url"}
urlencode is used to encode (not escape!) a string to be used as a query part inside an URL passed as GET var: http://php.net/manual/en/function.urlencode.php
URL encoded space is either a plus sign or %20. They are equivalent, and are both interpreted as a space on the server.
If you see either in the URL, then the server will see a space.
You say that the plus sign is preventing the image from being loaded. This sounds like a deeper problem than simply using the wrong encoding. Possibly it's being double-encoded?
What is the actual URL being requested in the browser? Open the dev tools/Firebug, and look at the requests to find out. If the URL includes %2B then the plus sign is being double-encoded. This is the problem you need to solve.
The other solution, of course, is not to use spaces in filenames on the web. The only reason one would want spaces in filenames is for readability, but since the web requires spaces to be urlencoded, it removes that readability anyway. Take away the spaces, and the problem will go away by itself.

Filenames with spaces

I am running PHP locally (no webserver involved at all). I am having trouble accessing a file with spaces in the path.
My bare bones case is
$indexFile = "file:///Users/username/Documents/My Folder/test.txt";
echo file_exists($indexFile);
or:
$indexFile = "file:///Users/username/Documents/My‰20Folder/test.txt";
echo file_exists($indexFile);
AFAICT this latter case is a well-formed file scheme URI. It's exactly what appears in the browser location field if I drag the file in there.
URI's without spaces don't have any problem. Unfortunately I am not at liberty to change "My Folder" to "MyFolder", and besides, I want to find a solution.
I tried using urlencode, rawurlencode, escapeshellarg, I've tried replacing %20 with a backslash-escaped space "My\ Folder" but none of this works. I've also hunted through google and stackoverflow, but while there are many suggstions, the question remains unanswered:
How to access an arbitrary file (on the local host) using a path which contains spaces?
This looks like a Mac OSX file system and so this should work:
$indexFile = "/Users/username/Documents/My Folder/test.txt"
echo file_exists("${indexFile}");
Remove the first file:// from the first code snippet. It'll work.
$indexFile = "file:///Users/username/Documents/My Folder/test.txt";
echo file_exists(str_replace("file://", "", $indexFile));

Categories