Posts Tagged ‘file’

show / download image with PHP

March 9th, 2010

To show a image in browser:

$file_path = ICON_DIR . $_GET['f'];
// TODO check file_path security
$file_name = basename($_GET['f']);
$fileLineArray = file($file_path);
header(‘Content-Type: image/jpeg’);
header(“Content-Disposition:filename=$file_name”);
foreach($fileLineArray as $fileLine)
echo $fileLine;

To force the browser to show the download dialogue, add a attachment header:

header(“Content-Disposition:filename=$file_name”);
header(“Content-disposition: attachment; filename=$file_name”);

using bash to count lines in file

February 22nd, 2010

find *.c -print0 | xargs -0 wc -l

Export results from MySQL into an external file

February 10th, 2010

1. From mysql command line:

  • SELECT colA, colB FROM table1 INTO OUTFILE "/tmp/output.txt";

The detail SELECT syntax is:

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

2. From console: combine mysql logging in & exporting

  • echo "SELECT colA, colB FROM table1" | mysql -uXXX -pYYY SchemaName > /tmp/outout.txt;
  • mysql mysql -uXXX -pYYY SchemaName < /tmp/export.sql > /tmp/outout.txt;

REFERENCE:

——————————————————————————————–

The corresponding syntax of loading outfile data is:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]

REFERENCE: