Page 1 of 1

PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 4:19 pm
by mikeashfield
Hi guys,

I've read that you can generate files on the fly with PHP such as PDF ect, but can it handle any file extension? I'd like to create custom .ics files (iCal invites for Mac). They're really straight forward and are formated like this:

[syntax]BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
X-WR-CALNAME:1012
METHOD:PUBLISH
PRODID:-//Apple Inc.//iCal 5.0.1//EN
BEGIN:VTIMEZONE
TZID:Europe/London
BEGIN:DAYLIGHT
TZOFFSETFROM:+0000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
DTSTART:19810329T010000
TZNAME:BST
TZOFFSETTO:+0100
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0100
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
DTSTART:19961027T020000
TZNAME:GMT
TZOFFSETTO:+0000
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20111027T210619Z
UID:2C185D57-1B0D-400E-8CD6-96A4422D48F5
DTEND;TZID=Europe/London:20111025T043000
TRANSP:OPAQUE
SUMMARY:1012
DTSTART;TZID=Europe/London:20111025T041500
DTSTAMP:20111027T210641Z
LOCATION:
SEQUENCE:3
URL;VALUE=URI:
ty_number=
END:VEVENT
END:VCALENDAR
[/syntax]

I've edited one I created with iCal in TextWrangler and saved it as a .ics file which works just fine.

Could I have PHP generate the file based on a number of variables passed to it? If so how do you get PHP to do this?

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 4:47 pm
by mechanism
Hey try having a look at the fopen() function.

this page should give you all you need to know about doing that http://php.net/manual/en/function.fopen.php

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 4:56 pm
by manohoo
Let's give it a try:
1. using a text editor create a template with text as you have in your post, call it template.ics for instance
2. edit template.ics create "holders" for each value that you'd like. For example: BEGIN:<<begin>>. Save the file.
3. open the file in PHP and replace the holders with your desired values
This is an example:

Code: Select all

$holders = array('<<begin>>'=>'value1', '<<whatever>>'=>'value2'); // use as many key=>value pairs as needed
// generate the headers to help a browser choose the correct application
header( 'Content-type: text/plain' );
header( 'Content-Disposition: inline, filename=output.ics'); // output.ics will be the file generated

$filename = 'templace.ics'; // include a path if needed
$fp = fopen ($filename, 'r' ); // open the file
$output = fread( $fp, filesize( $filename ) );
fclose ( $fp );

// now replace holders with content
foreach ($holders as $key=>$value) {
   $output = str_replace( "<<$key>>", $value, $output) ;
}
echo $output;
I haven't tried it, but it should work.

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 5:10 pm
by mikeashfield
I don't think it's working? This is what Chrome gives.

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 5:22 pm
by manohoo
i found my error, try this:

Code: Select all

foreach ($holders as $key=>$value) {
   $output = str_replace( $key, $value, $output) ;
}

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 5:37 pm
by manohoo
Also, if you want to output to a file instead of the browser try this:

Code: Select all

header( 'Content-type: application/txt' );

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 5:40 pm
by mikeashfield
Thanks, it works a treat! :)

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 5:46 pm
by flying_circus
This should work as well:

Code: Select all

<?php
  $file = 'test.ics';

  if(file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Content-Disposition: inline; filename='.basename($file)); // Inline File
    header('Content-Length: ' . filesize($file));
    readfile($file);
  }
  exit;
?> 
It can be modified easily if you want to output a file that is dynamically generated.

Re: PHP - On the fly file generation.

Posted: Thu Oct 27, 2011 11:44 pm
by mechanism
oh, sorry, I get it, so its not to save the file but to open a non existent file to save locally. my bad, your not going to need fopen() (obviously)