[SOLVED] Parsing File

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
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Parsing File

Post by Harlequin »

Morning all.

I'm using the following code to parse a file:

Code: Select all

// Change File Permissions: 
    chmod("{$uploadfile}", 0644); 

// Parse the File:    
   $filename = "$uploadfile"; 
   $handle = fopen($filename, "r"); 
   $contents = fread($handle, filesize($filename)); 

// Stuff The Parsed File into the database: 
   $Query01 = "UPDATE MembersData SET 
    CVParsed  = '$contents' 
   WHERE UserID = '{$_SESSIONї'logname']}'"; 
    $Results01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); 
   fclose($handle);
But for some reason when a user uploads a DOC file it updates ALL the user's CVParsed fields.

Anyone come accross this or know why the code is doing this...?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Parsing File

Post by timvw »

Harlequin wrote:

Code: Select all

// Parse the File:    
   $filename = "$uploadfile"; 
   $handle = fopen($filename, "r"); 
   $contents = fread($handle, filesize($filename));
$contents = mysql_escape_string(file_get_contents($filename));

Harlequin wrote:

Code: Select all

// Stuff The Parsed File into the database: 
   $Query01 = "UPDATE MembersData SET 
    CVParsed  = '$contents' 
   WHERE UserID = '{$_SESSION['logname']}'"; 
    $Results01 = mysql_query($Query01) or die("Error 01: " . mysql_error());
might want to echo $Query01 first.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

timvw

Thanks for your help with this. It's now behaving reasonably well and only updating the one user's CVParsed field in the table.

One final thing though (if you have time).

Is there a way to stop the extraneous characters being generated when I change the file to RTF or should I simply leave the fie extension alone...?

The main reason I force the rather crude change to RTF is to stop idiots uploading exectables or betch files and so on...

Any ideas...?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

remember to do something like below to remove the slashes that were added mysql_escape_string when outputting the data

Code: Select all

$query = "SELECT * FROM foo";
$result = mysql_query($query) or die(mysql_error());

$rows = array();
while ($row = mysql_fetch_assoc($result))
{
  foreach($row as $col => $val)
  {
     $row[$col] = stripslashes($val);
  }
  $rows[] = $row;
}




also, if you see a lot of weird characters, you might have a problem with iso-8859-1 vs utf-8 conversion?


the easiest way to avoid unwanted thingies, you could for example use a regular expression to replace all non-characters by ''.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

Agreed Tim. I'll have to chew this one over because most users wil be uploading DOC files so maybe making that default extension might help.

Thanks for your time again mate :o
Post Reply