为什么我的 blob 没有被发送?
Why does my blob not get sent?
我是新手,正在尝试编写一个文件上传验证器,该验证器将图像作为 blob 发送到 MySQL 数据库。图片取自wordpress中上传的一个重力表单文件(表单ID为1)。当我 运行 下面的脚本时,没有 blob 发送到我的 mallampati_images table,但是会显示文件格式警报。它还会输出此错误:
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
我不明白的是如何将link格式化为文件。几个小时以来,我一直在尝试各种可以在网上找到的方法,但我无法让它发挥作用。我将下面的代码编辑为 return link(这是一个正确的绝对值 link,似乎...)
脚本:
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);
}
}
我终于成功了
Gravity 表单似乎无法完全按照我一直在查看的示例处理文件。问题是它导致 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>";
}
}
}
我是新手,正在尝试编写一个文件上传验证器,该验证器将图像作为 blob 发送到 MySQL 数据库。图片取自wordpress中上传的一个重力表单文件(表单ID为1)。当我 运行 下面的脚本时,没有 blob 发送到我的 mallampati_images table,但是会显示文件格式警报。它还会输出此错误:
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
我不明白的是如何将link格式化为文件。几个小时以来,我一直在尝试各种可以在网上找到的方法,但我无法让它发挥作用。我将下面的代码编辑为 return link(这是一个正确的绝对值 link,似乎...)
脚本:
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);
}
}
我终于成功了
Gravity 表单似乎无法完全按照我一直在查看的示例处理文件。问题是它导致 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>";
}
}
}