count characters in 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
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

count characters in file

Post by juline »

hi friends,

is there a possibility of counting characters in a file like .pdf or .doc ?

cheers,

juline
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

not too sure but maybe

Code: Select all

$contents = file_get_contents($file);

echo strlen($contents);
I know that applies to text, but not sure about documents and such.
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

Post by juline »

thanx.

i dcripted following code which is uploading a file and counting the chracters.

Code: Select all

<?php
	
	$uploaddir = 'uploads/temp/'; 
	$filename=$_FILES&#1111;'file']&#1111;'name']; 
  
	if (move_uploaded_file($_FILES&#1111;'file']&#1111;'tmp_name'], $uploaddir."$filename")) 
	&#123; 
       
    $datei=fopen("uploads/temp/$filename", r);   
	$content=file_get_contents("uploads/temp/$filename");

	echo "Filename: ";
	echo "$filename";
	echo "<br>";
	echo "Chars ";
	echo strlen($content);

	
	&#125; 
  
?>
but the output is not exactly correct. it puts more out than actually is in file. then i would like to devide tha amount of chars into 50. and after displaying the result delete the uploaded file.

can someone help me please ?

i tried the following:



Code: Select all

<?php
	
	$uploaddir = 'uploads/temp/'; 
	$filename=$_FILES&#1111;'file']&#1111;'name']; 
  
	if (move_uploaded_file($_FILES&#1111;'file']&#1111;'tmp_name'], $uploaddir."$filename")) 
	&#123; 
       
    $datei=fopen("uploads/temp/$filename", r);   
	$content=file_get_contents("uploads/temp/$filename");
	$norm=$content/50;
	echo "Dateiname: ";
	echo "$filename";
	echo "<br>";
	echo "Zeichen ca. ";
	echo strlen($content);
	echo " <br>";
	echo "Normzeile(n): ";
	echo "$norm";
	
	&#125; 
  
?>

regards,
juline
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not use filesize()?
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

Post by juline »

feyd wrote:why not use filesize()?
i dont know what you mean. the characters are being count to calculate the approximate value for the standard lines in the text by deviding into 50.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

both pdf and doc are binary file formats. You won't be able to just open the file with any arbitrary function and read out all the actual text data.

The code you are working with right now is based off the file's size. Not the character counts contained in their encoding. This is the reason the sizes you recieve back are inaccurate.
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

Post by juline »

all right, i got it. but i dont know what to do now.
Post Reply