如何通过php将图像从域上传到同一服务器下的另一个域?

时间:2013-04-24 09:56:41

标签: php image file file-upload

我想将图片从www.abc.com/upload/abc.jpg文件夹移动到www.def.com/upload/abc.jpg。这两个网站有相同的godaddy cpanel,但他们的ftp细节不同..我怎么能移动那个图像?

  <?php
   move_uploaded_file($_FILES["file"]["tmp_name"],
  "C:\inetpub\vhosts\def.com\upload\\" . $_FILES["file"]["name"]);

 ?>

它不起作用。但是当我使用ftp_put()函数时它会起作用,这个函数需要ftp用户名和密码..我想移动没有ftp细节的图像。我怎么能移动它并且有可能吗?

1 个答案:

答案 0 :(得分:1)

试试这个解决方案:

# Source domain code..
$save_dir = 'C:\inetpub\vhosts\abc.com\upload\abc.jpg' ;# Temporary save file in your source domain..
$url = "http://www.def.com/copy_upload_file.php"; # destination domain URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    "file"=> "@".$save_dir,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);

在您的其他域中添加此文件..

# Destination domain file called as copy_upload_file.php
$path = 'C:\inetpub\vhosts\def.com\upload\'; # Write full path here
if(isset($_FILES["file"]))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "0";
    }
    else
    {
        if(is_uploaded_file($_FILES["file"]["tmp_name"]) && move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]))
        {
            echo "1";
        }
        else {
            echo "0";
        }
    }
}
else
{
    echo "0";
}