Cancellare tutti i file vecchi

26 luglio 2010 fox91 Nessun commento

Script per cancellare tutti i file più vecchi di un numero di giorni:

find /dir/to/file/* -mtime +3 -exec rm {} \;

dove -mtime +3 indica che verranno cercati i file creati più di 3 giorni fa.

Categorie:Script Bash Tag:

PHP cURL file download

22 luglio 2010 fox91 Nessun commento

Se si tratta di un file ASCII utilizzare il seguente codice:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://news.google.com/news?hl=en&topic=t&output=rss');
$fp = fopen('rss.xml', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);

Se invece si tratta di un file binario utilizzare quest’altro codice

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/serverfile.zip');
$fp = fopen('file.zip', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
Categorie:PHP Tag: , , ,

Link mailto antispam

2 aprile 2010 fox91 Nessun commento

Come molti sapranno è sufficiente inserire un indirizzo email in una pagina web per essere bersagliati da una valanga di email di spam. Con qualche piccolo accorgimento si può evitare questo inconveniente. Io ad esempio ho deciso di usare javascript ma ci sono moltissimi altri modi per farlo. Ecco il codice da inserire nell’head:

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function () {
	mail_addr('#mail', 'info', 'andreafalco', 'com');
});
function mail_addr(i, n, d, t) {
	var string_mail = n + '@' + d + '.' + t;
	$(i).html(string_mail);
	$(i).attr('href', 'ma' + 'ilto' + ':' + string_mail);
}
</script>
Categorie:jQuery, xHTML Tag:

Decomprimere file .gz

14 gennaio 2010 fox91 Nessun commento
function gzfile_get_contents($filename, $use_include_path = 0)
{
	//File does not exist
	if(!@file_exists($filename)) {
		return false;
	}

	//Read and imploding the array to produce a one line string
	$data = gzfile($filename, $use_include_path);
	$data = implode($data);
	return $data;
}
//scriviamo il risultato in un file
file_put_contents('punto.txt', gzfile_get_contents('punto.txt.gz'));
Categorie:PHP Tag:

System.Collections.Hashtable

7 gennaio 2010 fox91 Nessun commento

Rappresenta un insieme di coppie chiave/valore organizzate in base al codice hash della chiave.

using System;
using System.Collections;

class Example
{
	public static void Main()
	{
		Hashtable htTest = new Hashtable();

		htTest.Add("txt", "notepad.exe");
		htTest.Add("bmp", "paint.exe");
		htTest.Add("dib", "paint.exe");
		htTest.Add("rtf", "wordpad.exe");

		try
		{
			htTest.Add("txt", "winword.exe");
		}
		catch
		{
			Console.WriteLine("Un elemento con 'Key' = 'txt' esiste gia.");
		}

		Console.WriteLine("Per la 'key' = 'rtf', 'Value' = {0}.", htTest["rtf"]);

		htTest["rtf"] = "winword.exe";
		Console.WriteLine("Per la 'key' = 'rtf', 'Value' = {0}.", htTest["rtf"]);

		htTest["doc"] = "winword.exe";

		if (!htTest.ContainsKey("ht"))
		{
			htTest.Add("ht", "hypertrm.exe");
			Console.WriteLine("Valore aggiunto con 'key' = 'ht', 'Value' = {0}", htTest["ht"]);
		}

		Console.WriteLine();
		foreach( DictionaryEntry de in htTest )
		{
			Console.WriteLine("'Key' = {0}, 'Value' = {1}", de.Key, de.Value);
		}

		ICollection valueColl = htTest.Values;

		Console.WriteLine();
		foreach( string s in valueColl )
		{
			Console.WriteLine("'Value' = {0}", s);
		}

		ICollection keyColl = htTest.Keys;

		Console.WriteLine();
		foreach( string s in keyColl )
		{
			Console.WriteLine("'Key' = {0}", s);
		}

		Console.WriteLine("\nRimuovo elemento con chiave = 'doc'");
		htTest.Remove("doc");

		if (!htTest.ContainsKey("doc"))
		{
			Console.WriteLine("L'elemento con chiave = 'doc' non esiste.");
		}
	}
}

Formattare i numeri con printf()

6 gennaio 2010 fox91 Nessun commento
$number = 15;
printf("Decimal: %d", $number);	//Decimal: 15
printf("Binary: %b", $number);	//Binary: 1111
printf("Double: %f", $number);	//Double: 15.000000
printf("Octal: %o", $number);	//Octal: 17
printf("String: %s", $number);	//String: 15
printf("Hex (lower): %x", $number);	//Hex (lower): f
printf("Hex (upper): %X", $number);	//Hex (upper): F
Categorie:PHP Tag: ,

Watermark in PHP

19 novembre 2009 fox91 Nessun commento
$main_img = "Porsche_911_996_Carrera_4S.jpg"; //immagine principale
$watermark_img = "watermark.gif"; //use GIF or PNG, JPEG has no tranparency support
$padding = 3; //distance to border in pixels for watermark image
$opacity = 100; //image opacity for transparent watermark

$watermark = imagecreatefromgif($watermark_img); //create watermark
$image = imagecreatefromjpeg($main_img); //create main graphic

if(!$image || !$watermark)
	die("Error: main image or watermark could not be loaded!");

$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];  

$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;

//copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);

//print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

Tratto da http://www.sastgroup.com/tutorials/aggiungere-un-watermark-alle-vostre-immagini-con-php

Categorie:PHP Tag: ,

Contare il numero di files in una directory

15 novembre 2009 fox91 Nessun commento
$count = 0;
$hd = opendir("percorso/vostra/directory");

while(($file = readdir($hd)) !== false) {
	//Se i file sono nascosti non gli conta
	if ($file[0] != ".") $count++;
}
echo $count; //stampa a video il numero totale di files

Tratto da http://www.sastgroup.com/tutorials/php-contare-il-numero-di-files-in-una-directory

Categorie:PHP Tag: , , ,

Gestire BBCode in PHP

7 novembre 2009 fox91 Nessun commento
function BBCode($text) {
/**
* The following array has the structure Key => Value.
* The key is the item that will be typed e.g [b] the value will be the html equivilant.
*/
	$BBCode = array("&" => "&amp;", "< " => "&lt;", ">" => "&gt;", "[b]" => "<b>", "[/b]" => "</b>", "[i]" => "<i>", "[/i]" => "</i>", "[u]" => "<u>", "[/u]" => "</u>", "[img]" => "<img src='", "[/img]" => "'/>"); // Comment Above
	$parsedtext = str_replace(array_keys($BBCode), array_values($BBCode), $text); //This function will get the input string. and then the keys and values of the array above. It will then replace them with the html equivilant.
	return $parsedtext; // Return $parsedtext.
}

Esempio Applicato:

$text = BBCode("[b][u]HELLO[/u][/b]"); // Execute Function
echo $text; // Echo $text.

Tratto da http://www.sastgroup.com/tutorials/bbcode-in-php

Ripristinare grub

31 ottobre 2009 fox91 Nessun commento
sudo grub
find boot/grub/stage1 -> (la risposta sarà hdx,y)
root (hdx,y)
setup(hdx)
quit
Categorie:Linux Tag: