<? DEW-CODE.COM  

Downloads

Assorted scripts  (1)
DewNewPHPLinks  (9)





Lost Password?
No account yet? Register

Dew-Code Newsfeed

Resources


 USdigitalcable.com

Dew-Code
Welcome, Guest
Please Login or Register.    Lost Password?
Re:Handling upload and parse of text files (1 viewing) (1) Guest
Post your PHP related questions or observations here. Code snippets are always welcome.
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Handling upload and parse of text files
#345
Dewed (Admin)
Admin
Posts: 603
graph
User Online Now Click here to see the profile of this user
Dew-Code.com
Handling upload and parse of text files 3 Years, 7 Months ago Karma: 9  
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
 
Report to moderator   Logged Logged  
 
Nothing to it but to Dew it !
Dew-Code.com
  The administrator has disabled public write access.
#1249
XAOPT.ru (User)
Fresh Boarder
Posts: 2
graphgraph
User Offline Click here to see the profile of this user
Re:Handling upload and parse of text files 2 Years ago Karma: 0  
Thx, it's really usefull
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1301
4square (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:Handling upload and parse of text files 1 Year, 11 Months ago Karma: 0  
Thanks for the script but I'm new at this and I'm having a little difficulty following it. Do you have this in a structured format? Thanks for the help!
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1302
Dewed (Admin)
Admin
Posts: 603
graph
User Online Now Click here to see the profile of this user
Dew-Code.com
Re:Handling upload and parse of text files 1 Year, 11 Months ago Karma: 9  
Not sure what you mean by structured format.

Just copy from the top <html> tag to the bottom ?> tag.
Paste that into notepad. Save the file as a .txt then rename it to .php and upload to your server.
 
Report to moderator   Logged Logged  
 
Nothing to it but to Dew it !
Dew-Code.com
  The administrator has disabled public write access.
#1303
4square (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:Handling upload and parse of text files 1 Year, 11 Months ago Karma: 0  
Thanks for the reply,
I did that but it comes in as one line of code. It's very difficult to follow. For the most part, the ';' is the end of the line except during the if-then logic. I'm trying to work through it. I guess if it was indented structure of a normal script it would be easier to work though.

This is my problem:
I have very small simple text 2k to 6k files that I need to extract some information out of and I'm having a problem doing that. Some of the information is standard labels that can be searched. But there is a section that is variable length where I only need to count the number of '.' in that group.

I have the upload part working. I just don't have the counting part working. I can send you a file if you would be interested in looking at it. Let me know.
Thank you again!
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1304
XAOPT.ru (User)
Fresh Boarder
Posts: 2
graphgraph
User Offline Click here to see the profile of this user
Re:Handling upload and parse of text files 1 Year, 11 Months ago Karma: 0  
Just try other browser or check current configuration. It looks like JS highlighting script doesn't work.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
get the latest posts directly to your desktop
Outsource your projects to thousands of programmers at
ScriptLance.com
Newsflash
Interested in
advertising?

This space for rent!

Sign up for PayPal and start accepting credit card payments instantly.
Copyright Dew-Code 2008