0

我的代码从 url 中检索照片并使用 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';
                    }
4

1 回答 1

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';
}
于 2013-01-19T01:52:09.890 回答