Php 为什么我的blob没有被发送?

Php 为什么我的blob没有被发送?,php,mysql,wordpress,gravity-forms-plugin,Php,Mysql,Wordpress,Gravity Forms Plugin,我是一个新手,正在尝试编写一个文件上传验证器,将图像作为blob发送到MySQL数据库。该图像取自wordpress中上传的重力表单文件(表单ID为1)。当我运行下面的脚本时,不会向我的mallampati_images表发送blob,但会显示文件格式警报。它还输出此错误: Warning: file_get_contents(/home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/uploads/mallampati.png)

我是一个新手,正在尝试编写一个文件上传验证器,将图像作为blob发送到MySQL数据库。该图像取自wordpress中上传的重力表单文件(表单ID为1)。当我运行下面的脚本时,不会向我的mallampati_images表发送blob,但会显示文件格式警报。它还输出此错误:

Warning: file_get_contents(/home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/uploads/mallampati.png): failed to open stream: No such file or directory in /home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/themes/twentytwelve/functions.php on line 535
我不明白的是如何格式化指向该文件的链接。几个小时以来,我一直在尝试我能在网上找到的每一种方法,但我无法让它发挥作用。我编辑了下面的代码以返回链接(这似乎是一个正确的绝对链接…)

剧本:

function testimage($path)
{
   if(!preg_match("/\.(png|jpg|gif)$/",$path,$ext)) return 0;

   $ret = null;
   switch($ext[1])
   {
       case 'png': $ret = @imagecreatefrompng($path); break;
       case 'jpg': $ret = @imagecreatefromjpeg($path); break;
       case 'gif': $ret = @imagecreatefromgif($path); break;
       default: $ret = 0;
   }

   return $ret;
}

add_action("gform_after_submission_1", "input_fields", 10, 3);
function input_fields($entry){
    global $wpdb;

   if (isset($_FILES['input_1'])) {
     $file_url = $entry['1'];
     //$img_blob = file_get_contents($file_url);
     $validate = testimage($file_url);

     $udir = wp_upload_dir();
     $basedir = $udir['basedir'];
     $target=$basedir.'/'.basename($_FILES['input_1']['name']);

     //$try = $_FILES['input_1']['tmp_name'];
     $img_blob = file_get_contents ($target);
     echo "<script type='text/javascript'>alert('value: $target');</script>";
   }

  if(!empty($validate)) {
     echo "<script type='text/javascript'>alert('The file was of the correct format');</script>";

     $SQL = "INSERT INTO mallampati_images (img_blob) VALUES ( $img_blob )";
     $wpdb->query($SQL);
   }
}
函数测试图像($path)
{
如果(!preg_match(“/\(png | jpg | gif)$/”,$path,$ext))返回0;
$ret=null;
交换机($ext[1])
{
案例'png':$ret=@imagecreatefrompng($path);break;
案例'jpg':$ret=@imagecreatefromjpeg($path);break;
案例'gif':$ret=@imagecreatefromformgif($path);中断;
默认值:$ret=0;
}
返回$ret;
}
添加_操作(“提交_1之后的gform_”,“输入_字段”,10,3);
函数输入字段($entry){
全球$wpdb;
如果(isset($_文件['input_1'])){
$file_url=$entry['1'];
//$img\u blob=file\u get\u contents($file\u url);
$validate=testimage($file\u url);
$udir=wp_upload_dir();
$basedir=$udir['basedir'];
$target=$basedir.'/'.basename($_文件['input_1']['name']);
//$try=$_文件['input_1']['tmp_name'];
$img\u blob=文件获取内容($target);
回显“警报($value:$target”);”;
}
如果(!空($validate)){
echo“警报('文件格式正确');”;
$SQL=“插入mallampati_图像(img_blob)值($img_blob)”;
$wpdb->query($SQL);
}
}
我终于成功了

重力表单似乎并不像我所看到的那个样处理文件。问题是它导致SQL查询中使用了不正确的文件路径

function testimage($path)
{
   if(!preg_match("/\.(png|jpg|gif)$/",$path,$ext)) return 0;

   $ret = null;
   switch($ext[1])
   {
       case 'png': $ret = @imagecreatefrompng($path); break;
       case 'jpg': $ret = @imagecreatefromjpeg($path); break;
       case 'gif': $ret = @imagecreatefromgif($path); break;
       default: $ret = 0;
   }

   return $ret;
}

add_action("gform_after_submission_1", "input_fields", 10, 2);
function input_fields($entry){
    global $wpdb;

    if(isset($entry[1])){
      $valid = testimage($entry[1]);

      if($valid){
        $mpFilePath= $entry[1];
        $blob = file_get_contents($mpFilePath) or die ('cannot read file');
        $blob = mysql_escape_string($blob);
        $SQL = "INSERT INTO mallampati_images (img_blob) VALUES ( '$blob' )";
        $wpdb->query($SQL) or die ('query failed');
      }
      else{
        echo "<script type='text/javascript'>alert('Incorrect file format');</script>";
      }
    }
}
函数测试图像($path)
{
如果(!preg_match(“/\(png | jpg | gif)$/”,$path,$ext))返回0;
$ret=null;
交换机($ext[1])
{
案例'png':$ret=@imagecreatefrompng($path);break;
案例'jpg':$ret=@imagecreatefromjpeg($path);break;
案例'gif':$ret=@imagecreatefromformgif($path);中断;
默认值:$ret=0;
}
返回$ret;
}
添加_操作(“提交_1之后的gform_”,“输入_字段”,10,2);
函数输入字段($entry){
全球$wpdb;
如果(isset($entry[1])){
$valid=testimage($entry[1]);
如果($有效){
$mpFilePath=$entry[1];
$blob=file_get_contents($mpFilePath)或die('cannotreadfile');
$blob=mysql\u escape\u字符串($blob);
$SQL=“插入mallampati_图像(img_blob)值(“$blob”)”;
$wpdb->query($SQL)或die('query failed');
}
否则{
echo“警报(‘文件格式不正确’);”;
}
}
}

您的$_文件['input_1']['tmp_name']正在返回您的应用程序试图在应用程序中搜索的相对路径。路径可能是/home/clients/4CEAA5FAAB208E8AF43501386848484E6D6D/web/wp content/themes/Twenty12。尝试其他途径。首先用硬编码路径进行测试。PS没有否决你。看起来
wpdb
中不支持插入
BLOB
s,因此你可能必须直接使用
mysqli
。不幸的是,它们也不能使
mysqli
实例与getter一起使用,因此您需要扩展
wpdb
或创建一个新的
mysqli
实例来使用。。。见这个问题: