1

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);
  • I voted it as off-topic because it is more general issue. You are putting unsanitized data in URL, precisely in $image_url. That is a very, very bad idea that will fail on many characters, not only on space. See urlencode 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
  • About the off-topic point. I checked documentation and web. I cannot find any topic about this issue. It's not the first time I use system_retrieve_file(). Usually it works like a charm. Anyway thank you for explaining your down vote. – 118218 Jan 22 '15 at 10:08
  • OK, now it isn't off-topic. But instead of $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
2

I managed to solve the issue by adding this command:

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

I juste replaced all whitespaces with html whitespace characters..

0

First I recommend using https://www.drupal.org/project/transliteration in drupal, so that all imported files will get lower letters and "_" instead of blancs.

For similar file imports from url to entity fields I already used feeds with feeds tamper successfully. As long as your url fit (you can see them in browser) that would be no problem. Hope I understood your issue right and this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.