Jump to content

Recommended Posts

Posted

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

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

Posted
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

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
  • Recently Browsing   0 members

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