dongren7374 2015-11-19 05:12
浏览 48
已采纳

无法使用wpdb将图像插入数据库

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条回答 默认 最新

  • doudu161481 2015-11-19 05:15
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记