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()?



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.

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.