Posted by: prajapatinilesh | September 26, 2007

File Related Example in php

Example 1: **************************************
//create file myfile.txt … with value like….
nilesh prajapati | nilu | nil | prajapati
//now php code for example….
<?php
function getline( $fp, $delim ) {
$result = “”;
while( !feof( $fp ) ) {
$tmp = fgetc( $fp );
if( $tmp == $delim )
return $result;
$result .= $tmp;
}
return $result;
}
// Example:
$fp = fopen(“myfile.txt”, ‘r’);
$resArray = array();
while( !feof($fp) ) {
$str = getline($fp, ‘|’);
$resArray[] = $str;
//echo $str;
// Do something with $str
}
echo “<pre>”;
print_r($resArray);
echo “</pre>”;
fclose($fp);
?>
Example 2: **************************************
<?php
function write_lines($text) {
$file = fopen(‘myfile.txt’, ‘a’);
fwrite($file, str_replace(“\n”, ‘ ‘, $text).”\n”);
fclose($file);
}
function read_all() {
$file = fopen(‘myfile.txt’, ‘r’);
$i = 1;
while (!feof($file)) {
$line = fgets($file);
echo ‘<u>Line #’. $i .'</u> => ‘.nl2br($line).'</p>’;
$i++;
}
fclose($file);
}
read_all();
?>

Example 3: **************************************
<?php
//instead of reading whole file with it max size… need to read in solt of 8192….
$fd = fopen(“myfile.xml”,”r”) or die(“can not open file for reading….”); //rb
//$fstr = fread($fd,filesize(‘myfile.xml’)) or die(‘cannot read file….’);
$contents = “”;
do {
$data = fread($fd, 8192);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while (true);
$fstr = trim($contents);
echo $fstr;
?>
*****************************************************************************
Note: If you are having problems with PHP not recognizing the line endings when reading files either on or created by a Macintosh computer, you might want to enable the auto_detect_line_endings run-time configuration option.
you can use
$file = array_map('rtrim',file('myfile.txt'));
to remove annoying ending lines of the resulting array.

******************************************************************************
<? //Example usage of file to remove line containing a key string
$key = “nilesh”;
$fc=file(“myfile.txt”);
$f=fopen(“myfile.txt”,”w”);
foreach($fc as $line) {
if (!strstr($line,$key)) //look for $key in each line
fputs($f,$line); //place $line back in file
}
fclose($f); ?>
******************************************************************************
to get pdf to attach right some of you may want to try reading it as binary first then base 64 it.
//this did not work with a pdf file

$data = chunk_split(base64_encode(implode("", file($filelocation))));
//but it seemed to make it work correctly
$data = fread($file,filesize($filelocation));
fclose($file);
$data = chunk_split(base64_encode($data));

******************************************************************************
$filename = ‘confing_file_path’; parse_ini_file($filename);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: