This php code will strip the extension from a filename.
to remove the extension just call the function with the name of the file.
function strip_ext($name)
{
$ext = strrchr($name, '.');
if($ext !== false)
{
$name = substr($name, 0, -strlen($ext));
}
return $name;
}
// demonstration
$filename = 'file_name.txt';
echo strip_ext($filename)."\n";
// to get the file extension, do
echo end(explode('.',$filename))."\n";
?>
Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts
Monday, July 23, 2007
File Handling - Write to File
function to write a string to a file
function file_write($filename, $filecontent)
{
if($fp = @fopen($filename,"w"))
{
$contents = fwrite($fp, $filecontent, 80000);
fclose($fp);
return true;
}else{
return false;
}
}
?>
function file_write($filename, $filecontent)
{
if($fp = @fopen($filename,"w"))
{
$contents = fwrite($fp, $filecontent, 80000);
fclose($fp);
return true;
}else{
return false;
}
}
?>
File Handling - Write to File First Line
This snippet let’s you insert data at the beginning of a file.
it reads the contents and adds new data to the first line of the file.
after this it joins the lines and writes them back to the file again.
// your new data + newline
$new_line = 'some new data here'."\n";
// the filepath
$file = 'emp/file.txt'
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join(",",old_lines);
$fp = fopen($file,‘w’);
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);
?>
it reads the contents and adds new data to the first line of the file.
after this it joins the lines and writes them back to the file again.
// your new data + newline
$new_line = 'some new data here'."\n";
// the filepath
$file = 'emp/file.txt'
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join(",",old_lines);
$fp = fopen($file,‘w’);
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);
?>
Subscribe to:
Posts (Atom)