Save generated csv file to sub folder with php - php

I have tried to store the created csv file to another subfolder instead of getting stored where the php-file is located but with no luck. Have anyone solved this by changing fopen? This is my code:
<?php
$data_array = array (
array ('test', $_POST['test'],)
);
$csv = "col1,col2 \n";//Column headers
foreach ($data_array as $record){
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('order_'.date('m-d-Y_hia').'.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
?>

Related

PHP Replace all values in the third column in a csv file

I am trying to replace some hyperlinks in a csv file, like this one:
[https://assets.suredone.com/683987/media-pics/6164307j-gabriel-61643-proguard-steel-shock-absorber-for-select-chevrolet-gmc-models.jpg. Here is my code:][1]. Here is my code:
<?php
$in_file = 'gabriel-images-urls.csv';
$out_file = 'results.csv';
$fd = fopen($in_file, "r");
$new_array= array();
$toBoot= array();
while ($data = fgetcsv($fd)) {
echo '<pre>';
if (strpos($data[2],'media-pics') !== false) {
$data[2]=str_replace('media-pics','media-photos',$data[2]);
fputcsv($fd, $data);
// echo $output;
}
}
?>
The new link for example must look like this:[1]https://assets.suredone.com/683987/media-photos/6164307j-gabriel-61643-proguard-steel-shock-absorber-for-select-chevrolet-gmc-models.jpg. The goal is he "media-pics" substring to be replaced with "media-photos". At this point nothing happens in the file. I think this is because the file is open only for reading but I am not sure.
Can you not simply do a string replacement on the whole file rather than attempting to load and process each line of the file using fgetcsv?
<?php
$srcfile='gabriel-images-urls.csv';
$outfile='results.csv';
$csvdata=file_get_contents( $srcfile );
$moddata=str_replace('media-pics','media-photos',$csvdata);
file_put_contents( $outfile, $moddata );
?>

Export HTML table to CSV file

I am making a script which gets a table from your mail and puts it into a CSV file.
This is the code I use to transfer my html table to CSV
$html = str_get_html($outputstr);
// For Excel
header('Content-type: application/ms-excel');
// Download File
header('Content-Disposition: attachment; filename=sample.csv');
$fp = fopen("php://output", "w");
// Take out empty lines
foreach($html->find('tr') as $element) {
$td = array();
foreach( $element->find('th') as $row) {
$td [] = $row->plaintext;
}
foreach( $element->find('td') as $row) {
$td [] = $row->plaintext;
}
fputcsv($fp, $td);
}
fclose($fp);
The only problem that I'm getting is that when I am opening the CSV file, some of the empty columns have a strange character:
I cannot read through with my PHP script to export it to a database
fgetcsv($handle, 1000, "\t");
How can I fix this problem?
Do I fix this by modifying the code on the part where I create the CSV file or where I read the CSV file when I'm transferring it to a MySQL database?
When I use an online html to CSV converter it works fine and I am not facing this issue then.
If there is any code needed then I'd love to share it.
Any help would be appreciated.
Have you tried setting your charset to UTF-8? Additionally, you're not setting this up as a CSV with your header, instead it is an Excel file.
header("content-type:application/csv;charset=UTF-8");

fopen and fwrite inside for loop

for($i=0;$i<$directoriesCount;$i++)
{
$fileName=$Config['path']['basePath']."language/".$directories[$i]."/"."commontest.conf";
$file = fopen($fileName,"a");
$data = "testcontent";
fwrite($file,"\n");
fwrite($file,$data);
fclose($file);
}
the $directories variable will have array values:
en_lang
fr_lang
it_lang, etc.,
in the every directory we should find the commongtest.conf file to write the content.
In my test file its writes only the first values of array for ex 1. en_lang folder file only get fwrite other files not affected.
You have an extra double quote here:
."/".commontest.conf"
should be:
."/.commontest.conf"
A foreach statement might work better here for you:
foreach($directories as $directory){
$fileName=$Config['path']['basePath']."language/".$directory."/.commontest.conf";
$file = fopen($fileName,"a");
$data = "testcontent"."\n";
fwrite($file,$data);
fclose($file);
}
This code is working for me.
I think using file_put_contents would be even better.
foreach($directories as $directory){
$fileName=$Config['path']['basePath']."language/".$directory."/.commontest.conf";
$data = "testcontent"."\n";
file_put_contents($fileName,$data);
}

File empty after fputcsv

I created a file with fputcsv
$handle = fopen('php://output', 'w+');
// Add the header of the CSV file
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
// Query data from database
// Add the data queried from database
foreach ($results as $result) {
fputcsv(
$handle, // The file pointer
array(...), // The fields
';' // The delimiter
);
}
file_put_contents('mycsv.csv',fgetcsv($handle), FILE_APPEND);
fclose($handle);
....
I want to save the output on mycsv.csv but the file is empty
The php://output is a stream that works like echo or print. It means, you are writing in the standard output (maybe, your console or the browser).
If you want to write your content in a csv file, try to open this file or create it using PHP directly, instead of use the file_put_contents.
$handle = fopen("mycsv.csv","wb");
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
//...put your code
rewind($handle);//optional.it will set the file pointer to the begin of the file

Create CSV and save on local/server automatically

I need to create a CSV file in PHP and that created file should be saved on local/server rather than asking to save.
For the PHP versions later than 5.1.0 you can use the built-in CSV functions.
<?php
$data_array = array (
array ('col1','col2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$fp = fopen('csvfile.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
This code will do the trick
$data_array = array (
array ('1','2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$csv = "col1,col2 \n";//Column headers
foreach ($data_array as $record){
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('csvfile.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
but be sure about permission of csvfile so your script can create or write on it

Categories