我正在使用此代码尝试将服务器中的图像上载到wordpress安装的媒体库。
<?php
include("../../../wp-blog-header.php");
include("../../../wp-admin/includes/file.php");
include("../../../wp-admin/includes/media.php");
$id = 0;
$image = $_SERVER[\'DOCUMENT_ROOT\'] . "/image01.jpg";
$array = array( //array to mimic $_FILES
\'name\' => basename($image), //isolates and outputs the file name from its absolute path
\'type\' => \'image/jpeg\', //yes, thats sloppy, see my text further down on this topic
\'tmp_name\' => $image, //this field passes the actual path to the image
\'error\' => 0, //normally, this is used to store an error, should the upload fail. but since this isnt actually an instance of $_FILES we can default it to zero here
\'size\' => filesize($image) //returns image filesize in bytes
);
media_handle_sideload($array, $id); //the actual image processing, that is, move to upload directory, generate thumbnails and image sizes and writing into the database happens here
?>
我可以看到,这段代码获取位于该路径中名为“image01.jpg”的图像,然后将图像放入/wp contents/uploads/。但是,当我转到wordpress中的管理面板查看媒体库中的图像时,我看不到图像。此外,此图像的wp\\u帖子中没有任何行。
UPDATE:
这次我使用了以下代码:
$result = media_handle_sideload($array, $id);
echo "Finish media handle sideload<br>";
if ( ! is_wp_error( $result ) ) {
echo "Result<br>";
print_r($result);
} else {
echo "Error<br>";
print_r($results);
}
echo "Finish script<br>";
没有显示执行情况。我看不到消息“Finish media handle sideload”。但是,文件将从“$image”中指定的路径移动到“wp content/uploads”。请注意,文件是移动的,而不是复制的,因此如果我执行脚本而不将图像再次复制到源(路径为$image),我会看到一些关于filesize函数的警告,如下所示:
警告:filesize():C:/Users/dlopez/Documents/Development/web folders/image01的stat失败。C中的jpg:\\Users\\dlopez\\Documents\\Development\\web folders\\wp\\U test\\wp content\\plugins\\pods csv importer\\upload。php在线16