Using PHP to write to a file

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 10 pptx (Trang 65 - 70)

Writing to a file with PHP isn’t difficult, but it involves three steps, as follows:

1.Create a resource handler to open the file.

2.Write the contents to the file.

3.Close the file.

Each step uses an intuitively named function: fopen(), fwrite(), and fclose().

Unfortunately, fopen()has a bewildering range of options that prepare the file for read- ing and writing in different ways. If you’re interested in the details, study the PHP online manual at http://docs.php.net/manual/en/function.fopen.php or read Chapter 7 of my book PHP Solutions: Dynamic Web Design Made Easy(friends of ED, ISBN-13: 978-1- 59059-731-6).

The option that I’m going to use overwrites any existing content in the file. This is ideal for creating a static XML document from a dynamic source. All you need to do is run the script each time you update the contents of your database, and the XML document is automatically updated. I have wrapped the script in a custom function, so you can use it in conjunction with any script that you want to write the contents of a variable to an exter- nal file.

The function, complete with inline comments, follows (if you installed the snippets exten- sion in Chapter 11, it’s in Write to filein the PHP-DWCS4folder of the Snippetspanel):

// function to overwrite content in a file function writeToFile($content, $targetFile) {

// open the file ready for writing if (!$file = fopen($targetFile, 'w')) {

echo "Cannot create $targetFile";

exit;

}

// write the content to the file if (fwrite($file,$content) === false) {

echo "Cannot write to $targetFile";

exit;

}

echo "Success: content updated in $targetFile";

// close the file fclose($file);

}

The writeToFile()function takes two arguments, as follows:

The content that you want to write to the file The name of the target file

To create an XML document from a remote source, include the writeToFile()definition.

Then delete the following lines from proxy.phpor curl.phpin the previous section (they are highlighted in bold in the full listings):

header('Content-Type: text/xml');

echo $remote;

Replace them with a call to the writeToFile()function like this:

$xmlfile = 'foed.xml';

writeToFile($remote, $xmlfile);

A

This creates a file called foed.xmlthat contains the latest version of the friends of ED RSS feed. If there’s any problem with creating the file, an appropriate error message is dis- played instead.

To create a static XML document from an XML source generated dynamically using the technique described in the “Converting a recordset to generate XML” section, you need to adapt proxy.php.

The script in proxy.phpuses the file_get_contents()function to retrieve the XML from a remote source. If you try to use this on a local file, such as japan_xml.php, instead of the XML, you get the PHP script that generates the XML. So, instead of using the file name, you need to use the full URL so that the file is processed by the web server.

The script looks like this (it’s in local_proxy_write.phpin examples/appendix):

<?php

$url = 'http://dwcs4/workfiles/appendix/japan_xml.php';

// Get remote headers

$headers = get_headers($url);

// Make sure the first header includes 'OK' if (stripos($headers[0], 'OK')) {

$xml = file_get_contents($url);

// Set the name of the file to write the XML to

$xmlfile = 'japan_proxy.xml';

// function to overwrite content in a file function writeToFile($content, $targetFile) { // open the file ready for writing

if (!$file = fopen($targetFile, 'w')) { echo "Cannot create $targetFile";

exit;

}

// write the content to the file if (fwrite($file,$content) === false) {

echo "Cannot write to $targetFile";

exit;

}

echo "Success: content updated in $targetFile";

// close the file fclose($file);

}

// Write to the file

writeToFile($xml, $xmlfile);

} else {

echo "Cannot open file at $url";

}

?>

This gets the XML generated by japan_xml.php(the file that created XML from a record- set earlier in this appendix) and writes it to a file called japan_proxy.xml. To use this script with another page that generates XML on the fly, just change the values of $urland

$xmlfile.

To find the correct value for $url, open in the Document window the page that generates the XML source, and press F12/Opt+F12 to view the XML output in the browser. Select the URL in the browser address bar, and paste it into local_proxy_write.php. This won’t work if you have set your Dreamweaver preferences to use a temporary file for Preview in Browser(see Chapter 2 for details of how to change the setting).

Although I designed the writeToFile()function to write an XML source to file, it is completely generic. It writes any string stored in the first argument to the file named in the second argument.

A

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 10 pptx (Trang 65 - 70)

Tải bản đầy đủ (PDF)

(94 trang)