@zeni1agent

Как скачать файл на сайте wordpress через метод post?

я написал вот такой код для скачивания файлов

<?php 
	if ( get_post_meta(get_the_ID(), 'test_file', true) ) : 
	$GET11 = wp_get_attachment_url ( get_post_meta(get_the_ID(), 'test_file', true) );
	
?>
<form method="POST">
		<input type="submit" value="downlode" name="test_downlode">
</form>
<?php
if ($_POST['test_downlode']){
 
$number_downlode = get_post_meta($postID, 'downlode_registration', true);
$number_downlode++;  
update_post_meta($postID, 'downlode_registration', $number_downlode);


$file = ($GET11);
 if (file_exists($file)) {
    if (ob_get_level()) {
      ob_end_clean();
    }
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
}
	endif;
	?>


Но она не работает для ссылок
Есть ли способ с помощью PHP скачать файл из базы данных wordpress?
  • Вопрос задан
  • 508 просмотров
Решения вопроса 1
Но она не работает для ссылок

Я думаю что для ссылок вам нужно использовать хук template_redirect

Вот код который возвращает у меня файл по ссылке
add_action('template_redirect','pchv_ew_template_redirect');

function pchv_ew_template_redirect() {
  if ($_SERVER['REQUEST_URI']=='/downloads/result.csv') {

	$opt = unserialize(get_option('import_info')); // Получаум опцию

	if (!$opt['resultfile']) { exit; }

    header("Content-type: application/x-msdownload",true,200);
    header("Content-Disposition: attachment; filename=result".date("_d.m.y_H:i:s").".csv");
    header("Pragma: no-cache");
    header("Expires: 0");
	readfile($opt['resultfile']);
    exit();
  }
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
san_jorich
@san_jorich
Творческий кодер
Брррр... Можно уточнить вопрос? Вы хотите закачать на сервер, скачать с сервера?))
Ответ написан
deniscopro
@deniscopro Куратор тега WordPress
WordPress-разработчик, denisco.pro
А какой-нибудь WordPress Download Manager уже пробовали?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы