如何将网址中的照片添加到wordpress?

时间:2013-01-19 01:33:27

标签: wordpress

我的代码从网址检索照片,并使用file_put_contents将它们添加到当前目录,但我需要将它们添加到/ wordpress / wp-content / uploads,其中wordpress保存手动上传的文件。

$Address = "www.xxx.com/" . $file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$url = curl_exec($ch);
curl_close($ch);
$location = "/wordpress/wp-content/uploads/2013/a.jpg"; 
file_put_contents($location, file_get_contents($url));
if(! function_exists('wp_upload_dir'))
                {
                    define('WP_USE_THEMES',false);
                    require 'wordpress/wp-load.php';
                }
    $upload_dir = wp_upload_dir(); 
    echo $upload_dir['path'];
    echo $upload_dir['url'];
    echo $upload_dir['subdir']; 

我甚至在索引文件中复制了以下部分,但它没有解决问题

  if(! function_exists('wp_upload_dir'))
                    {
                        define('WP_USE_THEMES',false);
                        require 'wordpress/wp-load.php';
                    }

1 个答案:

答案 0 :(得分:1)

一些想法:

$uploads = wp_upload_dir();
$upload_path = $uploads['baseurl']; // get uploads dir

rename("a.jpg", trailingslashit($upload_path) . "a.jpg"); // move

参考:http://codex.wordpress.org/Function_Reference/wp_upload_dir
参考:http://php.net/manual/en/function.rename.php

<强>更新

我猜你在WP之外工作。尝试在代码之前获取wp_upload_dir()和所有WP功能:

if ( ! function_exists('wp_upload_dir'))
{
    define('WP_USE_THEMES', false);
    require PATH_WP_DIR . '/wp-load.php';
    // ex: require '/wordpress/wp-load.php';
}