0

The task - there is a direct link to download the file, you need to use php methods to download this file and save it on the server. There is code, but it does not load the file, but only creates a new one.

<?php
$token = 'token';
// file from yandex disk.
$yd_file = '/FermerPrice/test.jpeg/';

// save this.
$path = download;

//get array with link
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);

// $res['href'] - link download
$res = json_decode($res, true);


if (empty($res['error'])) {
    $file_name = $path . '/' . basename($yd_file);
    $file = @fopen($file_name, 'w');

    $ch = curl_init($res['href']);
    curl_setopt($ch, CURLOPT_FILE, $file);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    curl_close($ch);
    fclose($file);
    echo "Good.";
}

?>
adRaduga
  • 1
  • 1

1 Answers1

0

You can use this:

file_put_contents("localfilename", file_get_contents('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file)));
Ramin Rezazadeh
  • 326
  • 1
  • 12