Я сохраняю файл моего сайта на FTP-сервере. В следующих кодах пользователи могут получить доступ к файлам ftp через запрос ajax.
HTML
<a onclick="attach('354.tif')"><i class="fa fa-paperclip" aria-hidden="true"></i></a>
JavaScript
function attach(fileLink) {
var xmlhttp = new XMLHttpRequest();
var ajaxResponse;
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
ajaxResponse = this.responseText;
var response = JSON.parse(ajaxResponse);
if(response.location){
window.location.href = response.location;
}
}
};
xmlhttp.open("GET", "/ajax/ftp-download.php?q=" + fileLink, true);
xmlhttp.send();
}
ftp-загрузка. php
<?php
define('__ROOT__',dirname(dirname(__FILE__)));
require_once __ROOT__.'/config/ftpconfig.php';
require_once __ROOT__.'/functions/file-process/tiff-to-pdf.php';
$ftp_file = '/'.$_REQUEST["q"];
$ftp_file_ext = pathinfo($ftp_file, PATHINFO_EXTENSION);
// set up basic connection
$conn_id = ftp_connect($ftp_server, $ftp_port) or die(err_handler(ftp_connect_error));
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
$downloaded_file = $_SERVER['DOCUMENT_ROOT']."/user-files/".mt_rand(1000, 999999).".".$ftp_file_ext;
// try to download $ftp_file and save to $local_file
if (ftp_get($conn_id, $downloaded_file, $ftp_file, FTP_BINARY)) {
if (strcasecmp($ftp_file_ext,"tif") == 0 or strcasecmp($ftp_file_ext,"tiff") == 0) {
$output_file = "/user-files/".convert_tif_to_pdf($downloaded_file);
} else {
$output_file = "/user-files/".basename($downloaded_file).PHP_EOL;
}
header('Content-Type: application/json');
echo json_encode(['location'=>$output_file]);
}
// close the connection
ftp_close($conn_id);
?>
I Хотите знать, возможно ли это удалить $downloaded_file
и $output_file
после доступа пользователя к файлу?
Спасибо.