我正在使用一个前端上传表单,它使用blueimp jQuery文件上传程序来完成所有繁重的工作。。。但是,在尝试向blueimp添加必要的WordPress功能后,我开始遇到一些我无法理解的错误。
Update / My code
HTML(前端表单):
<form id="fileupload" action="" method="POST" enctype="multipart/form-data">
<span class="btn btn-success fileinput-button">
<span>Add files...</span>
<!-- The file input field used as target for the file upload widget -->
<input type="file" name="files[]" multiple />
</span>
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
</form>
JS(我使用的JS与Basic Plus演示文件中使用的JS相同),只是将url变量更改为UploadHandler的位置。php文件
PHP(我上传脚本的相关部分…我使用的是默认值UploadHandler script 并尝试为WordPress定制)
// Required WordPress files and hooks
require_once( $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-load.php" );
require_once( $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/media.php" );
require_once( $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/file.php" );
require_once( $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/image.php" );
$upload_dir = wp_upload_dir();
global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;
// Some default blueimp code
// First few lines of the __construct function
global $upload_dir;
$this->options = array(
\'script_url\' => $this->get_full_url(),
\'upload_dir\' => $upload_dir[\'path\'] . \'/\',
\'upload_url\' => $upload_dir[\'url\'] . \'/\',
// A lot more default script code
// The handle_file_upload function with WordPress customizations
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
$index = null, $content_range = null) {
global $logged_in_user;
$file = new stdClass();
$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
$index, $content_range);
$file->size = $this->fix_integer_overflow(intval($size));
$file->type = $type;
if ($this->validate($uploaded_file, $file, $error, $index)) {
$this->handle_form_data($file, $index);
$upload_dir = $this->get_upload_path();
if (!is_dir($upload_dir)) {
mkdir($upload_dir, $this->options[\'mkdir_mode\'], true);
}
$file_path = $this->get_upload_path($file->name);
$append_file = $content_range && is_file($file_path) &&
$file->size > $this->get_file_size($file_path);
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
// multipart/formdata uploads (POST method uploads)
if ($append_file) {
file_put_contents(
$file_path,
fopen($uploaded_file, \'r\'),
FILE_APPEND
);
} else {
move_uploaded_file($uploaded_file, $file_path);
}
} else {
// Non-multipart uploads (PUT method support)
file_put_contents(
$file_path,
fopen(\'php://input\', \'r\'),
$append_file ? FILE_APPEND : 0
);
}
$file_size = $this->get_file_size($file_path, $append_file);
if ($file_size === $file->size) {
$file->url = $this->get_download_url($file->name);
if ($this->is_valid_image_file($file_path)) {
$this->handle_image_file($file_path, $file);
}
} else {
$file->size = $file_size;
if (!$content_range && $this->options[\'discard_aborted_uploads\']) {
unlink($file_path);
$file->error = $this->get_error_message(\'abort\');
}
}
$this->set_additional_file_properties($file);
}
$attachment = array(
\'post_mime_type\' => $file->type,
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $file_path )),
\'post_content\' => \'\',
\'post_author\' => $logged_in_user,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'guid\' => $this->options[\'upload_url\'] . $name
);
$attachment_id = wp_insert_attachment( $attachment, $file_path );
// Generate the attachment data
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );
// Update the attachment metadata
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $file;
}
Updated Issue:
起初,这似乎奏效了。。。但是,当你检查
wp_posts database table
未添加任何内容。此外,当查看
media library
它只显示上载的图像(数据库中没有其他图像),并且以下错误出现在
error_log
:
PHP警告:strpos()要求参数1为字符串,即/Applications/MAMP/htdocs/wordpress/wp includes/post中给出的对象。php第188行php警告:preg\\u match()期望参数2是字符串,对象在/Applications/MAMP/htdocs/wordpress/wp includes/post中给出。php第188行php可捕获致命错误:stdClass类的对象无法转换为/Applications/MAMP/htdocs/wordpress/wp includes/post中的字符串。php在线189
我知道我调用的变量是一个对象,而不是一个字符串,但我不确定应该在其中放置什么。
非常感谢您的帮助,我已经尽力了。真的需要尽快启动并运行!谢谢