Whenever the function in codeigniter will run,I want to save the timestamp in a text file. How can I achieve that?
public function updateData(){
Running this function each time it will save the timestamp in a text file.
}
you can find more info and examples here -> https://www.w3schools.com/php/php_file_open.asp
public function updateData(){
# Running this function each time it will save the timestamp in a text file.
$path2file = "/docs/";
$fileName = "timestamp.txt";
$data = time()."".PHP_EOL; // PHP_EOL - new line
$fp = fopen($path2file.$filename, 'a');
fwrite($fp, $data);
}
Related
I am trying to implement Box/Spout into a project of mine and I want to export some data.
The thing is that when I tried to make a test file to get downloaded it doesn't work, it's just a simple white page and the .xlsx download is not triggered.
Below is my code:
<?php
require_once 'vendor/box/spout/src/Spout/Autoloader/autoload.php';
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;
function xlsx(){
$writer = WriterEntityFactory::createXLSXWriter();
// $writer = WriterEntityFactory::createODSWriter();
// $writer = WriterEntityFactory::createCSVWriter();
$writer->setShouldUseInlineStrings(true); // default (and recommended) value
$writer->setTempFolder($customTempFolderPath);
$fileName = 'test.xlsx';
// $writer->openToFile($filePath); // write data to a file or to a PHP stream
$writer->openToBrowser($fileName); // stream data directly to the browser
$cells = [
WriterEntityFactory::createCell('Carl'),
WriterEntityFactory::createCell('is'),
WriterEntityFactory::createCell('great!'),
];
/** add a row at a time */
$singleRow = WriterEntityFactory::createRow($cells);
$writer->addRow($singleRow);
/** add multiple rows at a time */
$multipleRows = [
WriterEntityFactory::createRow($cells),
WriterEntityFactory::createRow($cells),
];
$writer->addRows($multipleRows);
/** Shortcut: add a row from an array of values */
$values = ['Carl', 'is', 'great!'];
$rowFromValues = WriterEntityFactory::createRowFromArray($values);
$writer->addRow($rowFromValues);
$writer->close();
}
This is a sample taken from the official documentation: https://opensource.box.com/spout/
Is there something I missed? What I am doing wrong?
It looks good to me. You just need to call the xlsx() function from your index.php to actually trigger the download.
To test if it really works, just move the contents of the xlsx() function directly inside index.php; that could help debug your issue
I am trying to dynamically create migration files, and I encounter a problem with the fopen function, whenever I call the function with a dynamic string for the name of the file I get the following error:
"failed to open stream: Invalid Argument.
now the wierd thing is, when I take the file_name string that caused the error, and put it fixed in the fopen function, it works and creates the file.
here is the part of the code that fails:
public function add_tables($from, $to) {
$migration_name = $this->generate_migration_name($from, $to);
$migration_file = fopen($migration_name, "w") or die("Unable to open file!");
$migration_content = "...";
fwrite($migration_file, $migration_content);
}
public function generate_migration_name($from, $to) {
$current_date = Date('Y_m_d_His');
return $current_date."_create_msl_".$from."_to_".$to."_table.php";
}
am I doing anything wrong?
UPDATE: the $to and $from are two strings im reading from a text file using fgets function. example for string that failed:
2017_02_22_154148_create_msl_yeshut_yatzran_to_mimshak_table.php
when I put it fixed in the fopen function the file is created successfully.
You may have spaces in your filename. You can remove them by using trim:
public function generate_migration_name($from, $to) {
$current_date = Date('Y_m_d_His');
return $current_date."_create_msl_".trim($from)."_to_".trim($to)."_table.php";
}
The file you are trying to create is not in your server or check file permissions
In my controller, there are three methods:
NOTICE: I use Codeigniter v3...
public funtion index(){
$data['code'] = $this->generate_random_string();
$myfile = fopen("C:\wamp\www\write.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data['code']);
fclose($myfile);
$this->load->view('path_to_view/view1', $data);
}
//$param is numeric
public funtion send($param){
$data['code'] = $this->generate_random_string();
$myfile = fopen("C:\wamp\www\write.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data['code']);
fclose($myfile);
$this->load->view('path_to_view/view2', $data);
}
public function generate_random_string(){
$this->load->helper('string');
$date = new DateTime();
return random_string('sha1', 40) . $date->getTimestamp();
}
in the first and second method, I generate a random string, assign it to $data['code'] and also save it in a file (write.txt), and then load a view and echo $data['code'].
The problem is: in first method, stored $data['code'] in file and printed one in view file is the same, but in second method (send) they are different!!!
Another thing to say is: when I add below statement in second method and print value in controller (instead of view), everything will be ok:
var_dump($data['code']);
I could not understand what happened! There is not any special code to affect these, just some loading view, header, footer.
Could anyone help to find possible issues or guess what is ?
thanks.
SOLVED: I check all view file, I couldn't find related code to this issue. But, when I comment below line in header, the problem is solved!
Could anyone explain about this? just a link element cause problem!
I am reading two excel file, using php-excel-reader (From this)
After reading two files of 1st row, I am comparing it. If they are same then I am appending contain of on file to other. To write the file I am using this
Now for doing this I want to close one file, but that function is not available in php-excel-reader
here is my code
compare file
{
$data = new Spreadsheet_Excel_Reader($filepath);
$data1 = new Spreadsheet_Excel_Reader($destinationfilepath);
}
unset($data);
unset($data1);
if($flag==0)
{
$excel = new ExcelWriter($destinationfilepath);
// read the source file
$finalarray= array();
for($m=1;$m<$sourcefilerowcount;$m++)
{
$charvalue='A';
$temprow=$m+1;
for($n=0;$n<$destinationcolnum;$n++)
{
$data = new Spreadsheet_Excel_Reader($filepath);
$finalarray[$n]=$data->val($temprow,$charvalue);
$charvalue++;
}
print_r($finalarray)."<br/>";
$excel->writeLine($finalarray);
}
There is no need to explicitly call close() function, because the file is automatically closed in the load() method. If you look at Excel2007.php, where PHPExcel_Reader_Excel2007 is defined, you'll see:
public function load($pFilename)
{
...
$zip = new ZipArchive;
$zip->open($pFilename);
...
$zip->close();
return $excel;
}
Just unset your PHPExcel_Reader object, and the data will be removed from memory:
$objReader = PHPExcel_IOFactory::createReader('Excel2003XML');
$objPHPExcel = $objReader->load("Excel2003XMLTest.xml");
...
unset($objPHPExcel);
unset($objReader);
Which flag should i use for create a file if not exists? Please not that i'll close the point right after fopen() because the "hard part" (decoding the encrypted content) is carried by load() function (decoding logic is not shown):
Class MyClass
{
protected $filename, $data;
public function __construct($filename)
{
$this->filename = $filename;
// Create if not exists
if(!file_exists($this->filename))
{
$fp = fopen($this->filename, '');
fclose($fp);
}
$this->load();
}
public function load()
{
$data = file_get_contents($this->filename);
$this->data = $data === false ? array() : $data;
}
}
wb is about all you'd need. Open a file for writing, truncate any file which already exists, set the file pointer to the start of this new file, and enable binary mode (which prevents PHP from translating line-ending characters on certain platforms like Windows).
'a+', here manual. Ensure that permissions for the dir is ok.