使用Bluimp Uploader从自定义页面上传图像?

时间:2014-01-25 作者:Designer 17

我正在使用一个前端上传表单,它使用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

我知道我调用的变量是一个对象,而不是一个字符串,但我不确定应该在其中放置什么。

非常感谢您的帮助,我已经尽力了。真的需要尽快启动并运行!谢谢

2 个回复
最合适的回答,由SO网友:Designer 17 整理而成

在进行测试并与朋友合作后,我找到了一个解决方案:

只需更改功能get_full_url 获取url:

// Go to \'protected function get_full_url()\'
// Line 200 (approx. if you\'ve made the same changes stated above) in UploadHandler.php 
// change this line, should be the last one in the function
substr($_SERVER[\'SCRIPT_NAME\'],0, strrpos($_SERVER[\'SCRIPT_NAME\'], \'/\'));

// After editing this is what you should end with
substr($_SERVER[\'DOCUMENT_ROOT\'],0, strrpos($_SERVER[\'DOCUMENT_ROOT\'], \'/\'));
这就是我的工作原理(以及问题中的自定义代码),允许我在自定义WordPress页面中使用演示上传程序。创建前端上传表单!希望这对别人有帮助。

SO网友:user3111957

我最近修改了Wordpress页面,添加了文件上传插件。在一个独立的测试页面上,控件(也是从basicplus示例修改而来)运行良好。但是,在Wordpress中上载失败。我拆下了表单包装,这就解决了它。

结束

相关推荐

Protect Uploads in Multisite

我正在使用WordPress multisite(3.3.1),并试图保护从某些博客上传的内容不被热链接和直接访问。这与How to Protect Uploads, if User is not Logged In?, 但我只想通过服务器上运行的脚本访问上传内容(即,只有服务器可以显示/提供上传服务),因此内容可以受到WordPress用户权限的保护。例如,我有一个名为40c的字段。jpg位于localhost/files/2011/07/40c。jpg;我想让文件显示出来only 在本地域上由HTML