从插件调用时,Wordpress wp_get_attachment_metadata()无法正常工作

时间:2014-07-14 00:08:31

标签: php wordpress metadata

我试图在插件上使用wp_get_attachment_metadata()函数。它正在调用函数,在里面我尝试获取附件的元数据信息。

$file = wp_get_attachment_metadata( $attachment_id, false);

问题是此代码仅返回图像文件(png,jpg)的元数据,即使其他文件类型已配置并正在正确上载。 (我可以看到媒体列表中的其他文件,但是,我无法使用此功能访问其元数据。)

请查看抓取元数据的代码:

add_filter( 'fu_is_debug', '__return_true' );

add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 3 );
function my_fu_after_upload( $attachment_ids, $success, $post_id ) {
    // do something with freshly uploaded files
    // This happens on POST request, so $_POST will also be available for you
    $dest_folder = "/exports/data/".$post_id;

    echo "FUNCTION AFTER UPLOAD: " . "<br>attachment_ids=";
    print_r($attachment_ids);
    echo "<br>result=". $success . "<br>post_id=". $post_id ;
    print_r($_POST);

    $post_name = get_post_field( 'post_title', $post_id, "");
    echo "Post name = " . $post_name . "<br>";

    if (!is_dir($dest_folder))
        mkdir($dest_folder);
    $upload_dir =  wp_upload_dir();
    echo "Upload dir<br>";
    print_r($upload_dir);

    foreach ($attachment_ids as $attachment_id) {
        printf("Getting attacments to atta %d <br>", $attachment_id);
        $file = wp_get_attachment_metadata( $attachment_id, false);
        print_r($file);
        echo "<br>";
        $file = $file['file'];
        $source_file = $upload_dir['basedir'] .'/'. $file;
        // Get the last name of the file, ie for /2013/07/file.img returns file.img
        $file = explode('/', $file);
        $file = $file[count($file) - 1];
        // Get the file extension
        $f_extension = explode('.', $file);
        if (count($f_extension) < 2 ) {
            $f_extension = "";
        } else {
            $f_extension = $f_extension[count($f_extension) - 1];
        }

        $dest_file = $dest_folder . '/' . $_POST['ra'] . '.' . $f_extension;

        printf("Copying '%s' to '%s'<br>", $source_file, $dest_file);
        // TODO: Verificar se não existe o arquivo antes
        copy($source_file, $dest_file);

    }
    flush();
}

add_action('fu_upload_result', 'my_fu_upload_result', 10, 2 );
function my_fu_upload_result( $layout, $result ) {
    // do something
    echo "FUNCTION UPLOAD RESULT: " . "<br>layout=" . $layout .
        "<br>result=". $result;
    print_r($_POST);

}

来源:https://gist.github.com/sombrafam/426dad85eb390c9b6082

图像文件的屏幕日志:

FUNCTION AFTER UPLOAD: 
attachment_ids=Array ( [0] => 121 ) 
result=1
post_id=32Array ( [post_ID] => 32 [post_title] => Erlon Cruz [ra] => 123456 [action] => upload_ugc [form_layout] => image [fu_nonce] => d71734168a [_wp_http_referer] => /?p=32 [ff] => f3c01b8520a3bdaf1f0141dd2b20f488 [form_post_id] => 32 ) Post name = Trabalho 1 - (ICA)
Upload dir
Array ( [path] => /var/www/wordpress/wp-content/uploads/2014/07 [url] => http://www.erloncruz.com.br/wp-content/uploads/2014/07 [subdir] => /2014/07 [basedir] => /var/www/wordpress/wp-content/uploads [baseurl] => http://www.erloncruz.com.br/wp-content/uploads [error] => ) Getting attacments to atta 121 
Array ( [width] => 940 [height] => 168 [file] => 2014/07/banner1.png [sizes] => Array ( [thumbnail] => Array ( [file] => banner1-150x150.png [width] => 150 [height] => 150 [mime-type] => image/png ) [medium] => Array ( [file] => banner1-300x53.png [width] => 300 [height] => 53 [mime-type] => image/png ) ) [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => ) ) 
Copying '/var/www/wordpress/wp-content/uploads/2014/07/banner1.png' to '/exports/data/32/123456.png'

Notice: Array to string conversion in /var/www/wordpress/wp-content/plugins/frontend-uploader/lib/php/functions.php on line 312
FUNCTION UPLOAD RESULT: 
layout=image
result=ArrayArray ( [post_ID] => 32 [post_title] => Erlon Cruz [ra] => 123456 [action] => upload_ugc [form_layout] => image [fu_nonce] => d71734168a [_wp_http_referer] => /?p=32 [ff] => f3c01b8520a3bdaf1f0141dd2b20f488 [form_post_id] => 32 ) 
Warning: Cannot modify header information - headers already sent in /var/www/wordpress/wp-includes/pluggable.php on line 1121 

来源:https://gist.github.com/sombrafam/c3bea65068e4c1b3b039

pdf上传日志:

FUNCTION AFTER UPLOAD: 
attachment_ids=Array ( [0] => 122 ) 
result=1
post_id=32Array ( [post_ID] => 32 [post_title] => Erlon Cruz [ra] => 123456 [action] => upload_ugc [form_layout] => image [fu_nonce] => d71734168a [_wp_http_referer] => /?p=32 [ff] => f3c01b8520a3bdaf1f0141dd2b20f488 [form_post_id] => 32 ) Post name = Trabalho 1 - (ICA)
Upload dir
Array ( [path] => /var/www/wordpress/wp-content/uploads/2014/07 [url] => http://www.erloncruz.com.br/wp-content/uploads/2014/07 [subdir] => /2014/07 [basedir] => /var/www/wordpress/wp-content/uploads [baseurl] => http://www.erloncruz.com.br/wp-content/uploads [error] => ) Getting attacments to atta 122 


Warning: Illegal string offset 'file' in /var/www/wordpress/wp-content/plugins/frontend-uploader/lib/php/functions.php on line 285

Notice: Uninitialized string offset: 0 in /var/www/wordpress/wp-content/plugins/frontend-uploader/lib/php/functions.php on line 285
Copying '/var/www/wordpress/wp-content/uploads/' to '/exports/data/32/123456.'

Warning: copy(): The first argument to copy() function cannot be a directory in /var/www/wordpress/wp-content/plugins/frontend-uploader/lib/php/functions.php on line 302

Notice: Array to string conversion in /var/www/wordpress/wp-content/plugins/frontend-uploader/lib/php/functions.php on line 312
FUNCTION UPLOAD RESULT: 
layout=image
result=ArrayArray ( [post_ID] => 32 [post_title] => Erlon Cruz [ra] => 123456 [action] => upload_ugc [form_layout] => image [fu_nonce] => d71734168a [_wp_http_referer] => /?p=32 [ff] => f3c01b8520a3bdaf1f0141dd2b20f488 [form_post_id] => 32 ) 
Warning: Cannot modify header information - headers already sent in /var/www/wordpress/wp-includes/pluggable.php on line 1121

来源:https://gist.github.com/sombrafam/bfdd531bbfa97e34f28d

1 个答案:

答案 0 :(得分:0)

我刚刚在我的开发环境中测试过,并没有在PDF上提取数据。我知道这并不是你想要听到的答案,但看起来这就是预期的功能。