我想从管理面板添加名称和图像。我已经在mysql中创建了一个表
名称> Varchar [255]img> [文本]
当我单击保存按钮名称字段时,效果很好:)但文件字段名称为空我如何获取文件字段值,然后将此图像上传到<
这样的文件夹我想将文件上传到我的cutom文件夹中,并将其保存到我的mysql表的路径。但我的代码不在这里工作。<?php
$id = $_POST["id"];
$name = $_POST["name"];
$photo = $_POST["photo"];
//insert
if(isset($_POST['insert'])){
global $wpdb;
$wpdb->insert(
'wp_savans', //table
array(
'name' => $name,
'photo' => $photo
),
array('%s', '%s') //data format
);
$message.="Inserted";
}
?>
<div class="wrap">
<?php if (isset($message)): ?><div class="updated"><p><?php echo $message;?></p></div><?php endif;?>
<form method="post" name="form1" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table width="464" border="0">
<tr>
<td width="127" bgcolor="#66CCFF"><strong>Name</strong></td>
<td width="327"><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td bgcolor="#66CCFF"><strong>Photopath</strong></td>
<td><input name="photo" id="photo" type="file"></td>
</tr>
</table>
<input type='submit' name="insert" value='Save' class='button'>
</form>
</div>
$photo = $_POST["photo"];
,因为您应将其视为文件类型,将其视为$photo = $_FILES["photo"]["name"];
。在这里,我在wp-content/uploads/contactimage/
中建立了一个文件夹,图像已成功上传。尝试此操作,我对您的代码进行了一些更正,可能会帮助您。 $id = $_POST["id"];
$name = $_POST["name"];
$photo = $_FILES["photo"]["name"];
//insert
if(isset($_POST['insert'])){
global $wpdb;
$wpdb->insert(
'wp_savans', //table
array(
'name' => $name,
'photo' => $photo
),
array('%s', '%s') //data format
);
$message.="Inserted";
}
$destination= get_home_path().'wp-content/uploads/contactimage/';
move_uploaded_file($_FILES["photo"]["tmp_name"], $destination.$_FILES["photo"]["name"]);
echo "Stored in: " .get_home_path().'wp-content/uploads/contactimage/'. $_FILES["photo"]["name"];
?>
<div class="wrap">
<?php if (isset($message)): ?><div class="updated"><p><?php echo $message;?></p></div><?php endif;?>
<form method="post" name="form1" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table width="464" border="0">
<tr width="327">
<td ></td>
<tr></tr>
<td width="127" ><strong>Name</strong></td>
<td width="327"><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td ><strong>Photopath</strong></td>
<td><input name="photo" id="photo" type="file"></td>
</tr>
</table>
<input type='submit' name="insert" value='Save' class='button'>
</form>
</div>
<?php } ?>
$id = $_POST["id"];
$name = $_POST["name"];
$photo = $_FILES["photo"]["name"];
//insert
if(isset($_POST['insert'])){
global $wpdb;
$wpdb->insert(
'wp_savans', //table
array(
'name' => $name,
'photo' => $photo
),
array('%s', '%s') //data format
);
$message.="Inserted";
}
$destination= get_home_path().'wp-content/uploads/contactimage/';
move_uploaded_file($_FILES["photo"]["tmp_name"], $destination.$_FILES["photo"]
["name"]);
echo "Stored in: " .get_home_path().'wp-content/uploads/contactimage/'.
$_FILES["photo"]["name"];
?>
我放置在functions.php中并尝试回显此$ photo = $ _FILES [“ photo”] [“ name”];但我没有得到结果