2

我正在尝试从网络服务接收文件并将它们保存到 wordpress 但我有以下问题,

  1. 一些文件将被上传,但它们已损坏,我无法打开它们,尽管它们的 web 服务地址是正确的。
  2. 它显示了以下错误(尽管它显示了这些错误,但还是上传了文件,这很好:D,但我想知道为什么我会收到它们)。

注意:常量 ABSPATH 已在第 22 行的 /Applications/MAMP/htdocs/wordpress/wp-load.php 中定义

致命错误:在第 95 行的 /Applications/MAMP/htdocs/wordpress/wp-content/plugins/myproject/Myclasses/retrieve_2334.php 中调用未定义函数 wp_generate_attachment_metadata()

我的代码:

    ini_set( 'display_errors', TRUE );
    error_reporting( E_ALL );
    require("/Applications/MAMP/htdocs/wordpress/wp-load.php");
    $pos = strrpos($Address, "&f=");
    $loc = substr($Address, $pos + 3);

    $Address = "http://dev.piction.com/v60/" . $Address;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, $Address);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);



    $upload_dir = wp_upload_dir();
    $location= $upload_dir['basedir']. "/" . $loc;

try{
    $location= $upload_dir['path']. "/" . $loc;
    $wp_filetype = wp_check_filetype($loc, null );
    $fullpathfilename = $upload_dir['path'] . "/" . $loc;


    $fileSaved = file_put_contents($location, $output);
                if ( !$fileSaved ) {
                    throw new Exception("The file cannot be saved.");
                }
                $attachment = array(
                     'post_mime_type' => $wp_filetype['type'],
                     'post_title' => preg_replace('/\.[^.]+$/', '', $loc),
                     'post_content' => '',
                     'post_status' => 'inherit',
                     'guid' => $upload_dir['url'] . "/" . $loc
                );
                $attach_id = wp_insert_attachment( $attachment, $fullpathfilename, 0 );
                if ( !$attach_id ) {
                    throw new Exception("Failed to save record into database.");
                }
                //require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                $attach_data = wp_generate_attachment_metadata( $attach_id, $fullpathfilename );
                wp_update_attachment_metadata( $attach_id,  $attach_data );

        } catch (Exception $e) {
                $error = '<div id="message" class="error"><p>' . $e->getMessage() . '</p></div>';
            }
    echo 'Photo is uploaded';
4

2 回答 2

0

似乎您确实包含了两次文件,这可以解释为什么您会收到“已定义”错误。

尝试删除require()代码第三行中的语句并查看它是否有效,因为该文件似乎已包含在脚本的其他位置。

于 2013-02-15T10:41:27.463 回答
0

尝试更改为,require_once("/Applications/MAMP/htdocs/wordpress/wp-load.php");因为您收到已定义的通知,这可能表明您已经包含 wp-load.php。

此外,您需要取消注释该行//require ( ABSPATH . 'wp-admin/includes/image.php' );才能使用wp_generate_attachment_metadata()

于 2013-02-15T11:02:36.343 回答