1

i save my site file within a ftp server. In following codes users can access ftp files via ajax request.

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-download.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 want to know is this possible delete $downloaded_file and $output_file after user access the file?

Thanks.

a.sadegh63
  • 56
  • 1
  • 8

2 Answers2

1

There's no need for you to save the file to a temporary physical file on your web server.

You can send it directly to the client.

See Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Thank you Martin, finally i solve my problem with your Help. I want to share my code, maybe it will help others.

With following codes you can download files from ftp server and convert non PDF file into PDF with PHP Imagick class and view file in browser windows

HTML Element

<a onclick="attach('354.tif')"><i class="fa fa-paperclip" aria-hidden="true"></i></a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- jQuery Modal -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />

New ftp-download.php File

<?php

    $ftp_file = "/".$_REQUEST["q"];
    $ftp_file_ext = pathinfo($ftp_file, PATHINFO_EXTENSION);

    $pdfFile = get_ftpfile_blob($ftp_file);


    if (strcasecmp($ftp_file_ext, 'pdf') != 0) {
        $pdfFile = convert_tif_to_pdf($pdfFile);    
    }

function get_ftpfile_blob($file) {

    require_once $_SERVER['DOCUMENT_ROOT'].'/config/ftpconfig.php'; // this file contain ftp configuration

        $conn_id = ftp_connect($ftp_server, $ftp_port); 
        ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        ftp_pasv($conn_id, true);

        ob_start();
        $result = ftp_get($conn_id, "php://output", $file, FTP_BINARY);
        $data = ob_get_contents();
        ob_end_clean();

        ftp_close($conn_id);

        if ($result) {
            return $data;
        } else {
            die('');
        }
}

function convert_tif_to_pdf($inputFile) {
    $document = new Imagick();
    $document->readImageBlob($inputFile);
    $document->setFormat("pdf");
    return $document->getImagesBlob();  
}

    echo base64_encode($pdfFile);
    die();
?>

New jquery

function attach(fileLink) {
    $.get("/ajax/ftp-download.php", 
        {
            q: fileLink
        } , 
        function(data){
            // Display the returned data in browser
            if (data == "") {
                var modalWindow = '<div id="saveMsg" class="modal">' +
                                  '<p>File Not Found!</p>' +
                                  '<a href="#" rel="modal:close"></a>' +
                                  '</div>';
                $('body').append(modalWindow);
                $('#saveMsg').modal({fadeDuration: 100});
                return;
            }
            var link = document.createElement('a');
            link.href = 'data:application/pdf;base64,' + data;
            document.body.appendChild(link);
            window.open(link);
        }
    );
}
a.sadegh63
  • 56
  • 1
  • 8