PHP - On the fly file generation.
Moderator: General Moderators
-
mikeashfield
- Forum Contributor
- Posts: 159
- Joined: Sat Oct 22, 2011 10:50 am
PHP - On the fly file generation.
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?
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.
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
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.
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:
I haven't tried it, but it should work.
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;
-
mikeashfield
- Forum Contributor
- Posts: 159
- Joined: Sat Oct 22, 2011 10:50 am
Re: PHP - On the fly file generation.
I don't think it's working? This is what Chrome gives.
Re: PHP - On the fly file generation.
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.
Also, if you want to output to a file instead of the browser try this:
Code: Select all
header( 'Content-type: application/txt' );-
mikeashfield
- Forum Contributor
- Posts: 159
- Joined: Sat Oct 22, 2011 10:50 am
Re: PHP - On the fly file generation.
Thanks, it works a treat! 
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: PHP - On the fly file generation.
This should work as well:
It can be modified easily if you want to output a file that is dynamically generated.
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;
?> Re: PHP - On the fly file generation.
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)