Search code examples
phpfilecopyphp-stream-wrappers

File copied from a URL is truncated


I have a problem copying a file. My code:

$file = "https://www.ilportaleofferte.it/portaleOfferte/resources/opendata/csv/offerteML/2019_1/PO_Offerte_G_MLIBERO_20190130.xml";

$newfile = $_SERVER['DOCUMENT_ROOT'] . '/input/PO_Offerte_G_MLIBERO_20190130.xml';

if(copy($file, $newfile)) {
    echo "salvato<br>";
} else {
    echo "ERROR inport file PO_Offerte_".$data.".".$ext."<br>";
    die;
}

copy() give true, the file is created but some lines at the end of the file are missing... the file is 3.6MB and 0.3 at the end of the file are missing...

If I download the file manually all is fine so the source is complete...

I actually have the same problem if I get the file content with file_get_contents() and I try to save it in a file using file write function...

I do not think upload_max_filesize and post_max_size actually are involved in copy() but they are 20MB setted

any tip?

thanks


Solution

  • I was able to use file_get_contents and forcing HTTP/1.1 protocol:

    $context = stream_context_create([
        'http' => [
          'protocol_version' => '1.1',
          'header' => 'Connection: Close'
        ],
    ]);
    $content = file_get_contents('https://www.ilportaleofferte.it/portaleOfferte/resources/opendata/csv/offerteML/2019_1/PO_Offerte_G_MLIBERO_20190130.xml', false, $context);
    file_put_contents('document.xml', $content);
    

    Having said that, I would recommend using CURL:

    $ch = curl_init();
    $curlopts = array();
    $curlopts[CURLOPT_RETURNTRANSFER] = true;
    $curlopts[CURLOPT_VERBOSE] = true;
    $curlopts[CURLOPT_URL] = 'https://www.ilportaleofferte.it/portaleOfferte/resources/opendata/csv/offerteML/2019_1/PO_Offerte_G_MLIBERO_20190130.xml';
    curl_setopt_array($ch, $curlopts);
    $content = curl_exec($ch);
    file_put_contents('document.xml', $content);
    curl_close($ch);