Jump to content

[SOLVED] Using WireUpload to upload an image file to a page image field - Nothing Uploaded


prestoav
 Share

Recommended Posts

Hi all,

PW 3.0.210

PHP 7.4.21

I'm trying to use WireUpload for the first time to upload an image to an existing multi-image field on a existing page but no image is uploaded to the temp path or the page itself.

Here's my code which is based on suggestions from other posts:

// Set File Extensions
$fileExtensionsAllowed = ['jpeg','jpg', 'JPEG', 'JPG', 'png', 'PNG', 'gif', 'GIF'];

// Set upload path
$upload_path = $urls->assets . "cache/temp_image_uploads/";

$p->of(false);

// New wire upload
$img1 = new WireUpload('img1');
$img1->setMaxFiles(1);
$img1->setOverwrite(true);
$img1->setDestinationPath($upload_path);
$img1->setValidExtensions($fileExtensionsAllowed);

// execute upload and check for errors
$imgs = $img1->execute();

// Run a count($files) test to make sure there are actually files; if so, proceed; if not, generate getErrors()
if(!count($imgs)) {
    echo "Sorry, but you need to add a photo!";
    return false;
}

// Run photo upload
foreach($imgs as $filename) {
    $pathname = $upload_path . $filename;
    $p->product_images->add($pathname);
    unlink($pathname);
}
// Save page
$p->save();
$p->of(true);

$p is the page that the field is present on and $->product_images being the field the image is to be uploaded to. At this time the intention is to upload just one image file, even though the field will take multiple images.

The upload directory definitely exists and when I run a print_r($_FILES) after form submission (before the code above) a file with the name attribute 'img1' is present in the $_FILES array.

When this code runs the "Sorry, but you need to add a photo!" error is seen and a the $imgs variable is empty.

There is nothing in the PW logs or MAMP PHP error logs reported.

Does anyone have any idea what I might be doing wrong here?

Thanks in advance for any pointers!

Link to comment
Share on other sites

  • prestoav changed the title to Using WireUpload to upload an image file to a page image field - Nothing Uploaded
4 hours ago, BitPoet said:

It should be $config->paths->assets, not $urls->assets, since you're passing a storage path on the server as the destination path, not a web link.

Your code doesn't inspect $img1->getErrors(), even though the comment mentions it before checking $imgs.

Hi @BitPoet,

Many thanks for this. The $urls->assets issue was the problem here. I didn't understand the difference between this and the $config->paths one so that's something new I've learned.

Thanks!

  • Like 1
Link to comment
Share on other sites

  • prestoav changed the title to [SOLVED] Using WireUpload to upload an image file to a page image field - Nothing Uploaded

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
 Share

  • Recently Browsing   0 members

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