FabioRicali.it

PHP

Rinominare un file con php

by info@fabioricali.it on giu.21, 2010, under PHP

Per rinominare un file con php basta questa semplice funzione

<?php
rename("/path/file.tx", "/path/newfile.txt");
?>
Leave a Comment more...

Copiare un file con php

by info@fabioricali.it on giu.10, 2010, under PHP

Questa funzione ci permette di copiare un file sia locale che in remoto in un altro percorso

<?php
function copyFile($source, $destination){
    if(!@copy($source,$destination)){
        $errors = error_get_last();
        $error  = $errors['type']."<br/>".$errors['message'];
        return $error;
    }else{
        return true;
    }
}
?>

Utilizzo:

<?php
copyFile("http://www.sitoremoto.it/file.txt", "./file.txt");
?>

In caso di successo torna “true” altrimenti l’errore.

Leave a Comment more...

Convertire una data RFC822 in una normale

by info@fabioricali.it on giu.09, 2010, under PHP

Questa funzione ci permette di convertire una data GMT in un formato data normale

<?php
function RFC822ToDate($gmtDate, $format="it", $showTime=true){
        list($dayT, $dayN, $monthT, $year, $time) = explode(" ",$gmtDate);
        $dayT = trim($dayT,",");
        $months = array("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
        $monthT = array_search($monthT, $months);
        if(!$showTime) $time = "";
        switch($format){
            case "it":
                $date = $dayN."-".$monthT."-".$year." ".$time ;
                break;
            case "us":
                $date = $year."-".$monthT."-".$dayN." ".$time ;
                break;
            default:
                $date = $year."-".$monthT."-".$dayN." ".$time ;
                break;                
        }
        return $date;
    }
echo RFC822ToDate("Tue, 08 Nov 2014 06:47:10 GMT"); 
//stampa 08-11-2014 06:47:10   
?>
Leave a Comment more...

Leggere feed rss con DOM XML php

by info@fabioricali.it on giu.09, 2010, under PHP

Questa classe ci permette di leggere un RSS sfruttando la libreria DOM xml introdotta da PHP 5.
Il metodo principale è getRss con tre paramentri:

$urlXml (url al file xml)
getRss(“http://sito.it/rss.xml”)

$index di default è null (un numero corrispondente ad un nodo, es: se volessimo estrarre il nodo 5)
getRss(“http://sito.it/rss.xml”,5)

$offset di default é null (imposta assieme a $index il range di nodi da estarre, es: dal nodo 1 al 5)
getRss(“http://sito.it/rss.xml”,1,5)

<?php
/**
 * rss
 * 
 * @package RssNow
 * @author Fabio Ricali
 * @copyright 2010
 * @version 1.0.0
 * @access public
 */
class RssNow{
 
    /**
     * rss::getRss()
     * 
     * @param string $urlXml
     * @param int $index
     * @param int $offSet
     * @return array
     */
    public function getRss($urlXml, $index=null, $offSet=null){
        $doc = new DOMDocument();
        $doc->load($urlXml);
 
        $items = $doc->getElementsByTagName("item");
        $i = 0;
 
        foreach( $items as $item ){
            $i++;
            $titles = $item->getElementsByTagName("title");
            $title = $titles->item(0)->nodeValue;
 
            $pubdates = $item->getElementsByTagName("pubDate");
            $pubdate = $pubdates->item(0)->nodeValue;
 
            $descriptions = $item->getElementsByTagName("description");
            $description = $descriptions->item(0)->nodeValue;
 
            $categories = $item->getElementsByTagName("category");
            $category = $categories->item(0)->nodeValue;
 
            $authors = $item->getElementsByTagName("author");
            $author = $authors->item(0)->nodeValue;
 
            $comments = $item->getElementsByTagName("comments");
            $comment = $comments->item(0)->nodeValue;
 
            $links = $item->getElementsByTagName("link");
            $link = $comments->item(0)->nodeValue;
 
            $enclosures = $item->getElementsByTagName("enclosure");
            foreach($enclosures as $enclosure_){
               $enclosure = $enclosure_->getAttribute("url");
            }
 
            $eval='
                $array[$i][\'title\'] = $title ;
                $array[$i][\'description\'] = $description ;
                $array[$i][\'pubDate\'] = $this->gmtToDate($pubdate) ;
                $array[$i][\'author\'] = $author ;
                $array[$i][\'comments\'] = $comment ;
                $array[$i][\'category\'] = $category ;
                $array[$i][\'enclosure\'] = $enclosure ;
                $array[$i][\'link\'] = $link ;
            ';
 
            if($index==null && $offSet==null){    
                eval($eval);
            }else if($index!=null && $offSet!=null){
                $range = range($index,$offSet);
                if(in_array($i,$range)){
                    eval($eval);
                }
            }else{
                if($i==$index){
                    $eval = str_replace('[$i]','',$eval);
                    eval($eval);
                    return $array;
                }
            }        
        }
        //$array[0]['length'] = $items->length;  
        return $array;
    }
 
    private function gmtToDate($gmtDate, $format="it", $showTime=true){
        list($dayT, $dayN, $monthT, $year, $time) = explode(" ",$gmtDate);
        $dayT = trim($dayT,",");
        $months = array("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
        $monthT = array_search($monthT, $months);
        if(!$showTime) $time = "";
        switch($format){
            case "it":
                $date = $dayN."-".$monthT."-".$year." ".$time ;
                break;
            case "us":
                $date = $year."-".$monthT."-".$dayN." ".$time ;
                break;
            default:
                $date = $year."-".$monthT."-".$dayN." ".$time ;
                break;                
        }
        return $date;
    }     
}
?>

Utilizzo:

<?php
$rss = new RssNow;
$arr = $rss->getRss("http://www.repubblica.it/rss/cronaca/rss2.0.xml");
foreach($arr as $item){
    echo " Titolo: ".$item['title']."<br>";
    echo " Descrizione: ".$item['description']."<br>";
    echo " Data pubbl.: ".$item['pubDate']."<br>";
    echo " Autore: ".$item['author']."<br>";
    echo " Categoria: ".$item['category']."<br>";
}
 
?>
Leave a Comment more...

Redirect in base alla lingua del browser con php

by info@fabioricali.it on mag.16, 2010, under PHP

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang) {
	case "it":
			header("location: it/");
		break;
	case "en":
			header("location: en/");
		break;
	default:
			header("location: en/");
		break;
}
?>
Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...