Hi !

I would like to know how exactly should I set my .csv file to be able to import images into my contents.
Here's the problem I'm encountering :
I would like to import hundreds of contents into a custom content type. This content type has an image field, with a subdirectory parameter, eg. "mysubdirectory" where the files should be stored.
I have hundreds of images located on the server, say in "sites/default/files/to_import/" directory.
My .csv file has an "image" column containing relative paths to the images, as "my_image.jpg".

But when I import this.csv file, feeds module throws an error :

Invalid enclosure my_image.jpg

And my content is not created.

Then I try to put in the .csv file an absolute path for the image url, eg. "/var/www/sites/default/files/to_import/my_image.jpg".
In this case, the import goes well, the image is created in the "files" table, etc... BUT... the image is stored in "/tmp/my_image.jpg". It should be instead "sites/default/files/mysubdirectory/my_image.jpg" (the subdirectory I've set in content type creation phase).

So, to resume, I've got 2 questions :

  • is it normal to have to put an absolute path to an image to be imported with the feeds module ?
  • why my image stays in the /tmp directory instead of its final location ?

Many thanks for anyone giving me more explanations on how to use correctly this module.

CommentFileSizeAuthor
#16 add_prefix.zip503 byteskasalla
#6 filefield-localfile-1041030-6.patch765 bytescopini
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Notachel’s picture

Version: 6.x-1.0-beta10 » 6.x-1.x-dev

I have more or less the same type of question but the result is not the same .

But first, could you please explain ( an example will be great ) the writing of "The URL for the CCK (field name) field of the node." in "the Mapping for Node processor" ( Legend ).

In my case, the file field use a tokenised path : [ogname]/[yyyy]/[mm]/docs and the "file to be imported from your local system." for simple testing contains only 2 lines :
Fichiers ( = header )
/sites/(name-of-the-site)/files/feeds/(name-of-the-file-with-extension-uploaded-by-ftp-waiting-in-feeds-directory)

For info, i use the last FileField Paths 6.x-2.x-dev Posted by Deciphered on October 11, 2010 at 6:11am downloaded at http://drupal.org/node/937890

The Feeds mapping has been defined with 2 lines and Target are Fichiers and URL ( URL because "Make sure that at least one definition has a Unique target.") but the file to be imported contains only the file field.
"Import CSV files with one or more of these columns: node-URL, Fichiers. " is not so clear : is the URL needed in the imported file or not ?

the result is :
* ( content type name ) (title of the node) has been created.
* There is no new content.
the node is created but without the file populated in the file field !

Because it's my goal to use the Feeds module to "attach" dozen of files to new nodes, it's rather annoying...

Thanks for your help

Nota

kalis1’s picture

Component: Documentation » Miscellaneous

FYI, I've found a temporary workaround commenting these 2 tests on the file existence and set the $file variable an absolute path, in plugins/FeedsParser.inc, getFile() function :

  public function getFile() {
    if(empty($this->file) && $this->getValue()) {
      // Check if this enclosure contains a local file.
      if (!parse_url($this->getValue(), PHP_URL_SCHEME)) {
//        if (file_check_location($this->getValue(), file_directory_path()) || file_check_location($this->getValue(), file_directory_temp())) {
//          if (file_exists($this->getValue())) {
//            $this->file = $this->getValue();
            $this->file = $_SERVER['DOCUMENT_ROOT'].'/'.file_directory_path().'/'.$this->getValue();
            return $this->file;
//      }
//        }
//        throw new Exception(t('Invalid enclosure %enclosure', array('%enclosure' => $this->getValue())));
      }
micheleannj’s picture

Subscribing.

Same issue -- I want the imported images to go to a specific folder. In fact, they were all already there, so they didn't need to be copied (I can't find any documentation that explained the behavior of image imports so I'd already put them in the files direction).

I'll open new issues for "Specify a destination subfolder for images" and "make image copying optional" but I thought I'd see if anyone chimes in here with a link to something I missed first...

Thanks
m

sonar_un’s picture

Subscribing

I have been looking for the same functionality. I don't need the images to be copied as they are already on the server, I just need to supply the proper relative path to the image itself. Also, I have noticed that there is no way to bring in the title or alt text of an image either, which would be nice for SEO purposes.

mokko’s picture

Same problem here. I tried with absolute path, but haven't found out yet if they end up in tmp. EDIT: No they don't. Also I wonder: which absolute path. I used /home/mokko/bla/1.jpg now. Next I will try something in drupal root. Also I will try to turn on the DEBUG described in README.txt

Hm. Can't get feeds_debug to work, but this is not the issue here

I have a csv with 50 lines, one column has a filename which I want to become imagefield image. I import and end up with 4 nodes which are created. None of them has an image. Why are those 4 created?

Have a dir now like this
sites/default/files/mg_import/ (similar to ex. above)

where the images lay.

They should be readable to my php, so no permission problem.

I still get Invalid enclosure file/mg_import/0143.tif-medium.jpg warnings.

copini’s picture

We were importing a lot of nodes with attachments (filefields) and ran into similar problems. We stored the files locally and imported from an XML source (via feeds_xpathparser).
The frustrating part was there were absolutely no errors (see also #1095826: Filefield mapper: Better error handling to allow mapping to continue). Nodes were created just fine, but with no attachments.

We were finally able to import the files with a few extra steps:

  1. We changed the file paths to the absolute local paths (/tmp/...) using feeds_tamper.
  2. filefield.inc checks the url with valid_url(), which doesn't always work for local files (especially when the filename contains spaces). We fixed this by adding an is_file() check (see attached patch).
  3. Some of our filenames had special characters, not encoded in UTF-8. Our source XML was encoded in UTF-8 which caused is_file() to fail for filenames with special characters (ë etc.). We fixed this by converting the filename encoding using the command-line utility "convmv".

The files are still copied (not moved) from their temporary location to sites/default/files, but that's okay with us.

johnv’s picture

subscribe.
[Edit] I want to import files/images into multi-value fields, using a csv-file on D7.
I solved the multi-value issue using Feeds_tamper module.
I posted the files tot /tmp/{filename}, Feeds copies them to the correct location.

kapayne’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta10

I'm having the original problem - feeds isn't attaching files that exist on the server to nodes created by parsing a CSV file. I get the error message "Invalid enclosure myfile.xml" - I have tried every configuration of absolute and relative paths that I can think of - this happens when I try to attach either an image to an image field and a free standing xml to a file field specifying the location with file field sources. I don't have any hinky file names or character strings that might need to be tweaked. Any ideas?

kapayne’s picture

For what it's worth - after installing the filefield error handling patch (http://drupal.org/node/1095826#comment-4258962) I got the error message "An HTTP error 500 occurred. //index.php?q=batch&id=327&op=do" - vague I know. Full error message:
warning: require_once(./sites/all/modules/feeds/mappers/filefield.inc) [function.require-once]: failed to open stream: Permission denied in \sites\all\modules\feeds\plugins\FeedsPlugin.inc on line 94.

kapayne’s picture

Is anyone else getting 'invalid enclosures' when trying to import nodes with the csv parser? I can't attach images or stand alone text files.

johnv’s picture

@kapayne, I still cannot import images when the path has spaces/dashes (in D7). This is how my csv-file looks like (with 2 values in the column, to be exploded with FeedsTamper):
start;KEY;Images
1;2351;http://www.example.nl:8082/files/NL/2351/IMG_1164.jpg,http://www.example...
I also make sure:
- to have a dummy start column
- no spaces in the column titles

kapayne’s picture

worked like a charm thanks johnv!

13rac1’s picture

Status: Active » Closed (works as designed)

Per kapayne, this looks closed.

@johnv I made an issue/patch which corrects the problem with spaces you are having: #1256952: Feeds file.inc file import doesn't support spaces in URLs

johnv’s picture

@eosrei, thanks, I'll test it.

Problue Solutions’s picture

Version: 6.x-1.0-beta10 » 6.x-1.0-beta11
Status: Closed (works as designed) » Active

I am having almost the same issue as the OP, however I'm a bit confused as to what feeds uses as an 'absolute URL'.

Instead of:

Invalid enclosure my_image.jpg

I get:

Cannot write to /tmp/my_image.jpg

In the orginal post it says:

Then I try to put in the .csv file an absolute path for the image url, eg. "/var/www/sites/default/files/to_import/my_image.jpg". In this case, the import goes well."

I did the same thing on my own server, using the full root server path as above (starting /var/www/) and it worked. However when I use the absolute path "http://www.mywebsite.com/sites/default/files/to_import/my_image.jpg" it does not work.

My problem is that the server i tested this on is my own server so I was able to use "/var/www/sites/default/files/to_import/my_image.jpg", but on the shared server my client is on I don't know this path/directory structure right back to the server root. I've tried every variation I can find and nothing works.

Why does the full absolute URL not work?

i.e. "http://www.mywebsite.com/sites/default/files/to_import/my_image.jpg"

kasalla’s picture

FileSize
503 bytes

My usecase:
Import csv-file
One column contains the filename, e.g. picture_1.jpg, Title is just "PICTURE"
Images which are supposed to be used as cck imagefields are located in sites/default/files/pics_for_feeds_import

I wrote a little plugin for feeds tamper module which simply adds a prefix to the field.
What worked for me:

1. Create a folder "pics_for_feeds_import" in "sites/default/files" and copy/upload your images here.
2. Install module http://http://drupal.org/project/feeds_tamper
3. Copy file "add_prefix.inc" into feeds_tamper/plugins directory
4. Map the CSV-Value "PICTURE" (or whatever) to the imagefield as usual
5. Add a plugin to that field Text->Add Prefix
6. Enter "sites/default/files/pics_for_feeds_import/".
7. Run the import as usual.

The node should be created with the image. Imagecache works as expected aswell.
This plugin not only works with imagefields, but with every text based value.

goodeit’s picture

If you're trying to map to a file that already exists, try this:
public://images/myimage.jpg

This assumes that myimage.jpg is in /sites/default/files/images, so you'll have to change the path if you put it somewhere else. But, since it's relative to /sites/default/files, you don't need to know the entire server directory hierarchy.

I'm not sure what happens if you don't have the image in the location that the image field wants it to be in.

csedax90’s picture

make sure if your files folders, all files and all directories inside it have all privileges (i set it to 777)

hanoii’s picture

I've been struggling with this as well, here's what I found:

First, what @goodeit said applies to 7.x as public:// is a file scheme added in 7.x.

- Make sure that the configured destination path is writable (has the proper permission).
- Make sure that in the CSV, you specify the full path to the file

This should work

kpv’s picture

if you compose your CSV manually don't include spaces before the link or you risk to loose from 4 to 5 hours :)
i.e.

wrong
title;path
Some new title; http://example.com/sites/default/files/imagecache/product_full/images/10...

right
title;path
Some new title;http://example.com/sites/default/files/imagecache/product_full/images/10...

maybe we should use trim for links
(D7)

asb’s picture

Hi,

has anyone found a way to import CSV values into "image attach" (from the legacy 'image' module for D6), or is this simply impossible with 'feeds'?

Thanks!

achton’s picture

I simply uploaded the files to the server before import, and put the path to the files relative to Drupal root in my CSV. Made sure to save it as UTF-8. The CSV fields with paths looked like this: "sites/sub.example.com/files/products/images/96.jpg".

This was on a multisite D6-install, using Feeds 1.0-beta11 and ImageField 3.10 for CCK. ImageCache also just works using this method.

But this should really be in the Feeds documentation. I searched for quite some time before finding this thread, and eventually achieving this in a way different from everyone else here it seems. O_o

leanderl’s picture

anstosser! Thank you about 1 million times!! This is exactly what I wanted/needed and it works great (even with multivalue field after first exploding them). It gives me the flexibilty to instruct others to just add the filename and not worry about the path. Brilliant!

ClemensM’s picture

Version: 6.x-1.0-beta11 » 7.x-2.0-alpha5

goodeit, you gave me the great hint, I was looking for. Thanks!

Howto:
- Just upload your images at your server manually.
- Then optionally use Tamper for changing the url name to its whole. Create a Tamper Rewrite plugin with public://photos/optional_category/[localpath] where localpath is the name of your csv field. (Beware! the token has to be written in lower case!!)
- Now in your csv file you only have to write the image file name in the localpath column.

Voilà: no problem with any uploading access stuff, without dublicated image files -> very easy to use and everything under control!

P.S. if the image doesn't exist there just will be a link to an empty not existing 0kb-image.

ClemensM’s picture

Well, but there is one point:
Is there an analogical way for keeping the images private??
Like private://... (doens't work) !?

richsky’s picture

@goodeit

Could not get the import to work with path like public://tmp/files/myfile.txt

What's work for me is to specify relative path such as: sites/mscpub/files/tmp/images/dummy.jpg

And the if the file is in the wysiwyg area change it to http://www.myprodsite.com/sites/mscpub/files/images/[tocken:if:any]/dumm... with feed tamper.

But I would love this solution works without the site name in the path to avoid mistake when I will move from dev to prod.

Sally Gr’s picture

Thanks to Kasalla for your work around with tamper feeds. The only drawback is that you have to have an image in the folder already or the import will fail. Otherwise, it works perfectly.

leanderl’s picture

Big thanks to Casalla!

With this small change to the add_prefix.inc (done by my collegue @Olleolleolle) it is possible to have fields with no data without interrupting the import:

function feeds_tamper_add_prefix_callback($source, $item_key, $element_key, &$field, $settings) {
    // Prefix any non-empty string
    if (strlen($field) > 0) {
      $field = $settings['mask'] . $field;
    }
}

You simply add an if-statement to check that the string is not empty.

markconroy’s picture

Small quirt I'm having with this:

1) If I put the path as absolute URL - http://www.example.com/image.jpg, it works as expected
2) If I am working on localhost and use a relative URL - sites/default/our-images/image.jpg OR public://our-images/image.jpg, it works as expected

HOWEVER

3) If I am developing the site on a live server without a domain name - so the client can see how things are progressing - for example, I have a VPS set up, added a domain name example.com, but not changed the nameservers/A records for example.com. I can access this domain by going to 123.abc.hostingco.com and have uploaded the relevant images to the server at sites/default/our-images/
3.1) If I use relative URLs here, the images don't appear.
3.2) If I use public://our-images here, the images don't appear
3.3) If I use 123.abc.hostingco.com/sites/default/files/our-images the images don't appear

