Marius van Witzenburg

Life can be so beautiful, if you have a wide vision.

Menu
  • Home
  • FreeBSD
  • How to
  • FAQ
  • Projects
  • Things I like

Tag: File

How to read a file line by line with Bash

By mariusvw
|
July 10, 2011
| No Comments
| How to

Sometimes you want to do actions per line instead of the complete file at once. Here is an example that you can use to read the file line by line.

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
run_cmd_file() {
    while read line
    do
        chr=${line:0:1}
        case $chr in
            "#" )  # ignore commented lines
                ;;
            *   )
                echo "$line"
                ;;
        esac
    done < $1
}
 
run_cmd_file filename

Read More »

Posted in How to Tagged Bash, File, Line, Lines, Loop, Read Leave a comment

How to get a file extension with PHP

By mariusvw
|
January 4, 2010
| No Comments
| How to

The following script makes it more easy to get the file extension from a file name:

PHP
1
2
3
4
5
6
7
8
function get_file_extension($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}
echo get_file_extension('myfile.txt');

In case you have the ability to use mime type detection, you could combine it with the above function to validate if the extension is valid for the content of the file.

Read More »

Posted in How to Tagged Extension, File, Filename, Name, PHP Leave a comment

Social

Search

Archives

  • December 2017
  • October 2017
  • September 2017
  • March 2017
  • February 2017
  • January 2017
  • November 2016
  • October 2016
  • February 2016
  • December 2015
  • March 2015
  • November 2013
  • July 2013
  • May 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • December 2011
  • November 2011
  • July 2011
  • June 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010

Categories

  • FAQ
  • FreeBSD
  • FreeBSD world
  • Funny
  • How to
  • Projects

FreeBSD

Proud Donor

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Marius van Witzenburg 2018 | All Rights Reserved. Powered by WordPress