duan5991518 2019-01-25 13:02
浏览 101

在服务器目录上保存PDF Curl响应

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);
?>
  • 写回答

1条回答 默认 最新

  • douzhenggui8171 2019-01-25 14:20
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题