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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题