PHP - On the fly file generation.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

PHP - On the fly file generation.

Post 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?
mechanism
Forum Newbie
Posts: 5
Joined: Thu Oct 27, 2011 4:43 pm

Re: PHP - On the fly file generation.

Post 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
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP - On the fly file generation.

Post 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.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: PHP - On the fly file generation.

Post by mikeashfield »

I don't think it's working? This is what Chrome gives.
Attachments
Screen Shot 2011-10-27 at 23.09.36.png
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP - On the fly file generation.

Post by manohoo »

i found my error, try this:

Code: Select all

foreach ($holders as $key=>$value) {
   $output = str_replace( $key, $value, $output) ;
}
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP - On the fly file generation.

Post 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' );
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: PHP - On the fly file generation.

Post by mikeashfield »

Thanks, it works a treat! :)
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP - On the fly file generation.

Post 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.
mechanism
Forum Newbie
Posts: 5
Joined: Thu Oct 27, 2011 4:43 pm

Re: PHP - On the fly file generation.

Post 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)
Post Reply