snippets / php

All snippets tagged php (14)

  1. PHP random password

    Generates random php password from [0-9a-z] characters.

    1 <?php echo base_convert(rand(0, PHP_INT_MAX) . rand(0, PHP_INT_MAX), 10, 36);
    Posted by alqimantas to php php random password ... saved by 1 person ... 0 comments ... 5 months, 3 weeks
  2. uploadImage

    upload image (et thumbnail si on fournis une largeur pour le thumbnail (3e argument))

     1 function uploadImage($fichier,$largeurImage, $largeurThumbnail = null)
    2 {
    3 if (isset ($fichier)){
    4 $imagename = $fichier['name'];
    5 $source = $fichier['tmp_name'];
    6 $target = "../img/".$imagename;
    7 move_uploaded_file($source, $target);
    8
    9 $imagepath = $imagename;
    10 $save = "../img/" . $imagepath; //This is the new file you saving
    11 $file = "../img/" . $imagepath; //This is the original file
    12
    13 list($width, $height) = getimagesize($file) ;
    14
    15 $modwidth = $largeurImage; // taille image grande
    16
    17 $diff = $width / $modwidth;
    18
    19 $modheight = $height / $diff;
    20
    21 $tn = imagecreatetruecolor($modwidth, $modheight) ;
    22 $image = imagecreatefromjpeg($file) ;
    23 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    24
    25 imagejpeg($tn, $save, 100) ;
    26
    27 $save = "../img/sml_" . $imagepath; //This is the new file you saving
    28 $file = "../img/" . $imagepath; //This is the original file
    29
    30 list($width, $height) = getimagesize($file) ;
    31
    32 $modwidth = $largeurThumbnail; // taille thumbnail
    33 if( $largeurThumbnail){
    34 $diff = $width / $modwidth;
    35
    36 $modheight = $height / $diff;
    37 $tn = imagecreatetruecolor($modwidth, $modheight) ;
    38 $image = imagecreatefromjpeg($file) ;
    39 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    40
    41 imagejpeg($tn, $save, 100) ;
    42 }
    43
    44 //echo "Large image: <img src='img/".$imagepath."'><br>";
    45 //echo "Thumbnail: <img src='img/sml_".$imagepath."'>";
    46
    47 return $imagename;
    48 }
    49 }
    Posted by juliend2 to php php ... saved by 2 persons ... 0 comments ... 6 months
  3. getFrenchMonthByInt

    prends en paramètre un INT du mois, et retourne un String du mois correspondant

     1 function getFrenchMonthByInt($mois)
    2 {
    3 $arrayMois = array( 1=>'Janvier',
    4 2=>'F&eacute;vrier',
    5 3=>'Mars',
    6 4=>'Avril',
    7 5=>'Mai',
    8 6=>'Juin',
    9 7=>'Juillet',
    10 8=>'Ao&ucirc;t',
    11 9=>'Septembre',
    12 10=>'Octobre',
    13 11=>'Novembre',
    14 12=>'D&eacute;cembre'
    15 );
    16
    17 return $arrayMois[$mois];
    18 }
    Posted by juliend2 to php php ... saved by 1 person ... 0 comments ... 6 months
  4. datetimeToArray

    retourne un array : [0] => année , [1] => mois , [2] => jour, [3] => heure, [4]=>minute, [5]=>seconde

     1 function datetimeToArray($dateTimeString)
    2 {
    3 $dateArray= explode(' ', $dateTimeString); // exploser pour prendre juste la date
    4 $date = $dateArray[0];
    5 $heure= $dateArray[1];
    6
    7 $date= explode('-', $date); // exploser pour prendre chacune des 3 parties de la date
    8 $heure= explode(':', $heure);
    9
    10 $dateTimeArray= array_merge($date,$heure);
    11 return $dateTimeArray;
    12 }
    Posted by juliend2 to php php ... saved by 1 person ... 0 comments ... 6 months
  5. convertDateTime

    convertir les années/mois/jour (et peut-être heure et minutes) en format DateTime pour MySQL retourne un String

    1 function convertDateTime($annee, $mois, $jour, $heure=null, $minute=null)
    2 {
    3 if($heure){
    4 return $annee."-".$mois."-".$jour." ".$heure.":".$minute.":01";
    5 }else{
    6 return $annee."-".$mois."-".$jour." 01:01:01";
    7 }
    8 }
    Posted by juliend2 to php php ... saved by 1 person ... 0 comments ... 6 months
  6. selectDateHeure

    echo des select de dates et heures, (année, mois, jour, heure, minute)

     1 function selectDateHeure($annee = null, $mois = null, $jour = null, $heure=null, $minute=null){
    2
    3 if($annee){
    4 selectDate($annee,$mois,$jour);
    5 }else{
    6 selectDate();
    7 }
    8 echo "&nbsp;&nbsp;&nbsp;";
    9 echo '<select name="heure" id="EvenementDateHour">
    10 <option value="" >&nbsp;</option>';
    11
    12 //heure
    13 for($i=0; $i<24; $i++)
    14 {
    15 if($heure !=null && $heure == $i)
    16 {
    17 if($i<10){
    18 echo "<option value='0".$i;
    19 }else{
    20 echo "<option value='".$i;
    21 }
    22 echo "' selected='selected' >".$i."</option>";
    23
    24 }else{
    25 if($i<10){
    26 echo "<option value='0".$i;
    27 }else{
    28 echo "<option value='".$i;
    29 }
    30 echo "' >".$i."</option>";
    31 }
    32 }
    33
    34 echo '</select> : <select name="minute" id="EvenementDateMinute">
    35 <option value="" >&nbsp;</option>';
    36
    37 //mois
    38 for($i=1; $i<60; $i++)
    39 {
    40 if($minute !=null && $minute == $i)
    41 {
    42 if($i<10){
    43 echo "<option value='0".$i;
    44 }else{
    45 echo "<option value='".$i;
    46 }
    47 echo "' selected='selected' >".$i."</option>";
    48 }else{
    49 if($i<10){
    50 echo "<option value='0".$i;
    51 }else{
    52 echo "<option value='".$i;
    53 }
    54 echo "' >".$i."</option>";
    55 }
    56 }
    57
    58 echo '</select>';
    59 }
    Posted by juliend2 to php php ... saved by 1 person ... 0 comments ... 6 months
  7. selectDate

    echo des select de dates, (année, mois, jour)

     1 function selectDate($annee = null, $mois = null, $jour = null){
    2
    3 echo '<select name="annee" id="EvenementDateYear">
    4 <option value="" >&nbsp;</option>';
    5
    6 //année
    7 for($i=2025; $i>1948; $i--)
    8 {
    9 if($annee !=null && $annee == $i)
    10 {
    11 echo "<option value='".$i."' selected='selected' >".$i."</option>";
    12 }else{
    13 echo "<option value='".$i."' >".$i."</option>";
    14 }
    15 }
    16
    17 echo '</select> - <select name="mois" id="EvenementDateMonth">
    18 <option value="" >&nbsp;</option>';
    19
    20 //mois
    21 $moisString= array('Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre');
    22 for($i=1; $i<13; $i++)
    23 {
    24 if($mois !=null && $mois == $i)
    25 {
    26 if($i<10){
    27 echo "<option value='0".$i;
    28 }else{
    29 echo "<option value='".$i;
    30 }
    31 echo "' selected='selected' >".$moisString[$i-1]."</option>";
    32 }else{
    33 if($i<10){
    34 echo "<option value='0".$i;
    35 }else{
    36 echo "<option value='".$i;
    37 }
    38 echo "' >".$moisString[$i-1]."</option>";
    39 }
    40 }
    41
    42 echo '</select> - <select name="jour" id="EvenementDateDay">
    43 <option value="" >&nbsp;</option>';
    44
    45 //jour
    46
    47 for($i=1; $i<32; $i++)
    48 {
    49 if($jour !=null && $jour == $i)
    50 {
    51 if($i<10){
    52 echo "<option value='0".$i;
    53 }else{
    54 echo "<option value='".$i;
    55 }
    56 echo "' selected='selected' >".$i."</option>";
    57 }else{
    58 if($i<10){
    59 echo "<option value='0".$i;
    60 }else{
    61 echo "<option value='".$i;
    62 }
    63 echo "' >".$i."</option>";
    64 }
    65 }
    66
    67 echo '</select>';
    68 }
    Posted by juliend2 to php php ... saved by 1 person ... 0 comments ... 6 months
  8. printer un array

    printer un array

    1 function pr($argument) {
    2 echo "<pre>";
    3 print_r($argument);
    4 echo "</pre>";
    5 }
    Posted by juliend2 to php php ... saved by 2 persons ... 0 comments ... 6 months
  9. Tick system

    This object make a tick system that could make operate some jobs at same time. This is like a cron job, but more simpler.

      1 <?php
    2
    3 class tick {
    4 private $ticks = array();
    5 private $ok;
    6
    7 static public $instance = false;
    8
    9 public static function GetInstance() {
    10 if (!self::$instance) {
    11 self::$instance = new tick();
    12 }
    13 return self::$instance;
    14 }
    15
    16 private function __construct() {
    17 $this->ok = true;
    18 }
    19
    20 /**
    21 * Adding a job to the tick list $tickname
    22 *
    23 * @param string $tickname
    24 * @param string $jobname
    25 * @param string $function Function to call
    26 * @param array $params Parameters send to the $function
    27 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
    28 * @param object $object Reference to an object if $function is a method
    29 * @return boolean
    30 */
    31 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
    32 if (isset($this->ticks[$tickname])) {
    33 if (!is_null($object)) {
    34 $this->ticks[$tickname]['jobs'][] = array(
    35 'jname' => $jobname,
    36 'function' => $function,
    37 'params' => $params,
    38 'temporary' => $temporary,
    39 'objectRef' => &$object
    40 );
    41 } else {
    42 $this->ticks[$tickname]['jobs'][] = array(
    43 'jname' => $jobname,
    44 'function' => $function,
    45 'params' => $params,
    46 'temporary' => $temporary
    47 );
    48 }
    49 return true;
    50 }
    51 return false;
    52 }
    53
    54 /**
    55 * Create a tick if not exist
    56 *
    57 * @param string $name
    58 * @param int $timer Number of seconds before the next tick
    59 * @return boolean
    60 */
    61 public function setTick($name,$timer) {
    62 if (!isset($this->ticks[$name])) {
    63 $this->ticks[$name] = array(
    64 'timer' => $timer,
    65 'reference' => time(),
    66 'tickOn' => strtotime("+$timer seconds"),
    67 'jobs' => array()
    68 );
    69 return true;
    70 }
    71 return false;
    72 }
    73
    74 /**
    75 * Delete a tick
    76 *
    77 * @param string $name
    78 * @return boolean
    79 */
    80 public function delTick($name) {
    81 if (isset($this->ticks[$name])) {
    82 unset($this->ticks[$name]);
    83 return true;
    84 }
    85 return false;
    86 }
    87
    88 public function doAllTicks() {
    89 $curtime = time();
    90
    91 foreach($this->ticks as $name => $options) {
    92
    93 if ($options['tickOn']<=$curtime) {
    94 echo $options['tickOn']," - $curtime\n";
    95
    96 foreach ($options['jobs'] as $functions) {
    97
    98 if (!$functions['temporary']) {
    99 $jlist[] = $functions;
    100 }
    101
    102 if (!$this->ircmain->ok) {
    103 continue;
    104 }
    105
    106 switch (isset($functions['objectRef'])) {
    107 case true:
    108 if (!is_object($functions['objectRef'])) {
    109 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
    110 }
    111 //print_r($functions);break;
    112 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
    113 break;
    114
    115 case false:
    116 call_user_func_array($functions['function'],$functions['params']);
    117 break;
    118 }
    119
    120 }
    121
    122 $options['jobs'] = $jlist;
    123
    124 $this->ticks[$name] = array(
    125 'timer' => $options['timer'],
    126 'reference' => time(),
    127 'tickOn' => strtotime("+{$options['timer']} seconds"),
    128 'jobs' => $options['jobs']
    129 );
    130
    131 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";
    132
    133 }
    134
    135 }
    136
    137 return true;
    138
    139 }
    140 }
    141
    142 ?>
    Posted by Xrogaan to php php tick cron job ... saved by 1 person ... 0 comments ... 9 months, 2 weeks
  10. Generate random password

    A fully password generator.

     1 <?php
    2
    3 /**
    4 * Generate random password
    5 *
    6 * @param integer $length Length of the generated password
    7 * @param boolean $allow_uppercase
    8 * @param boolean $allow_lowercase
    9 * @param boolean $allow_numbers
    10 * @param boolean $allow_special
    11 * @param boolean $fix_similar If true, check if string contains mistakeable chars, add if accepted
    12 * @param string $valid_charset
    13 * @return string
    14 */
    15 function rpassword($length = 8, $allow_uppercase = 1, $allow_lowercase = 1, $allow_numbers = 1, $allow_special = 0, $fix_similar = 0, $valid_charset = "") {
    16 // Create a list of usable chars based upon the parameters
    17 if (!$valid_charset) {
    18 if ($allow_uppercase) $valid_charset .= 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
    19 if ($allow_lowercase) $valid_charset .= 'abcdefghijklmnopqrstuvxyz';
    20 if ($allow_numbers) $valid_charset .= '0123456789';
    21 if ($allow_special) $valid_charset .= '!#$%&()*+-./;<=>@\_';
    22 }
    23 // Find the charset length
    24 $charset_length = strlen($valid_charset);
    25 // If no chars is allowed, return false
    26 if ($charset_length == 0) return false;
    27 // Initialize the password and loop till we have all
    28 $password = "";
    29 while(strlen($password) < $length) {
    30 // Pull out a random char
    31 $char = $valid_charset[mt_rand(0, ($charset_length-1))];
    32
    33 if (($fix_similar && !strpos('O01lI5S', $char)) || !$fix_similar) $password .= $char;
    34 }
    35
    36 return $password;
    37 }
    Posted by Xrogaan to php generate password random customizable ... saved by 2 persons ... 0 comments ... 10 months, 2 weeks
showing 10, 25, 50 items per pages

Pages : 1 2

Flux RSS friendsnippetLatest snippets


More...