0

In my particular situation I am trying to use Drupal's API to import a bunch of files from my computer into a local Drupal installation. Seems like a smart idea. So I have the following function:

  function create_picture_file($filepath, $public_place) {
    global $pic_basepath;
    $filepath = "$pic_basepath/$filepath";
    $file = new stdClass();
    $file->filename = basename($filepath);
    $file->filepath = $filepath;
    $file->filemime = file_get_mimetype($filepath);
    $file->filesize = filesize($filepath);
    $file->uid = 1;
    $file->status = FILE_STATUS_PERMANENT;
    $file->uri = $filepath;
    $file->display = 1;
    $file->description = '';
    print_r($file);
    $file = file_copy($file,"public://$public_place",FILE_EXISTS_REPLACE);
    print_r($file);
    return $file;
  }

You'll notice the two print_r statements towards the bottom. The first one prints the following:

stdClass Object
(
    [filename] => Arctic_HE0302_Central_bathy.jpg
    [filepath] => /home/lpeabody/unclos_data/pics/Arctic_HE0302_Central_bathy.jpg
    [filemime] => image/jpeg
    [filesize] => 516130
    [uid] => 1
    [status] => 1
    [uri] => /home/lpeabody/unclos_data/pics/Arctic_HE0302_Central_bathy.jpg
    [display] => 1
    [description] => 
)

Which looks good. The second one, however, print's absolutely nothing. Any pointers or tips as to what I'm doing wrong maybe?

UPDATE So the public file directory to which this image is being saved in is region_images. When this directory exists, nothing get's printed by the second print_r statement. When I remove the directory and all of it's associated style directories, the second print_r statement prints. However, it prints the following:

stdClass Object
(
    [filename] => region_images
    [filepath] => /home/lpeabody/unclos_data/pics/Arctic_HE0302_Central_bathy.jpg
    [filemime] => image/jpeg
    [filesize] => 516130
    [uid] => 1
    [status] => 1
    [uri] => public://region_images
    [display] => 1
    [description] => 
    [fid] => 2595
    [timestamp] => 1331828075
)

Which is wrong, as [filename] should be Arctic_HE0302_Central_bathy.jpg. Hopefully this can help shed some light on the situation.

  • It might be a simple folder permissions problem...when you manually create the region_images folder have you made sure the web server user has write permissions to it? – Clive Mar 15 '12 at 17:56
  • Ah, I did not specify the origin of the process. This is a custom PHP script I wrote and I bootstrap the Drupal environment from it. I'm using it as a way to learn the API. The permission weren't an issue as I was running the process and I have write permissions on the entire site installation. I solved the problem by using Oswald's solution. – Lester Peabody Mar 15 '12 at 19:48
1

Use "public://$public_place/$file->filename" as $destination parameter for file_copy().

| improve this answer | |
  • Very, very strange. It makes sense, but I've done similar file copies in the past successfully but without specifying the filename in the uri wrapper. I wonder if it's because of the way Drupal handles images (in that multiple styles of images can exist in a multitude of different directories). – Lester Peabody Mar 15 '12 at 19:49

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.