Jump to content
Sevarf2

images upload api issue

Recommended Posts

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";
        }

Share this post


Link to post
Share on other sites

Not sure, but maybe you are missing an ->add with the 

$page->$file_field

??

  • Like 1

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Maybe file permissions. Did you try it on another server/setup?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

well, I just created another image field with another name and it works...any idea why??

Share this post


Link to post
Share on other sites

Perhaps this:

$page->title             = $nome;

Should be:

$page->title             = $name;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...