It seems that unless using localhost, feeds wants a URL to work with. Am I right? Anyway around this - besides uploading the images to another "live" domain to then take them from there?

chriscalip’s picture

Also currently looking for this functionality. The ability to add a default url.

If csv has abc.jpg
What feeds would actually do is get said image from http://example/abc.jpg

nicl’s picture

I would suggest using the Feeds Tamper module and doing a rewrite on the field. This will allow you to add whatever prefix you like to the image name. So in your csv's you can just have (for example) abc.jpg

nicl’s picture

Ok, so I've been following this thread and wrestling with feeds import, and importing images in particular. Some things I've learnt:

- images will not be copied if they are already in the target location for the field.

E.g. I had a fabric image field which was set (in the content type) to be stored under [..]/files/fabrics . Feeds will simply use images (rather than attempt to copy them) provided they are stored in this folder. Note, they *cannot* be in subfolders of fabrics, only fabrics itself.

- prefacing images with public:// is a good way to point to your [..]/files folder.

A good solution is to use the Feeds Tamper module and rewrite image values to be prefixed with this. That way, you can easily change base paths later without changing CSVs if for any reason you need to (for example, to change to absolute paths).

- file.inc is where the magic happens

This is the Feeds module file (under the mappers directory) which handles file field behaviour for imports. So if you are stuck on how things are working/why they aren't working then take a look here - print, debug, or just look at the code.

kingandy’s picture

"- images will not be copied if they are already in the target location for the field"

I've experienced something that's somewhat the reverse of this - the images were being copied over successfully, but if they were already present in the files table they wouldn't be attached to the entity. This holds even if the file is renamed during the copy process, the system only checks the original filename.

Summit’s picture

Hi,
Is this still the case, so I can only import feed with images if I first wipe away all the images in the destination folder?
How about when I only want to update the feed and some items with images will be added.
Would it not be more sufficient to only add those images.

Is this possible somehow?
greetings, Martijn

Jim Bacon’s picture

A simple note of clarification. The csv file tells Feeds where to find the files. Feeds copies the files to the folder specified by your image field. i.e. To set the destination go to Structure>Content Types>YourContentType>Manage Fields>YourImageField. Edit the File Directory setting.

Tander1’s picture

One more interesting note that might not be obvious... if you are uploading multiple images per node, and you explode it with Tamper, you need to get the order right. You have to explode first, then do the add prefix... the other way around and you'll only get the first image...

MegaChriz’s picture

Status: Active » Fixed

It seems like this question has been answered.

To import images, Feeds needs to know the absolute path to these images. With Feeds Tamper you can correct image paths, if needed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.