2010-05-15, 02:46:32
Created a new plugin File Meta Info, which allows you to store extra (meta) information about files. Data is stored in an XML file (data/other/file_meta.xml) and you can define what meta data to store by changing the array $file_meta_fields.
To display the data in a page, use <?php echo file_meta_data('filename.pdf', 'tags') ?>.
Code:
To display the data in a page, use <?php echo file_meta_data('filename.pdf', 'tags') ?>.
Code:
Code:
<?php
/*
Plugin Name: File Meta Info
Description: Store additional information about uploaded files
Version: 1.0
Author: Sam Collett
*/
# get correct id for plugin
$thisfile=basename(__FILE__, ".php");
# register plugin
register_plugin(
$thisfile,
'File Meta Info',
'1.0',
'Sam Collett',
'http://www.texotela.co.uk',
'Save additional meta information for uploaded files',
'',
''
);
# "Text to display" => "metakey" (no spaces)
$file_meta_fields = array("Tags" => "tags");
# activate filter
add_action('index-pretemplate', 'file_meta_preload');
add_action('header', 'file_meta_header');
add_action('file-extras','file_meta_extras');
# path to xml file
$file_meta_path = GSDATAOTHERPATH.'file_meta.xml';
# xml data
$file_meta_xml;
# functions
function file_meta_preload()
{
global $file_meta_xml, $file_meta_path;
if(is_file($file_meta_path)) {
$file_meta_xml = getXML($file_meta_path);
}
else {
$file_meta_xml = @new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><files></files>');
XMLsave($file_meta_xml, $file_meta_path);
}
if (isset($_REQUEST["file_meta"]))
{
file_meta_update();
}
}
function file_meta_header()
{
// preload data
file_meta_preload();
echo '<style type="text/css">tr.file_meta { font-size: 0.875em } tr.file_meta input.meta { font-size: 0.875em; width: 200px } tr.file_meta td { padding: 0 18px }</style>';
}
function file_meta_extras() {
global $file_meta_fields, $upload;
echo '<tr class="file_meta"><td colspan="4">
<form method="post" action="'.htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES).'">';
foreach($file_meta_fields as $text => $metakey)
{
echo $text.': <input name="meta_'.$metakey.'" value="'.file_meta_data($upload['name'], $metakey).'" class="'.$metakey.' meta" />';
}
echo '<input type="hidden" name="file" value="'.$upload['name'].'" />
<input type="submit" value="Save" name="file_meta" />
</form>
</td></tr>';
}
function file_meta_data($file, $meta)
{
global $file_meta_xml;
$meta_data = '';
if(is_object($file_meta_xml))
{
$current = $file_meta_xml->xpath('//files/file[@name="'.$file.'"]');
if(count($current) !== 0) {
$children = $current[0]->xpath($meta);
if(count($children) > 0)
{
$meta_data = $children[0];
}
}
}
return $meta_data;
}
function file_meta_update()
{
global $file_meta_xml, $file_meta_path;
$file = '';
// meta data to update
$meta_update = array();
foreach($_REQUEST as $key => $value)
{
// temp array for storing meta key and its value
$ar = array();
// if request key is file
if($key == "file") $file = $value;
if(strpos($key, "meta_") === 0) {
$ar = explode('_',$key);
$meta_update[$ar[1]] = $value;
}
}
// find the file in the XML document
$file_item = $file_meta_xml->xpath('//files/file[@name="'.$file.'"]');
// if the file is not found
if(count($file_item) === 0) {
$file_item = $file_meta_xml->addChild('file');
$file_item->addAttribute('name', $file);
}
// loop through all meta keys to update
foreach($meta_update as $key => $value)
{
$children = $file_item[0]->xpath($key);
if(count($children) > 0)
{
// remove existing tag
unset($children[0][0]);
}
$file_item[0]->addChild($key, $value);
}
XMLsave($file_meta_xml, $file_meta_path);
}
-- Sam