Valider l'extension d'un fichier en PHP

function validate_extension($file_name) {

    $ext_array = array(".zip",".rar",".jpg",".jpeg",".gif",".bmp");
    $extension = strtolower(strrchr($file_name,"."));
    $ext_count = count($ext_array);


    if (!$file_name) {
        return false;
    } else {
        if (!$ext_array) {
            return true;
        } else {
            foreach ($ext_array as $value) {
                $first_char = substr($value,0,1);
                if ($first_char <> ".") {
                    $extensions[] = ".".strtolower($value);
                } else {
                    $extensions[] = strtolower($value);
                }
            }


            foreach ($extensions as $value) {
                if ($value == $extension) {
                    $valid_extension = "TRUE";
                }
            }

            if ($valid_extension) {
                return true;
            } else {
                return false;
            }
        }
    }
}

MySQL and JSON

toujours dans la suite du tutoriel sur JSON, nous allons extraire des données depuis une base de données MySQL.

Créons maintenant notre table : 

CREATE TABLE  `utilisateur` (
 `id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `nom` VARCHAR( 100 ) NOT NULL ,
 `prenom` VARCHAR( 100 ) NOT NULL ,
 `email` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;

Puis nous allons extraire les donnés depuis la table "utilisateur" et les convertire au format JSON

code PHP:

<?php
   
    define( 'HOST', 'localhost:8889' );
    define( 'USER', 'root' );
    define( 'PASSWORD', 'root' );
    define( 'DATABASE', 'myDB' );   
   
    $link = mysql_connect(HOST, USER, PASSWORD) or die(mysql_erro());
    mysql_select_db(DATABASE);
   
    $query = mysql_query("select * from utilisateur", $link) or die(mysql_error());
    $row = mysql_fetch_assoc($query);
   
    $json_data = array();
   
    do{
       
        array_push($json_data, $row);
       
    }while($row = mysql_fetch_assoc($query));
   
    echo json_encode($json_data);
   
?> 

PHP and JSON

Dans ce tutoriel nous allons créer des données et les convertir au format JSON.

avant de commencer nous allons d'abord vérifier si la bibliothque JSON est chargée sur notre serveur apache

faisant un simple phpinfo() pour afficher toutes les infos de notre serveur puis vérifier si JSON support est enabled

json

code PHP: personne.php

<?php

    class Personne
    {
        public $nom;
        public $prenom;
        public $email;
    }

?>


code PHP: json.php

<?php
    include ('personne.php');
   
    $p = new Personne();
    $p->nom = 'john';
    $p->prenom = 'Doe';
    $p->email = 'johndoe@mail.com';
   
    $p2 = new Personne();
    $p2->nom = 'john 2';
    $p2->prenom = 'Doe 2';
    $p2->email = 'johndoe2@mail.com';
   
    echo json_encode(array($p, $p2));
?>

 

Code Igniter un autre FrameWork php

Code Igniter est Framework php qui integre les possibilités qu'offrent les autres Frameworks(cakePHP, phpMVC, Symfony,...).

codeigniter_logo

Features

  • Model-View-Controller Based System
  • PHP 4 Compatible
  • Extremely Light Weight
  • Full Featured database classes with support for several platforms.
  • Active Record Database Support
  • Form and Data Validation
  • Security and XSS Filtering
  • Session Management
  • Email Sending Class. Supports Attachments, HTML/Text email, multiple protocols (sendmail, SMTP, and Mail) and more.
  • Image Manipulation Library (cropping, resizing, rotating, etc.). Supports GD, ImageMagick, and NetPBM
  • File Uploading Class
  • FTP Class
  • Localization
  • Pagination
  • Data Encryption

afficher les archives de votre blog

Afficher les archives de votre blog sous cette forme

  • Janvier 2007 (10)
  • Décembre 2006 (35)
  • ...

Cela peut parfois sembler compliquer mais non, on peut le faire grâce à une seule requête

Supposons notre table "blog"

CREATE TABLE `blog` (
`id_blog` int(11) NOT NULL auto_increment,
`titre` varchar(150) NOT NULL default '',
`texte` longtext NOT NULL,
`date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id_blog`)
) TYPE=MyISAM ;

Pour afficher les archives :

SELECT DISTINCT YEAR( date ) AS year, MONTH( date ) AS
month , Count( id_blog ) AS posts, MONTHNAME(date) as monthname
FROM blog
WHERE date < now( )
AND date != '0000-00-00 00:00:00'
GROUP BY YEAR( date ) , MONTH( date )
ORDER BY date DESC

Les champs que nous aurons sont : year,month,post,monthname

Reste plus qu'a les placer dans l'ordre : monthname year (post) avec le code php.

ET voici le code php

$query = "SELECT DISTINCT YEAR( date ) AS year, MONTH( date ) AS
month , Count( id_blog) AS posts, MONTHNAME(date) as monthname
FROM blog
WHERE date < now( )
AND date != '0000-00-00 00:00:00'
GROUP BY YEAR( date ) , MONTH( date )
ORDER BY date DESC "
$result = mysql_query($query,$connect);
$row = mysql_fetch_assoc($result);

do {
echo '<li><a href="/site/archives/'.$row['year'].'/'.$row['month'].'/">'.$row['monthname'].' '.$row['year'].' ('.$row['posts'].')</a></li>';
}while($row = mysql_fetch_assoc($result));