Juergen Posted May 5, 2015 Share Posted May 5, 2015 (edited) 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 Edited May 6, 2015 by horst Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 5, 2015 Share Posted May 5, 2015 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. 1 Link to comment Share on other sites More sharing options...
Juergen Posted May 5, 2015 Author Share Posted May 5, 2015 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 1 Link to comment Share on other sites More sharing options...
Juergen Posted May 5, 2015 Author Share Posted May 5, 2015 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. 4 Link to comment Share on other sites More sharing options...
Pete Posted May 5, 2015 Share Posted May 5, 2015 I've fallen for this before myself - glad you worked it out. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now