Sometimes you need to be able to upload a text file of data, like a CSV, tab delimited or even an XML file,
and parse it. I recently had such a task, so I thought I'd share the core of the routine I came up with.
Commented of course, for you viewing pleasure
| Code: |
<html>
<head></head>
<body>
<?php
// define a directory to store the uploaded files in ..
$feed_cache_dir = './feed_cache/';
// delete cached files older than this many days
$days = "15";
// possibly your webserver doesn't have upload_tmp_dir defined,
// if uploads fail for no obvious reason, try uncommenting the following line
// ini_set('upload_tmp_dir','/tmp');
// if cache directory does not exist, create it, best to let the web server create it,
// since you wont have to mess with creating it yourself, then changing permissions etc.
if (is_dir("$feed_cache_dir")){} else {mkdir("$feed_cache_dir", 0700);}
if (!empty($_FILES)){
$target = $feed_cache_dir . basename( $_FILES['uploaded']['name']) ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
// ok, apparently a file was uploaded
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
sleep(1); // be sure the system has closed the file before opening it ourselves
echo '<br />Opening '.$feed_cache_dir . basename($_FILES['uploaded']['name']).'<br />';
$dataFile = fopen($feed_cache_dir . basename($_FILES['uploaded']['name']), "rb");
while (!feof($dataFile)){
$buffer = fgets($dataFile, 4096);
// Ok, now the real work begins, extracting data from the file.
// Possibly you are trying to extract data from a pipe delimited file...
// dataline|Some value one|Some value two|Some value three|
$findpattern = "/dataline|(.*)|(.*)|(.*)|/";
preg_match($findpattern, $buffer, $foundmatches);
// possibly some identical data is in the source file twice, to strike duplicates
// uncomment the following line
// $foundmatches = array_unique($foundmatches);
// For now, we'll just echo the matches to the browser, but potentially you could
// store the data in a database, or maybe use it to generate a graph
if (!empty($foundmatches[1])){
echo '<br />'.$foundmatches[1] .'<br />'.$foundmatches[2] .'<br />'.$foundmatches[3] .';
} else {
// for debugging, its handy to see the lines that did not match
// uncomment the following line to have non matches prin to the browser
// echo '<br />did not mntach anything<br /><b><i><pre>' . $buffer .'<br /></i></b></pre>';
}
// also for debugging, uncomment this line to see the array with the matches in it
/* echo '<br />';
print_r($foundmatches);
echo '<br />';
*/
}
// all done close the file
fclose($dataFile);
} else { echo "Sorry, there was a problem uploading your file."; }
} else {
// The initial view of this page, so just show the file upload form, and do the
// cache directory clean up behind the scenes.
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Please choose a file: <input name="uploaded" type="file" /> <input type="submit" value="Upload" />
</form>
<?php
}
?>
</body></html>
<?php
////////////// CACHE CLEAN UP
// this checks for files older than $days, from the setting above and deletes them
$files = scandir($feed_cache_dir);
$seconds = $days * 24 * 60 * 60;
foreach ($files as $num => $fname){
if (file_exists("{$dir}{$fname}") && ((time() - filemtime("{$feed_cache_dir}{$fname}")) > $seconds)) {
$mod_time = filemtime("{$feed_cache_dir}{$fname}");
if (unlink("{$feed_cache_dir}{$fname}")){
$del = $del + 1;
echo "Deleted: {$del} - {$fname} --- ".(time()-$mod_time)." seconds old";
}
}
}
?>
|
Keep in mind, if this were to be used as a public form, you would want to do much more to ensure someone didn't try something sneaky. In my case, this script resides in a password protected directory, and only the site owner and myself have that user name and pass, so
I was a bit relaxed on security precautions