0

I'm doing an image upload but to upload the error.
The error is in the variable $novoNome, this variable can not be sent to the PDO function, which will insert fields in the database
But only happens on some images, some work some do not.

let's go to the code

if (isset($_POST['newpostimage'])) {

  if(empty($errors) === true){
    
    if(isset($_FILES['image']['name']) && $_FILES["image"]["error"] == 0)
    {

      $arquivo_tmp = $_FILES['image']['tmp_name'];
      $nome = $_FILES['image']['name'];

      $extensao = strrchr($nome, '.');

      $extensao = strtolower($extensao);
      if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
      {
        
        $novoNome = md5(microtime()) . $extensao;

        $destino = 'uploads/postimages/' . $novoNome;

        if( @move_uploaded_file( $arquivo_tmp, $destino  ))
        {

        }
        else
          echo "Error saving the file. Apparently you do not have write permission.<br />";
      }
      else
        echo "You can only send files \"*.jpg;*.jpeg;*.gif;*.png\"<br />";

      }



    $user   = $userid;
    $date  = date('Y/m/d');
    $time   = date('H:i:s');
    $posttype = "image";
    $post  = $_POST['mypost'];
    $linkimg = $novoNome;

    $users->newpostimage($user, $date, $time, $posttype, $post, $linkimg);
    header('Location: /home');
    exit();

  }
}


This is the function newpostimage

public function newpostimage($user, $date, $time, $posttype, $post, $linkimg){
		
		$query 	= $this->db->prepare("INSERT INTO `post` (`userid`, `date`, `time`, `post`, `posttype`, `image`) VALUES (?, ?, ?, ?, ?, ?)");

		$query->bindValue(1, $user);
		$query->bindValue(2, $date);
		$query->bindValue(3, $time);
		$query->bindValue(4, $post);
		$query->bindValue(5, $posttype);
		$query->bindValue(6, $linkimg);


		try{
			$query->execute();

		}catch(PDOException $e){
			die($e->getMessage());
		}

	}

The error is:

Notice: Undefined variable: novoNome in C:\xampp\htdocs\home.php on line 99 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null

0

2 Answers 2

1

The first thing you should do is move the newpostimage call into the innermost if clause. Otherwise the checks don't make alot of sense and the code is always executed - which is also the reason for the error.

if (isset($_POST['newpostimage'])) 
{
    if(empty($errors) === true)
    {
        if(isset($_FILES['image']['name']) && $_FILES["image"]["error"] == 0)
        {

            $arquivo_tmp = $_FILES['image']['tmp_name'];
            $nome = $_FILES['image']['name'];

            $extensao = strrchr($nome, '.');

            $extensao = strtolower($extensao);
            if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
            {
                $novoNome = md5(microtime()) . $extensao;
                $destino = 'uploads/postimages/' . $novoNome;

                if( @move_uploaded_file( $arquivo_tmp, $destino  ))
                {
                    $user   = $userid;
                    $date  = date('Y/m/d');
                    $time   = date('H:i:s');
                    $posttype = "image";
                    $post  = $_POST['mypost'];
                    $linkimg = $novoNome;

                    $users->newpostimage($user, $date, $time, $posttype, $post, $linkimg);
                    header('Location: /home');
                    exit();
                }
                else 
                {
                    echo "Error saving the file. Apparently you do not have write permission.<br />";
                }
            else 
            {
                echo "You can only send files \"*.jpg;*.jpeg;*.gif;*.png\"<br />";
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

hum, but yesterday was uploading normally today did not do. I changed the local server to the xampp is because of this?
and some images are uploaded but not others, same format until
There may be other issues with your code respectively folder permissions etc. But it's hard to tell with the information we have. The above error is because $novoNome is undefined which also means it doesn't pass the extension check or one of the checks before that.
possibly I need to make the variable $novoNome global
If you define it as an empty string at the very top of your code the error will be gone but I don't think that this is you goal because you will then have a db entry no matter if the file is uploaded or not...
|
0

When you upload an image with wrong extension ( if(strstr('.jpg;.jpeg;.gif;.png', $extensao)) ), your $novoNome variable would be undefined, so your 'image' param in the PDO will be null and I think your database schema don't allow that field to be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.