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->fid;
$node->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->fid;
$node->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);
$image_url
. That is a very, very bad idea that will fail on many characters, not only on space. Seeurlencode
and related functions. Sanitizing what you put in your URL string is general programming issue, would fail the same way no matter what CMS are you using. Probably no matter what language you are using. – Mołot Jan 22 '15 at 9:54$image_url = urlencode('www.example.com/images/news/'.$image);
try$image_url = 'www.example.com/images/news/'.urlencode($image);
- I'm not promising it'll work, but I think it might. That's what I meant in the first place, anyway. – Mołot Jan 22 '15 at 12:17