php - file_get_contents - Downloading files with spaces in the filename not working

Related searches

I am trying to download files using file_get_contents() function. However if the location of the file is name.jpg, the function fails to download this.

But if the URL is given as, the same gets downloaded.

I tried rawurlencode() but this coverts all the characters in the URL and the download fails again.

Can someone please suggest a solution for this?

I think this will work for you:

function file_url($url){
  $parts = parse_url($url);
  $path_parts = array_map('rawurldecode', explode('/', $parts['path']));

    $parts['scheme'] . '://' .
    $parts['host'] .
    implode('/', array_map('rawurlencode', $path_parts))

echo file_url(" bof/some file.jpg") . "\n";
echo file_url("") . "\n";
echo file_url("") . "\n";



I'd probably use urldecode and urlencode for this as the output would be identical for each url. rawurlencode will preserve the + even when %20 is probably suitable for whatever url you're using.

php - Downloading files with spaces in the filename not working, I am trying to download files using file_get_contents() function. However if the location of the file is name.jpg, the function fails to � urlencode – php – file_get_contents – Downloading files with spaces in the filename not working Posted by: admin July 12, 2020 Leave a comment Questions:

As you have probably already figured out urlencode() should only be used on each portion of a URL that requires escaping.

From the docs for urlencode() just apply it to the image file name giving you the problem and leave the rest of the URL alone. From your example you can safely encode everything following the last "/" character

file_get_contents - Manual, file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource If you're opening a URI with special characters, such as spaces, you need to Attempting to seek on non-local files may work with small offsets, but this is note: this method does not allow file uploads. if you want to upload a file with your� This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE. file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by

Here is maybe a better solution. If for any reason you are using a relative url like:


Prior to php 5.4.7 this would not create the [scheme] array element which would throw off maček function. This method may be faster as well.

$url = '//';

preg_match('/(https?:\/\/|\/\/)([^\/]+)(.*)/ism', $url, $result);

$url = $result[1].$result[2].urlencode(urldecode($result[3])); 

How to retrieve files from url when they have whitespace into , jpg in url bar will display the image correctly. But system_retrieve_file() isnt' able to reach it. $path = system_retrieve_file($image_url� I've seen many download scripts written in PHP, from simple one-liners to dedicated classes. Yet, at least half of them share common errors; in many cases programmers simply copy the code from something that works, without even attempting to understand what it really does. What follows is not a complete working download script, but rather a set of issues you should be aware about and that will

Assuming only the file name has the problem, this is a better approach. only urlencode the last section ie. file name.

private function update_url($url)
    $parts = explode('/', $url);

    $new_file = urlencode(end($parts));
    $parts[key($parts)] = $new_file;

    return implode("/", $parts);

Download file from URL using PHP, There are many approaches to download a file from a URL some of file_get_contents($path, $include_path, $context, $start, $max_length). The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.

This should work

$file = 'some file name';

How to download a CSV file in PHP that is triggered through a URL , Loops � Solving Recurrences � Amortized Analysis � What does 'Space Complexity' mean ? Downloading a CSV file through a URL is very easy with PHP and we will discuss here how to file_get_contents($path, $include_path, $ context, $start, $max_length) else echo "Sorry, that's not a CSV file" ; ? Im having a problem getting the copy function to work on my website. I am copying video files from youtube to my server. If I choose a short video, about 3 mins or less, everything works fine. If I choose a longer video, the copying goes for a few seconds, but then I get a 500 Internal Server Err

As of PHP 5.6 the file(), file_get_contents(), and fopen() functions will return false if you are referencing a source URL that doesn't have a valid SSL certificate. Presumably, you will run into this a lot in your development environments this will drive you crazy.

Downloading Files with PHP. Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. If such kind of file is stored in a public accessible folder, you can just create a hyperlink pointing to that file, and whenever a user click on the link, browser will

Any processes reading from the file will continue to do so, any processes trying to open the file while you're writing to it will get the old file (because you'll be writing to a temp file), and there is no "intermediate" time between there being a file, and there not being a file (or there being half a file).

  • Don't you mean file_get_contents?
  • some+name.jpg should work. How does it fail to download? 404 error? 400 bad request? file_put_contents() is for SAVING data. If you provide a URL, you're effectively trying to do an http upload, which is most likely NOT what you want.
  • the issue he is having is rawurlencode will double encode a string if it is already encoded. However, simply decoding all strings before encoding them doesn't look like it will work either. He needs a function that will work for all URLs
  • As noted in the question, if $file = 'some%20file%20name', this will fail. He needs something that works for both.