Thursday, 26 May 2016

PHP - Display script memory usage

1 Byte = 8 Bit
1 Kilobyte = 1024 Bytes
1 Megabyte = 1048576 Bytes
1 Gigabyte = 1073741824 Bytes 

echo "Memory Usage: " . (memory_get_usage()/1048576) . " MB \n";

 

Wednesday, 25 May 2016

MySqlDump export and MySQL import

To export data as a backup you can’t beat mysqldump.
# mysqldump -u username database > database_2007-05-14.sql



Then to restore a database you would use:
# mysql -u username -p database < database_2007-05-14.sql

Getting differences between dates quickly in PHP or MySQL



Using PHP:
<?php
$expireDate = "2006-02-07";
$year = substr($expireDate, 0, 4);
 $month = substr($expireDate, 5, 2);
$day = substr($expireDate, 8, 2);
$splitExpireDate = (mktime(0, 0, 0, $month, $day, $year));
$today = (mktime(0, 0, 0, date("m"), date("d"), date("Y")));
 $difference = (($today) - ($splitExpireDate));
$convertToDays = ($difference/86400); echo $convertToDays;
?>
Using MySQL:
SELECT (TO_DAYS(expire_date) - TO_DAYS(CURDATE())) AS days_expired FROM tablename;

Wednesday, 11 May 2016

Multiple Files Simultaneous Upload Array Programming Tutorial


Learn to program PHP multiple file upload applications. Which is the art of uploading many files as an array in a single form request, and moving them to your server all at once. At the end of the lesson we link you to an Ajax and PHP file upload application with HTML5 progress bar included that you can take to experiment with sending files through Ajax. We also discuss your server limits regarding how many files can your specific server can upload simultaneously and the size the files can be. Each server can be set differently by the server administrator using the php.ini configuration file.


example.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form action="my_parser.php" method="post" enctype="multipart/form-data"> <p><input type="file" name="file_array[]"></p> <p><input type="file" name="file_array[]"></p> <p><input type="file" name="file_array[]"></p> <input type="submit" value="Upload all files"> </form> </body> </html> my_parser.php <?php // http://www.youtube.com/watch?v=7fTsf80RJ5wif(isset($_FILES['file_array'])){ $name_array = $_FILES['file_array']['name']; $tmp_name_array = $_FILES['file_array']['tmp_name']; $type_array = $_FILES['file_array']['type']; $size_array = $_FILES['file_array']['size']; $error_array = $_FILES['file_array']['error']; for($i = 0; $i < count($tmp_name_array); $i++){ if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){ echo $name_array[$i]." upload is complete<br>"; } else { echo "move_uploaded_file function failed for ".$name_array[$i]."<br>"; } } } ?>

Wednesday, 4 May 2016

PHP - get all keys from a array that start with a certain string

Solution:$unprefixed_keys = preg_filter('/^foo-(.*)/', '$1', array_keys( $arr ));


Example:

<?php
$arr_main_array = array('foo-test' => 123, 'other-test' => 456, 'foo-result' => 789);
$unprefixed_keys = preg_filter('/^foo-(.*)/', '$1', array_keys( $arr_main_array
));
print_r($unprefixed_keys);
?>

Wednesday, 24 February 2016

PHP mp3 Audio file upload

<?php
/*
ini_set("error_display",1);

ini_set('memory_limit', '32M');
ini_set('upload_max_filesize', '24M');
ini_set('post_max_size', '32M');

//echo ini_get('upload_max_filesize');
*/

$uploadpath = 'upload/';      // directory to store the uploaded files
$max_size = 30000;          // maximum file size, in KiloBytes
$alwidth = 900;            // maximum allowed width, in pixels
$alheight = 800;           // maximum allowed height, in pixels
$allowtype = array('wav', 'mp3');        // allowed extensions

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
// print_r($_FILES['fileup']);
// exit();
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));
  $type = end($sepext);       // gets extension
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';         // to store the errors

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\\\/').'/'.$uploadpath.'</b>';
    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}
?> 
<div style="margin:1em auto; width:333px; text-align:center;">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
  Upload File: <input type="file" name="fileup" /><br/>
  <input type="submit" name='submit' value="Upload" /> 
 </form>
</div>