Jump to content

new file incrementing


luminous

Recommended Posts

I've got a script that generates a a file based on data that is being updated in the database. For the sake of an example everytime the database is updated, a new file is created with the new data.  Is it possible with fopen or another function to create new files with different file names from scratch?

Link to comment
https://forums.phpfreaks.com/topic/199755-new-file-incrementing/
Share on other sites

Sorry to be a pain perhaps you can help me further as this problem is slightly more puzzling then i first thought. Basically I run a script that makes a CSV file. Once created it returns the CSV file in the form of a header:

 



	if(!is_null($file_name)) $file_name = date('Y-m-d');
	// set content type and file name then output csv content
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=\"$file_name.csv\"");
	echo $csv;

 

but I just want it to save the CSV file to a disclosed location. I'm using FOPEN at the moment but this is creating a blank file:

 

		if(is_null($file_name)) $file_name = date('Y-m-d');
	// set content type and file name then output csv content
	$date = date('M-Y');
	$orderDate = 'Orders '.$date;

	if(!file_exists($orderDate))
		{
			mkdir($orderDate);
			fopen ($orderDate.'/'.$file_name.'.csv', 'x');
		}else{

			fopen ($orderDate.'/'.$file_name.'.csv', 'x');
		}

 

How could I resolve this?

fopen creates a blank file and returns an object you can use with fread or fwrite.

 

if(is_null($file_name)) $file_name = date('Y-m-d');

// set content type and file name then output csv content


$date = date('M-Y');

$orderDate = 'Orders '.$date;


if(!is_dir($orderDate))
{
mkdir($orderDate);
}

$file = fopen ($orderDate.'/'.$file_name.'.csv', 'x');

fwrite($file, $data); //$data is a string of what you want in the file.

 

I also changed your directory check to use is_dir instead of file_exists

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.