Jump to content

Single image upload on frontend doesnt work correctly


Juergen
 Share

Recommended Posts

hello @ all,

I have tried to use a code that i found in the forum to make a single image upload on the frontend. The problem is that it stores multiple images instead of deleting the old and storing the new.

My aim is to offer subscribers to add/change their profile image in the frontend.

Here is the code that I use from the post that I have found:

<?php

    $upload_path = $config->paths->assets . "files/avatar_uploads/"; 

    $f = new WireUpload('userimage'); 
    $f->setMaxFiles(1);
    $f->setMaxFileSize(1*1024*1024);
    $f->setOverwrite(true);
    $f->setDestinationPath($upload_path);
    $f->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

    if($input->post->form_submit) {
        if(!is_dir($upload_path)) {
            if(!wireMkdir($upload_path)) throw new WireException("No upload path!");
        }

        $files = $f->execute(); 
    
        if ($f->getErrors()) {    
            foreach($files as $filename) @unlink($upload_path . $filename);
            foreach($f->getErrors() as $e) echo $e;
        } else {
            //$u = $users->get($user);
            //$u = $user->name;
            //Save the photo to the avatar field
            $user->of(false);
            $user->userimage->removeAll(); // wirearray  (line added by @horst: explanation is three posts beneath)
            $user->userimage = $upload_path . $files[0];
            $user->save();
            $user->of(true);
            @unlink($upload_path . $files[0]);
        }
    }

?>
<form class="forum-form" accept-charset="utf-8" action="./" method="post" enctype="multipart/form-data" >
    <input type="file" id="attach" name="userimage" accept="image/jpg,image/jpeg,image/gif,image/png" /> 
    <input type="submit" name="form_submit" value="Submit"/> 
</form>

$f->setMaxFiles(1); so it is set to 1 file

$f->setOverwrite(true); so it should overwrite the existing image

Every time I upload a new image it will be added instead of deleting the old. So I have more than one image displayed in the backend area.

If I upload the image in the backend it works as expected.

I use the latest dev version of PW.

Has anyone an idea why MaxFiles and setOverwrite will be ignored?

This ist the array output if I use var_dump:

object(Pageimages)#729 (2) { ["count"]=> int(2) ["items"]=> array(2) { ["chrysanthemum.jpg"]=> string(17) "chrysanthemum.jpg" ["desert.jpg"]=> string(10) "desert.jpg" } } 

Best regards Jürgen

post-2257-0-95137600-1430826911_thumb.jp

post-2257-0-22698100-1430826924_thumb.jp

Edited by horst
Link to comment
Share on other sites

From the api side image fields are always multi-value. Only the "output-formatting" state does allow you to call the first image automatically. Therefore you need to clear the image field manually before adding the new image.

  • Like 1
Link to comment
Share on other sites

From the api side image fields are always multi-value. Only the "output-formatting" state does allow you to call the first image automatically. Therefore you need to clear the image field manually before adding the new image.

But for what is the API call "setOverwrite(true)" ? It must be possible to overwrite the existing image by uploading a new image without deleting the old manually. In backend it works perfectly.

Only one image is allowed: setMaxFiles(1)

If I upload a new one then delete (overwrite) the old on and store the new: setOverwrite(true)

This seems logical to me :undecided:

  • Like 1
Link to comment
Share on other sites

I found the solution:

you have to include

$user->userimage->removeAll(); // wirearray

before this line of code

$user->userimage = $upload_path . $files[0];

This removes the image array in a first step and then loads the new image in the database.

  • Like 4
Link to comment
Share on other sites

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...