Tag Archive for file

Alias a command and save it in .profile

echo "alias name='command string'" >> ~/.profile

source

tar basics

# create
tar cvf docs.tar docs2

# extract
tar xvf docs.tar

source

PHP File extension case changer

<?php
$root = $_GET['root'];
$files = array_merge(glob("$root*.*"),glob("$root**.*"),glob("$root***.*"));
$z=0;
for($i=0;$i<count($files);$i++) {
if(stripos($files[$i],"Thumbs.db")) {
unlink($files[$i]);
$z++;
}
else {
rename($files[$i],strtolower($files[$i]));
}
}
echo "$i files renamed. $z Thumbs.db files deleted.";
?>

source

PHP – Size converter

/**
* Get the human-readable size for an amount of bytes
* @param int  $size      : the number of bytes to be converted
* @param int $precision  : number of decimal places to round to;
*                          optional - defaults to 2
* @param bool $long_name : whether or not the returned size tag should
*                          be unabbreviated (ie "Gigabytes" or "GB");
*                          optional - defaults to true
* @param bool $real_size : whether or not to use the real (base 1024)
*                          or commercial (base 1000) size;
*                          optional - defaults to true
* @return string         : the converted size
*/
function get_size($size,$precision=2,$long_name=true,$real_size=true) {
$base=$real_size?1024:1000;
$pos=0;
while ($size>$base) {
$size/=$base;
$pos++;
}
$prefix=get_size_prefix($pos);
$size_name=$long_name?$prefix."bytes":$prefix[0].'B';
return round($size,$precision).' '.ucfirst($size_name);
}
/**
* @param int $pos : the distence along the metric scale relitive to 0
* @return string  : the prefix
*/
function get_size_prefix($pos) {
switch ($pos) {
case 00: return "";
case 01: return "kilo";
case 02: return "mega";
case 03: return "giga";
case 04: return "tera";
case 05: return "peta";
case 06: return "exa";
case 07: return "zetta";
case 08: return "yotta";
case 09: return "xenna";
case 10: return "w-";
case 11: return "vendeka";
case 12: return "u-";
default: return "?-";
}
}

source

Render file (no html)

send_file 'path/file_003', :disposition => 'attachment; filename=image.jpg'

source

Expand MySQL File

<?php
/**************************************
seesaw associates | <a href="http://seesaw.net" >http://seesaw.net</a>

client:
file:
description:

Copyright (C) 2008 Matt Kenefick(.com)
**************************************/

ini_set("memory_limit","100M");

class expandSQL{

var $db;

function expandSQL($db){
$this->db = $db;
}

function expand($file){
$sql = $this->readFile($file);
foreach($sql as $query)
$db->query($query);

return true;
}

function readFile($filename){
$fileExt = $this->getext($filename);

if(file_exists($filename)){
$nLines = '';

switch( strtolower($fileExt) ){
case 'gz':
$lines = gzfile($filename);
break;
case 'sql':
$lines = readfile($filename);
break;
}

foreach($lines as $line) {
if(substr($line,1,1)!='-' && substr($line,1,1)!='')
$nLines .= $line;
}

return split(';',$nLines);
}
}

function getext($filename){
$ext = split('.',$filename);
$ext = $ext[count($ext)-1];
return $ext;
}
}

?>

source

Include File Protection ..

Using includes in PHP to simplify life? Don't want your visitors to access them and receive errors or partial content? The solution is simple enough, though many people don't worry or don't consider it as a "threat".
There are a few reasons you wouldn't want someone to go through directories trying to access your include files. Whatever yours might be, there's an easy way to prevent it.
On your index file, you have it generate a variable.
<?php
$include_lock = "unlocked";
?> So we now have some code that generates a variable, '$include_lock' with the value 'unlocked'. Why are we doing this? Well, since you only want them to access the page through the index, we'll make a lock and key so that only going through the index file gives them the key.
With the code above, we need to now put something on the page that actually performs as the lock. It's simple enough, we'll just use a nice, clean if statement.
<?php
if ($include_lock != "unlocked") {
//Shut them down.
header("Location: 404.shtml");
OR
die("404 - File Not Found");
//You basically want it so that it would look like the typical 404 message from your site.
} else {
//your normal content goes here.
}
?> You'll want to take note that if you have 'register_globals' on, the user could simply add '?include_lock=unlocked' to the URL and gain access. To get around that, you could disable register_globals or use a session variable. If you go the session route, kill the variable after you do the include.

source

Python File manipulation Cheat Sheet

#write to file
f = open('c:/temp/workfile','w')
f.write('hallo')
f.close

#open from file
f = open('file.txt')

#read linewise
for i in f:
print i

#read characterwise
for i in f.read():
print i

source

Titlebar customization, file name displaying

(add-hook 'window-configuration-change-hook
(lambda ()
(setq frame-title-format
(concat
invocation-name "@" system-name ": "
(replace-regexp-in-string
(concat "/home/" user-login-name) "~"
(or buffer-file-name "%b"))))))

source

php uploading files

//if they DID upload a file...
if($_FILES['photo']['name'])
{
//if no errors...
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops!  Your file's size is to large.';
}

//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations!  Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
}
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']

source