IAmNoob
IAmNoob

Reputation: 641

Image not getting upload in PHP

I am uploading an image using the following php code but the file is not getting upload.

if(isset($_POST['submit'])){
    $title = $_POST['title'];

    $target_folder = "../newsimageuploads/";
    $bannerimagelink = "http://example.com/newsimageuploads";

    $bannerimage = addslashes(file_get_contents($_FILES['bannerimage']['tmp_name']));
    $bannerimage_name = addslashes($_FILES['bannerimage']['name']);
    $bannerimage_size = getimagesize($_FILES['bannerimage']['tmp_name']);

    if ($bannerimage!=""){

    $rand = rand(111111, 9999999);
    $fname =  $_FILES["bannerimage"]["name"];
    $newname = "Image ".$rand.".png";

    move_uploaded_file($_FILES["bannerimage"]["tmp_name"], $target_folder.$newname);
    $bannerimage_location = $bannerimagelink."/".$newname;
    } 

    $query =mysql_query("INSERT INTO mytable (title,image) VALUES ('$title','$bannerimage_location')")or die(mysql_error());
    if (($query) === TRUE) {
    echo "<p style='color:green;'>Added Successfully</p>";
    } else {
    echo "Some Error Occured :(";
    }
}

And my HTML part is

<form action="#" method="post">
    <input type="text" name="title">
    <input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
    <button type="submit" name="submit">Add</button>
</form>

My title is getting insert in the MySQL table but image does not.

Upvotes: 0

Views: 64

Answers (3)

Elyor
Elyor

Reputation: 5532

<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>

Upvotes: 1

Renato Gloyer
Renato Gloyer

Reputation: 61

Add

enctype="multipart/form-data"

to the form tag. Without this attribute you will only get the name of the file. But the file itself won't be uploaded.

Upvotes: 1

Mihai
Mihai

Reputation: 26784

You are missing enctype='multipart/form-data' in your form

<form action="#" method="post" enctype="multipart/form-data">

Look here for more details

Upvotes: 1

Related Questions