Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please add yandex drive upload. #6

Open
bahramzade opened this issue Aug 18, 2020 · 0 comments
Open

please add yandex drive upload. #6

bahramzade opened this issue Aug 18, 2020 · 0 comments

Comments

@bahramzade
Copy link

$token = 'token';
// Путь и имя файла на нашем сервере.
$file = __DIR__ . '/image.png';
// Папка на Яндекс Диске (уже должна быть создана).
$path = '/uploads/';
// Запрашиваем URL для загрузки.
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/upload?path=' . urlencode($path . basename($file)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
 
$res = json_decode($res, true);
if (empty($res['error'])) {
	// Если ошибки нет, то отправляем файл на полученный URL.
	$fp = fopen($file, 'r');
 
 	$ch = curl_init($res['href']);
	curl_setopt($ch, CURLOPT_PUT, true);
	curl_setopt($ch, CURLOPT_UPLOAD, true);
	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
	curl_setopt($ch, CURLOPT_INFILE, $fp);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_exec($ch);
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
 
	if ($http_code == 201) {
		echo 'Файл успешно загружен.';
	}
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant