从多个文件上传中获取附件 ID 以保存在自定义 table
Get attachment ID from multiple file upload for saving in custom table
我的代码已将附件文件保存在WP 媒体库中。现在我想将附件 ID 保存在我的自定义 table 中。我怎样才能做到这一点?我在我的 table 中保存了一个上传文件的附件 ID,但我不知道如何在自定义 table 中保存多个附件 ID?因为媒体上传使用了 for-each 循环。谢谢!
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$files = $_FILES["my_image_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_image_upload" => $file);
//var_dump($_FILES);
foreach ($_FILES as $file => $array) {
// $newupload = my_handle_attachment($file,$post_id);
$attach_id = media_handle_upload( $file, -1 );
var_dump($attach_id);
var_dump($array);
}
}
}
使用
global $wpdb
$wpdb->insert( $table, $data, $format );
$wpdb->insert_id
table (string) The name of the table to insert data into.
data (array) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL
escaped).
format (array|string) (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used
for all of the values in $data. If omitted, all values in $data will
be treated as strings unless otherwise specified in
wpdb::$field_types.
你也可以运行像
global $wpdb;
$sql = "INSERT INTO table (xxx, xxx, xxx, xxx, xxx) VALUES ('$xxx', '$xxx', 'xxx', '$xxx', CURRENT_TIMESTAMP)";
$wpdb->query($sql)
在这里您可以找到更多示例https://codex.wordpress.org/Class_Reference/wpdb
我的代码已将附件文件保存在WP 媒体库中。现在我想将附件 ID 保存在我的自定义 table 中。我怎样才能做到这一点?我在我的 table 中保存了一个上传文件的附件 ID,但我不知道如何在自定义 table 中保存多个附件 ID?因为媒体上传使用了 for-each 循环。谢谢!
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$files = $_FILES["my_image_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_image_upload" => $file);
//var_dump($_FILES);
foreach ($_FILES as $file => $array) {
// $newupload = my_handle_attachment($file,$post_id);
$attach_id = media_handle_upload( $file, -1 );
var_dump($attach_id);
var_dump($array);
}
}
}
使用
global $wpdb
$wpdb->insert( $table, $data, $format );
$wpdb->insert_id
table (string) The name of the table to insert data into.
data (array) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
format (array|string) (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
你也可以运行像
global $wpdb;
$sql = "INSERT INTO table (xxx, xxx, xxx, xxx, xxx) VALUES ('$xxx', '$xxx', 'xxx', '$xxx', CURRENT_TIMESTAMP)";
$wpdb->query($sql)
在这里您可以找到更多示例https://codex.wordpress.org/Class_Reference/wpdb