How to retrieve files from url when they have whitespace into filename? I need to import a lot of images/pdf from urls, and then save them to a file/image field into nodes. 

To achieve that, I am using this script (example with images): 

if($image!='\N'){   

    $image_url = 'http://www.example.com/images/news/'.$image;
    $managed = TRUE; // Whether or not to create a Drupal file record
    $path = system_retrieve_file($image_url, 'public://news/', $managed, $replace = FILE_EXISTS_REPLACE);
    $fid = $path-&gt;fid;
    $node-&gt;field_image['und'][0]['fid'] =$fid;

}


This is working so well in almost every case. Only files containing whitespaces in filename are not retrived as expected. Example:

2009 juin texte TVA vote final.pdf
Geoffrey Schinder Award.jpg


/**** EDIT ****/

Regarding Molot comment I changed my code including urlencode(), this way:

if($image!='\N'){   
    $image_url = urlencode('www.example.com/images/news/'.$image);
    $managed = TRUE; // Whether or not to create a Drupal file record
    $path = system_retrieve_file($image_url, 'public://news/', $managed, $replace = FILE_EXISTS_REPLACE);
    $fid = $path-&gt;fid;
    $node-&gt;field_image['und'][0]['fid'] =$fid;
}


But it's even worse, I can not retrieve any file this way. Here is my filename after urlencode():

http%3A%2F%2Fwww.example.com%2Fimages%2Fnews%2F2009+juin+texte+TVA+vote+final.pdf
http%3A%2F%2Fwww.example.com%2Fimages%2Fnews%2FGeoffrey+Schinder+Award.jpg


And before:

http://www.example.com/images/news/2009 juin texte TVA vote final.pdf
http://www.example.com/images/news/Geoffrey Schinder Award.jpg


/**** EDIT 2 ****/

Here is the kind of errors I am getting when file has white spaces:

HTTP error 404 occurred when trying to fetch http://www.example.com/images/photos/Geoffrey Schinder Award.jpg


This is so strange because I can reach all these images from browser, for example typing http://www.example.com/images/photos/Geoffrey Schinder Award.jpg in url bar will display the image correctly. But system_retrieve_file() isnt' able to reach it.

$path = system_retrieve_file($image_url, 'public://news/', $managed, $replace = FILE_EXISTS_REPLACE);
I managed to solve the issue by adding this command:

$image= str_replace(' ', '%20', $image);


I juste replaced all whitespaces with html whitespace characters..