使用cURL将文件(可以是jpg,css,js ...)保存到我的服务器并保留目录结构

To make it simple:

  1. I have an image file ex: https://www.example.com/a/b/c/image.jpg
  2. I want to use curl to download and save the image on my server but I want to keep the directory structure, so I want to save the file on my server to a/b/c/image.jpg , so the directories must also be created if they do not exist already
  3. If the file already exist to not download

Hope someone with more experience can help because I have tried 2 days on this an nothing.

For now I only have the CURL function that is working good:

    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL, $urltoget );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    $tempsavefile = curl_exec( $curl );
    curl_close( $curl );