AtarC5
AtarC5

Reputation: 413

Saving PDF Curl response on server directory

I'm getting and downloading a .pdf from an URL. It's downloading directly on my PC through the Curl request. How to save this in a directory at the server where the page is running?

I'm able to use PHP and JS, i've tried many things even combine the two languages but it seems only works the curl request, and it doesnt save that in the directory

<body>
    <button id="boton">CLICK ME</button>
        <?php
    $name = 'file'; 
    $file_downloaded = curl_init(); 
    curl_setopt($file_downloaded, CURLOPT_URL, 'The url'); 
    //curl_setopt($file_downloaded, CURLOPT_HEADER, true);
    curl_setopt($file_downloaded, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($file_downloaded, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($file_downloaded, CURLOPT_AUTOREFERER, true);
    $file_downloaded = curl_exec($file_downloaded); 
    if(!curl_errno($file_downloaded)) 
    {
      header('Content-type:application/pdf'); 
      header('Content-Disposition: attachment; filename ="'.$nuevo_nombre.'.pdf"'); 
      echo($file_downloaded);
      exit();
    }else
    {
      echo(curl_error($file_downloaded)); 
    }
    ?>

    <script>
        $("#boton").click(function () { 
        var archivo = '<?php echo($file_downlaoded) ?>';
        var data = new FormData();
        data.append("data" , archivo);
        var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
        xhr.open( 'post', '/my/directory', true );
        xhr.send(data);
    });

    </script>
</body>

It's downloading on my local PC, and it's expected to download on the path /my/directory of the server.

EDIT

        <?php
    $nuevo_nombre = 'remito'; //asignamos nuevo nombre
    $archivo_descarga = curl_init(); //inicializamos el curl
    curl_setopt($archivo_descarga, CURLOPT_URL, 'URL'); 
    //curl_setopt($archivo_descarga, CURLOPT_HEADER, true);
    curl_setopt($archivo_descarga, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($archivo_descarga, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($archivo_descarga, CURLOPT_AUTOREFERER, true);
    $resultado_descarga = curl_exec($archivo_descarga); 
    file_put_contents('/path/to/file.pdf', $resultado_descarga);
?>

Upvotes: 2

Views: 6113

Answers (1)

AtarC5
AtarC5

Reputation: 413

That was solved with the file_put_contents() function, and there was also an issue with permissions on the directory folder.

function Download($endpoint) {
        $URL = 'URL';
        $fileName= 'name1';
        $path = 'testDirectory/'.$filename.'.pdf';
        $file_download= curl_init();

        curl_setopt($file__download, CURLOPT_URL, $URL.$endpoint);
        curl_setopt($file__download, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($file__download, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($file__download, CURLOPT_AUTOREFERER, true);
        $result= curl_exec($file__download);
        file_put_contents($path, $result);
}

Upvotes: 3

Related Questions