0
votes

I am trying to run this code to take image input from front end. I know I can't simply upload images using just this $_POST['img']. I read that I have to use file_get_contents() for this purpose. But I am new to this , I don't know How exactly to use file_get_contents()

However I tried this code, but I am unsuccessful in inserting.

<form action="" method="post">
<label id="img">image: <input type="file" name="img" id='media'/></label>
<input type="submit" name="Submit" value="upload"/>

<?php

global $wpdb;
if($_POST['Submit']) 
{
$image=$_POST['img'];

if($wpdb->insert(
    'image',
    array(

            'image' => $image


        )
) == false) echo 'Database Insertion failed';
else echo 'Database insertion successful<p />';
}
?>

This did not work. After this I tried this. But still unsuccessful.

<form action="" method="post">
<label id="img">image: <input type="file" name="img" id='media'/></label>
<input type="submit" name="Submit" value="upload"/>

<?php

global $wpdb;
if($_POST['Submit']) 
{
$image=$_POST['img'];
$item = file_get_contents($_FILES['img']['tmp_name']);
if($wpdb->insert(
    'image',
    array(

            'image' => $item


        )
) == false) echo 'Database Insertion failed';
else echo 'Database insertion successful<p />';
}
?>

for this I am getting a warning Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\kite\wp-content\plugins\php-code-widget\execphp.php(27) : eval()'d code on line 11

Somebody please help me out.

And in my database, in the table "image" the column "image" has the data type BLOB, not sure if the problem is with this.

Thanks in advance

1

1 Answers

0
votes

For file uploads you should add, enctype="multipart/form-data"

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

First upload to some folder and then use file_get_contents()

if (move_uploaded_file($_FILES['file']['tmp_name'], "your path". $_FILES["file"]['name'])) {
       // image will get uploaded here
}

Then use

file_get_contents("new path")