How to stop foreach loop from printing duplicate data? - php

I am trying to generate unique CSV files from the csv data that I have using the following loop.
$k =1;
foreach ($csv_tbl as $_csv) {
$filename = "Agent_" . $k . ".csv";
$file_path = "agents/$filename";
file_put_contents($file_path, $_csv);
if (file_exists($_csv)) {
header('Content-Description: File Transfer');
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=' . $filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($_csv));
readfile($_csv);
die();
}
$k++;
}
The first file generated is perfect and only has the data it should have i.e the first csv table. The second file has both the first and the second tables this goes on for all my 21 files which means the 21st file will have all the tables.
Visual example of the first two files.
How can I prevent duplicates tables from my csv file?

As your foreach is printing duplicates , it means that your array $csv_tbl contains duplicate values, you can remove duplicate values from array using array_unqiue But also by looking at the screenshots i can see that the callid is different for every record. do your csv contain duplicates?::
$k =1;
$csv_tbl = array_unique($csv_tbl);
foreach ($csv_tbl as $_csv) {
$filename = "Agent_" . $k . ".csv";
$file_path = "agents/$filename";
file_put_contents($file_path, $_csv);
if (file_exists($_csv)) {
header('Content-Description: File Transfer');
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=' . $filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($_csv));
readfile($_csv);
die();
}
$k++;
}

Related

PHP headers to serve ZIP. It downloads but is corrupt... why?

Good morning,
I'm working on serving a zip file through the PHP headers, which downloads, but every time I try to extract it shows that the zip is corrupt... I need a 2nd pair of eyes to look over this... what am I missing? Thank you so much!
$file_download = 'example';
if (isset($file_download)) {
$file = 'path/to/file/'.$file_download.'.zip';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
echo 'File does not exist!';
}
}

Get the file correctly after uploading with md5

The following is part of the code in upload.php of OpenCart:
$file = $filename . '.' . md5(mt_rand());
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $file);
// Hide the uploaded file name so people can not link to it directly.
$this->load->model('tool/upload');
$json['code'] = $this->model_tool_upload->addUpload($filename, $file);
For the coding '$this->model_tool_upload->addUpload($filename, $file)', it will insert the data like this:
The field 'code' is generated by the the coding 'sha1(uniqid(mt_rand(), true))'.
And the new file will inserted into the folder:
I would like to upload image and then display it. In this situation, how can I get the file path correctly using PHP?
Also, can someone explain why the filename is appended with the md5 string? Is it only used for hide the uploaded file name and what is the field 'code' used for?
UPDATE
Just find the answer in one of the source file:
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file, 'rb');

Date issue in csv

Export to csv is working. But date format is ######## for date 11/11/2014. The result is
11 oct,14 for 11/7/2014. Error is during 2 digits for date. Could somebody solve the issue?
$fp = fopen('reports-export/publications_report-'.$from.'.csv', 'w');
fputcsv($fp, $header);
fputcsv($fp, array(date('M d, Y',strtotime($data_print->pfile_date))));
fclose($fp);
$file = 'reports-export/publications/publications_report-'.$from.'.csv';
if (file_exists($file)) {
//set appropriate headers
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
//read the file from disk and output the content.
readfile($file);

Nothing gets printed in the doc using PHPWord

I am using PHPWord to download docx files in php. But nothing gets printed in the file if I try to download it. But the contents get displayed in the file which gets saved on the server. Below is the code which I have used. Can anyone please tell me what the issue is.
<?php
include "../includes/config.inc.php";
include '../PHPWord.php';
// Create a new PHPWord Object
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
$section->addText('CANDIDATES DETAILS');
$filename='test';
$file=$filename.'.docx';
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('/form_doc/'.$filename.'.docx');
//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
Thanks in advance.
Regards,
Neha
Your should set path to file to readfile() and filesize() functions:
$file = $filename.'.docx';
$filepath = '/form_doc/' . $file;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($filepath);
//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);

http header for downloading Microsoft Word and Excel files

I can download my microsoft word successfully if I named it in the filename by default. But if I use $variables to name it. The document extension will be unknown.
Sample:
$No = 1;
$Name = 'John';
$Test = 'Science';
//Download header
$document->save($doc);
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($doc));
ob_clean();
flush();
readfile($doc);
So if i rename my filename as variables. The file download will be without the docx extension. Anyone can advise?
Thanks
Correct headers are
for Excel (*.xlsx):
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
for Word (*.docx):
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
Change this
header('Content-Type: application/msword');
to
header('Content-Type: application/octet-stream');
EDIT:
And change
header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
to
header("Content-Disposition: attachment; filename=\"{$No}_{$Name}_{$Test}.docx\"");
The correct use of that header is:
Content-Disposition: attachment; filename="fname.ext" note that if the name contains spaces it must be quoted
See RFC6266 section 5. Examples

Categories