Problems when saving image

Asked

Viewed 34 times

1

I’m trying to save an image I get through form.

I’m using move_uploaded_file() for that reason.

At the moment, my code is this way:

class Image
{
    public function save($image)
    {
        $name = time();
        $destination = "public/img/vehicles/" . $name . ".jpg";
        $result = move_uploaded_file($image['tmp_name'], $destination);
        return $result;
    }
}

File I receive form data:

$image = $_FILE['image'];

$saveImage = new Image();
$destinationImg = $saveImage->save($image);

But when I check the directory, the image is not there. And the $resultImg always returns to me false.

  • Within the class try to call it that, $image['nome_input_html']['tmp_name'], in doubt print_r($image)

  • @Rray Continued the same way...

  • 2

    Ahhh is $_FILES ta missing a s there in the code.

  • 1

    That’s right, rray! True. But still returns me false. :(

  • And print_r() returned q?

  • I gave a print_r on the variable $image, and it returned me the following: [type] => image/jpeg [tmp_name] => /tmp/phpnjYqd1 [error]

  • It may be an error of permission or that the folder does not exist etc. It does so p catch the error, if(!move_upload(......parametros.....){ print_r(error_get_last());}

  • Wouldn’t it be move_uploaded_files()? When I use move_upload he returns me call to Undefined Function

  • That’s right, haha typed wrong xD

  • 2

    I was able to fix it... it was just a mistake on the var $destination class. It turns out that she was in a subfolder and had to be in fact ../public/img/.

Show 6 more comments

1 answer

1

First see if the variable $imagem has a valid upload file, using the function is_uploaded_file ( string $filename ) .

class Image
{
    public function save($image)
    {
        var_dump(is_uploaded_file ($image['tmp_name']));

        $name = time();
        $destination = "public/img/vehicles/" . $name . ".jpg";
        $result = move_uploaded_file($image['tmp_name'], $destination);
        var_dump($image['error']);
        return $result;
    }
}

If the above function returns false (there is a problem with the upload file), see apache logs (if you use apache as a server) or use the function error_get_last().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.