1

I have an issue. I could not write my array of data into a file using PHP. I am explaining my code below.

<?php
$result=array(array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'23','subcat_name'=>'pakhal'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'22','subcat_name'=>'wine'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'24','subcat_name'=>'fuddy'),array('cat_id'=>'4','cat_name'=>'Continetal','subcat_id'=>'25','subcat_name'=>'cont'));
$handle = fopen("file.txt", "w");
fwrite($handle, $result);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
?>

Here I need to download also that file. Here I can download the file but this fwrite() expects parameter 2 to be string array given in is written inside that file and no data there. I need to write all of my array of data into that file and download it. Please help me to resolve this issue.

4
  • So convert array to string, with implode for example Commented May 24, 2017 at 13:31
  • Can you modify this please.? Commented May 24, 2017 at 13:32
  • What is the expected contents of a file? Commented May 24, 2017 at 13:32
  • I need to get that array values means output ofecho json_encode($result) values. Commented May 24, 2017 at 13:37

3 Answers 3

3

If you explicit need to put array in file you can do it with this

fwrite($handle, print_r($result, true));

or you can convert it to json, like this

fwrite($handle, json_encode($result));

And later when you read the file you can just undo the action with json_decode($your_file_content)

Sign up to request clarification or add additional context in comments.

Comments

0

First you have to process the array and format it, then write to file as below:

$result=array(array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'23','subcat_name'=>'pakhal'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'22','subcat_name'=>'wine'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'24','subcat_name'=>'fuddy'),array('cat_id'=>'4','cat_name'=>'Continetal','subcat_id'=>'25','subcat_name'=>'cont'));
$handle = fopen("file.txt", "w");
$finalData = '';
foreach ($result as $resultData) {
    foreach ($resultData as $key => $value) {
        $finalData .= ($key. ': '. $value.PHP_EOL);//you can play with this data in any way you want now
    }

}
fwrite($handle, $finalData);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;

OUTPUT:

cat_id: 2
cat_name: spirit
subcat_id: 20
subcat_name: pizza
cat_id: 3
cat_name: Food
subcat_id: 23
subcat_name: pakhal
cat_id: 2
cat_name: spirit
subcat_id: 22
subcat_name: wine
cat_id: 2
cat_name: spirit
subcat_id: 20
subcat_name: pizza
cat_id: 3
cat_name: Food
subcat_id: 24
subcat_name: fuddy
cat_id: 4
cat_name: Continetal
subcat_id: 25
subcat_name: cont

Comments

0

Writing to the file for that doesn't make sense, unless you are doing something with that file that you aren't showing us.

<?php
$result=array(array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'23','subcat_name'=>'pakhal'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'22','subcat_name'=>'wine'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'24','subcat_name'=>'fuddy'),array('cat_id'=>'4','cat_name'=>'Continetal','subcat_id'=>'25','subcat_name'=>'cont'));

header('Content-Type: application/json');
header('Content-Disposition: attachment; filename=file.json');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$encoded = json_encode($result);

header('Content-Length: ' . strlen($encoded));

echo $encoded;

exit;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.