Domanda

I have to migrate some content and everything is fine except the images.

I'm getting the images using file_get_contents() and save the file in the drupal /files folder using the following code:

    $image = file_get_contents('http://www.someurl.com/xx/util/yy.jpg'); 
    $file = file_save_data($image, 'yy.jpg',FILE_EXISTS_REPLACE); 
    print $file; // returns the path to the file ( /files/yy.jpg )

So the image is uploaded in the /files folder.

What I'm not able to understand is, how can I add the image to the files table so that I can be able later to use the file object?

I was looking through the files.inc functions for Drupal 6 but I wasn't able to find anything that could help. Should I insert the data manually using db_query()?

Thanks.

È stato utile?

Soluzione

It's just done manually, e.g.:

$file = new stdClass();
$file->filename = 'foo.bar';
$file->filepath = $file;
$file->filemime = file_get_mimetype($file->filename);
$file->uid = $user->uid;
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
drupal_write_record('files', $file);

See file_save_upload() for a much broader example, including validation.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange
scroll top