Suresh Kamal
Suresh Kamal

Reputation: 9

How to upload an image from a domain to another domain under same server by php?

I want to move an image from the folder www.abc.com/upload/abc.jpg to www.def.com/upload/abc.jpg. Here these two website have same cpanel of godaddy but their ftp details are different.. how can I move that image ?

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

 ?>

it is not working . But it will work when i am using ftp_put() function, this function needs ftp username and password.. i want to move the image without ftp details. how can i move it and is it possible?

Upvotes: 0

Views: 3072

Answers (1)

Pankaj Dadure
Pankaj Dadure

Reputation: 677

Try this solution:

# 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);

Add this file in your another domain..

# 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";
}

Upvotes: 1

Related Questions