Frank Vèssia Posted September 4, 2019 Share Posted September 4, 2019 Hello guys, I'm stuck on a simple images upload, it's not the first time I use a code like this but this time I get no errors and I don't know where is the problem. The page is saved, the log is saved with the image path but it's not added to the page...I checked everything... $page->of(false); $page->title = $nome; $page->city = $city; $page->phone = $phone; if($page->save()){ $file_field = "images"; $file_extensions = array('jpg', 'jpeg'); $max_upload_size = 6*1024*1024; $overwrite = false; $upload_path = $config->paths->assets . "tmp_uploads/"; if(is_dir($upload_path)) { $img = new WireUpload($file_field); $img->setMaxFiles(1); $img->setMaxFileSize($max_upload_size); $img->setOverwrite($overwrite); $img->setDestinationPath($upload_path); $img->setValidExtensions($file_extensions); $files = $img->execute(); if(!$img->getErrors()){ foreach($files as $filename) { $log->save("upload","page id: ". $page->id." ".$upload_path . $filename); $page->$file_field = $upload_path . $filename; unlink($upload_path . $filename); } $page->save(); }else{ // get the errors if(count($img->getErrors()) > 1){ // if multiple error foreach($img->getErrors() as $e) { $results .= $e." "; } } else { // if single error $results = $img->getErrors(); } } }else{ $results = ""; } $page->of(true); }else{ $results = "Error"; } Link to comment Share on other sites More sharing options...
horst Posted September 4, 2019 Share Posted September 4, 2019 Not sure, but maybe you are missing an ->add with the $page->$file_field ?? 1 Link to comment Share on other sites More sharing options...
lokomotivan Posted September 4, 2019 Share Posted September 4, 2019 foreach($files as $filename) { $page->{$file_field}->add($upload_path . $filename); } 1 Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 5, 2019 Author Share Posted September 5, 2019 7 hours ago, lokomotivan said: foreach($files as $filename) { $page->{$file_field}->add($upload_path . $filename); } thanks @horst and @lokomotivan but I already tried ->add but nothing changed ? , as I said this code has been used a lot of times in other projects with no issues, I really don't know what's happening here Link to comment Share on other sites More sharing options...
bernhard Posted September 5, 2019 Share Posted September 5, 2019 Maybe file permissions. Did you try it on another server/setup? Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 5, 2019 Author Share Posted September 5, 2019 16 minutes ago, bernhard said: Maybe file permissions. Did you try it on another server/setup? I didn't try on another server but I have checked file permissions, I also removed the unlink command to see if the file si uploaded and it's there, plus I use this exact code in another part of the site to add pictures to users profile and it works, the only difference is the template, user template vs normal page, it's quite a mistery... Link to comment Share on other sites More sharing options...
bernhard Posted September 5, 2019 Share Posted September 5, 2019 https://www.google.com/search?ei=Ic5wXcH_FsqNkwWrkrrABw&q=site%3Aprocesswire.com+user+template+image+upload&oq=site%3Aprocesswire.com+user+template+image+upload&gs_l=psy-ab.3...24062.26703..26931...3.0..0.86.703.10......0....1..gws-wiz.6KgaF6uurX4&ved=0ahUKEwiBlLOqqLnkAhXKxqQKHSuJDngQ4dUDCAs&uact=5 Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 5, 2019 Author Share Posted September 5, 2019 my form has the enctype="multipart/form-data" Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 5, 2019 Author Share Posted September 5, 2019 well, I just created another image field with another name and it works...any idea why?? Link to comment Share on other sites More sharing options...
gmclelland Posted September 5, 2019 Share Posted September 5, 2019 Perhaps this: $page->title = $nome; Should be: $page->title = $name; Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 5, 2019 Author Share Posted September 5, 2019 2 minutes ago, gmclelland said: Perhaps this: $page->title = $nome; Should be: $page->title = $name; this has nothing to do with the images upload, it's just another field on that page and the name of the variable is correct $nome, it's not english 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