Jump to content

Batch / Bulk Uploading of Images -- Files Not Moved to Assets Folder


Recommended Posts

Posted

I'm trying to migrate a ton of images to galleries on a website I'm working on, and everything works fine except the image files themselves are not moved to the proper Assets/Files folder. Anybody know what the problem might be?

foreach ($gallery_images as $gallery_name => $gallery) {
    $p = new Page();
    $p->parent = $wire->pages->get(1085); // 1085 => /photo-gallery/
    $p->template = 'gallery'; 
    $p->title = $gallery_name;
    echo "Set title to $gallery_name.";
    $p->setOutputFormatting(false);
    $p->save(); //save page in preparation for adding images (from Ryan)
    echo "Saved.\n";
    foreach ($gallery as $filename => $description) {
        $p->main_image->add($filename);
        echo "\tAdded image $filename\n";
        $p->main_image->last()->description = $description;
        echo "\t\tAdded description: $description";
        $p->save();
        echo "\t\tSaved.\n";
    }
    $p->save();
    echo "\tSaved again for good measure...not sure this is necessary\n";
}
 
Posted
    foreach ($gallery as $filename => $description) {
        $p->main_image->add($filename);
        echo "\tAdded image $filename\n";
        $p->main_image->last()->description = $description;
        echo "\t\tAdded description: $description";
        $p->save('main_image');                              // change this line and try out please
        echo "\t\tSaved.\n";
    }

 Hi Marc, please try it like shown above, this is working for me.

  • Like 1
Posted

Hi Horst,

I changed the "save" line and I'm still having the same problem. Here is the current code:

foreach ($gallery_images as $gallery_name => $gallery) {
    $p = new Page(); // create new page object
    $p->parent = $wire->pages->get(1085); // 1085 => /photo-gallery/
    $p->template = 'gallery'; // set template
    $p->title = $gallery_name; // set page title (not neccessary but recommended)
    echo "Set title to $gallery_name.";
    $p->setOutputFormatting(false);
    $p->save(); //save page in preparation for adding images (from Ryan)
    echo "Saved.\n";
    foreach ($gallery as $filename => $description) {
        $p->main_image->add($filename);
        echo "\tAdded image $filename\n";
        $p->main_image->last()->description = $description;
        echo "\t\tAdded description: $description";
        $p->save('main_image');
        echo "\t\tSaved.\n";
    }
    $p->save();
    echo "\tSaved again for good measure.\n";
}

I'm not sure if it matters, but I am running the script from within (site root)/import-images/script.php -- and that's where all the images are located, too.

Posted

Uhhm, - what is the value of $filename?  Fullpath or Partialpath or basename?

$p->main_image->add($filename);
echo "\tAdded image $filename\n";

Posted

you need to use the fullpath. As you have said the images are in the same directory like the importer-script you can use:

    foreach ($gallery as $filename => $description) {
        $filename = dirname(__FILE__) . '/' . $filename;
        $p->main_image->add($filename);
        echo "\tAdded image $filename\n";

  • Like 1
Posted

That did the trick! Thanks Horst. Out of curiosity, I'm wondering why the full path is required. Is this the thing where PHP is looking in special environment paths if no path is specified (and if so, would ./filename have worked)?

Marc

Posted

I'm not sure what is involved in the importer process when getting only a basename. Does it check the current working directory? If yes is this save? I think no.

PHP has a function to get the current working direction: getcwd() but if I remember right it is different if running as CLI or running in WebserverEnvironment.

Also I don't know if ./filename.typ works. I always want these things robust - bulletproof and therefor I ever use absolute pathes. :)

  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...