我正在尝试从网络服务接收文件并将它们保存到 wordpress 但我有以下问题,
- 一些文件将被上传,但它们已损坏,我无法打开它们,尽管它们的 web 服务地址是正确的。
- 它显示了以下错误(尽管它显示了这些错误,但还是上传了文件,这很好: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';