1

So, I have a form to post a text to a blog. One of the informations include an image.

<label class="w3-text-black">Image:</label>
<input name="image" type="file"/>
<br></br>
<button name="submit" type="submit" class="w3-btn w3-gray w3-hover-black">Send</button>

And I have the PHP part that receives all that information, but only sends the rest to the DB and uploads the image to my server if the image is a PNG.

$temp = explode(".", $_FILES['image']['name']);

    if (strstr('.png', end($temp))){ //condition }

This code worked when used in my localhost, but once i uploaded it to my server, this error appeared: Warning: strstr(): Empty needle

And what $temp does is separate the extension with the use of explosion.

After that, i change the name of my file, so i can access it later with js.

$sqlImg = "SELECT * FROM posts WHERE post_id = (SELECT MAX(post_id) FROM posts)";
            $resid = mysqli_query($conn, $sqlImg);
            $linha = mysqli_fetch_assoc($resid);
            $id = $linha['post_id'];

            $path = 'blogimg/blog_img' . $id . '.' . end($temp);

            if (move_uploaded_file($_FILES['imagem']['tmp_name'], $path)){}
j08691
  • 204,283
  • 31
  • 260
  • 272
Guilherme P.
  • 113
  • 9

2 Answers2

1

You should not check the file extension with filename instead of doing that.

$allowed =  array('png'); // n number of file types here
$filename = $_FILES['image']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(in_array($ext,$allowed) ) {
    // Do operations;
}
Mr.Throg
  • 925
  • 6
  • 21
0

I The prolem was actualy pretty simple. I had the enctype set on my local file, but the file hosted in my server did not have it. So i added it and everything worked.

Guilherme P.
  • 113
  • 9