FTP copy a file to another place in same FTP

前端 未结 9 2061
闹比i
闹比i 2020-11-28 11:54

I need to upload same file to 2 different place in same FTP. Is there a way to copy the file on the FTP to the other place instead of upload it again? Thanks.

9条回答
  •  迷失自我
    2020-11-28 12:41

    Here's another workaround using PHP cUrl to execute a copy request on the server by feeding parameters from the local machine and reporting the outcome:

    Local code: In this simple test routine, I want to copy the leaning tower photo to the correct folder, Pisa:

    $ch = curl_init();
    $data = array ('pic' => 'leaningtower', 'folder' => 'Pisa');
    curl_setopt($ch, CURLOPT_URL,"http://travelphotos.com/copypic.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,  CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
    

    Server code (copypic.php): On the remote server, I have simple error checking. On this server I had to mess with the path designation, i.e., I had to use "./" for an acceptable path reference, so you may have to tinker with it a bit.

    $pic = $_POST["pic"];
    $folder = $_POST["folder"];
    if (!$pic || !$folder) exit();
    
    $sourcePath = "./unsortedpics/".$pic.".jpg";
    $destPath =   "./sortedpics/".$folder."/".$pic.".jpg";
    
    if (!file_exists($sourcePath )) exit("Source file not found");
    if (!is_dir("./sortedpics/".$folder)) exit("Invalid destination folder");
    if (!copy($sourcePath , $destPath)) exit("Copy not successful");
    echo "File copied";
    

提交回复
热议问题