在wordpress中上传多张图片
Multiple image upload in wordpress
我想使用 wordpress 上传多张图片。
这是我用于单次上传的代码
if ( ! function_exists( 'wp_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['photo'];
$upload_overrides = array( 'test_form' => FALSE );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
}
else {
echo "Possible file upload attack!\n";
}
如何使用 wordpress 上传多张图片?
您可以使用本机 WordPress 媒体上传器更轻松地实现您想要做的事情。
我曾经为一个很好的资源添加了书签,但似乎找不到了。几个月前,我 运行 遇到了需要同样事情的情况。这就是我最终做的:
将 WordPress 图像脚本加载到我的 PHP 脚本的开头:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
然后您可以使用 jQuery 在元素点击时调用上传器,如下所示:
jQuery(document).ready(function($) {
// Define a variable to be used to store the frame data
var file_frame;
var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
var set_to_post_id = 999; // Set this
$('#upload_button').live('click', function( event ){
element = $(this);
event.preventDefault();
// If the media frame already exists, reopen it.
if (file_frame) {
// Set the post ID to what we want
file_frame.uploader.uploader.param('post_id', set_to_post_id);
// Open frame
file_frame.open();
return;
} else {
// Set the wp.media post id so the uploader grabs the ID we want when initialised
wp.media.model.settings.post.id = set_to_post_id;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: $(this).data('uploader_title'),
button: {
text: $(this).data('uploader_button_text'),
},
multiple: true // Set to false to allow only one file to be selected
});
// When an image(s) have been selected, run a callback.
file_frame.on('select', function() {
// Do something if necessary
function_to_fire();
});
// Finally, open the modal
file_frame.open();
});
});
有一个 attachment
对象传递给回调函数,它将包含上传附件的所有 ID 等。
set_to_post_id
是您要 "attach" 媒体的 post/page 的 ID。如果您不打算将它们附加到 post,则无需担心这一点,上面的某些代码将不适用。
尝试这样实现:
$files = $_FILES['photo'];
foreach ($files['photo'] 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]
);
wp_handle_upload($file);
}
}
我想使用 wordpress 上传多张图片。
这是我用于单次上传的代码
if ( ! function_exists( 'wp_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['photo'];
$upload_overrides = array( 'test_form' => FALSE );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
}
else {
echo "Possible file upload attack!\n";
}
如何使用 wordpress 上传多张图片?
您可以使用本机 WordPress 媒体上传器更轻松地实现您想要做的事情。
我曾经为一个很好的资源添加了书签,但似乎找不到了。几个月前,我 运行 遇到了需要同样事情的情况。这就是我最终做的:
将 WordPress 图像脚本加载到我的 PHP 脚本的开头:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
然后您可以使用 jQuery 在元素点击时调用上传器,如下所示:
jQuery(document).ready(function($) {
// Define a variable to be used to store the frame data
var file_frame;
var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
var set_to_post_id = 999; // Set this
$('#upload_button').live('click', function( event ){
element = $(this);
event.preventDefault();
// If the media frame already exists, reopen it.
if (file_frame) {
// Set the post ID to what we want
file_frame.uploader.uploader.param('post_id', set_to_post_id);
// Open frame
file_frame.open();
return;
} else {
// Set the wp.media post id so the uploader grabs the ID we want when initialised
wp.media.model.settings.post.id = set_to_post_id;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: $(this).data('uploader_title'),
button: {
text: $(this).data('uploader_button_text'),
},
multiple: true // Set to false to allow only one file to be selected
});
// When an image(s) have been selected, run a callback.
file_frame.on('select', function() {
// Do something if necessary
function_to_fire();
});
// Finally, open the modal
file_frame.open();
});
});
有一个 attachment
对象传递给回调函数,它将包含上传附件的所有 ID 等。
set_to_post_id
是您要 "attach" 媒体的 post/page 的 ID。如果您不打算将它们附加到 post,则无需担心这一点,上面的某些代码将不适用。
尝试这样实现:
$files = $_FILES['photo'];
foreach ($files['photo'] 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]
);
wp_handle_upload($file);
}
}