Search code examples
phpfwritefputcsv

Writing in a file make my program load really slowly


I am trying to write csv files with fwrite/fputcsv in a loop but when I use those functions, they write in the first file and then the tab loads infinitely and doesn't do anything else.
I know those functions are the problem because when I remove them the rest of the code runs properly.

Here's my code :

<?php

$auth = '';
$date = getdate(); //Needed for the Trade lists files

$path = array(
    'Some_folder/file1.csv',
    'Some_folder/file2.csv',
    'Some_folder/file3.csv',
    'Some_folder/file4.csv'
);//Full path the files need to be in
$file = array(
    pathinfo($path[0])['basename'],
    pathinfo($path[1])['basename'],
    pathinfo($path[2])['basename'],
    pathinfo($path[3])['basename']
);//The names the files will have
$fp = array(
    fopen($file[0], 'w+'), 
    fopen($file[1], 'w+'),
    fopen($file[2], 'w+'),
    fopen($file[3], 'w+')
);// Files that are create by fopen

for($i = 0; $i < 4; $i++) {
    fputcsv($fp[$i], $file);
    echo 'test';
    $size = filesize($path[$i]);
    $cheaders = array('Authorization: Bearer ' .$auth.'[Private access token]',
    'Content-Type: application/octet-stream',
    'Dropbox-API-Arg: {"path":"/'.$file[$i].'", "mode":"add"}');

    $ch = curl_init('[Private information]');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp[$i]);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    curl_close($ch);
    fclose($fp[$i]);
}

?>

fputcsv and fwrite do write in the first file (file1.csv) but they make the program stuck in an infinite loading. The console doesn't say anything since I can't load the page and I don't know what else to try/do. I tried looking for a solution, unfortunately I didn't find it so here I am. I'll still look for an awnser but if someone has a solution, I'll take it too.

Thanks to all those who will try to help.

EDIT: It finally loaded but it took a really long time since it started loading approximately an hour ago. Is there something I did wrong for my program to be so slow to write so slowly in a file ?


Solution

  • I would suggest you to split the task in two sections. First write the files in your server using fopen in w mode. After writings, use another loop for reading the files using fopen in r mode for using in your cURL to upload in the server. Using the same handler for writing and uploading might cause the lagging problem.

    You can use curl_multi_init for handling multiple cURL requests asynchronously. Here is also a post on it.