PHP Snippets
« 1 2 3 »random_sig
Information
(PHP Version >= 4.3.0)Functions used in this snippet: array_rand, getimagesize, header, image_type_to_mime_type, readfile, is_numeric.
Description
mixed random_sig( bool image )
If the optional image arguement is left as true, random_sig will output a random image. If image is set to false it will output the html tags for an image, and link if a link is supplied.
(note you cannot have more than one image pointing to the same link)
Snippet
<?php
function random_sig($image=true){
$array = array( 'http://www.google.com' => 'google.gif',
'someimage.jpg',
'anotherimage.png');
$rand = array_rand($array);
$size = getimagesize($array[$rand]);
if($image){
header('Content-type: ' . image_type_to_mime_type($size[2]));
readfile($array[$rand]);
exit;
}else{
if(!is_numeric($rand)){
return '<a href="' . $rand . '"><img src="' . $array[$rand] . '" alt="" /></a>';
}else{
return '<img src="' . $array[$rand] . '" alt="" />';
}
}
return false;
}
?>
Example
random_sig Can be used in the following way:
<?php
echo random_sig(false); // outputs <img src="" /> etc
random_sig(); // outputs an actual image.
// the above can be used like <img src="your_file_name.php" />
?>
get_ext
Information
(PHP Version >= 3)Functions used in this snippet: substr, strrpos.
Description
str get_ext( str str[, bool ext] )
If ext is left as false, this function returns str with its file extension taken away.
If ext is set to true, only the extension is returned.
Snippet
<?php
function get_ext($str,$ext=false){
if($ext){
return substr($str,strrpos($str,'.')+1);
}
return substr($str,0,strrpos($str,'.'));
}
?>
Example
get_ext Can be used in the following way:
<?php
echo get_ext('somefile.php'); // somefile
echo get_ext('package.zip', true) // zip
?>
filesize_tag
Information
(PHP Version >= 4)Functions used in this snippet: floor, array_key_exists, implode, array_reverse, sizeof.
Description
str filesize_tag( int size[, bool full] )
Takes a filesize of size and turns it into its textual form. If the optional field full is set to true, the complete filesize will be returned.
Snippet
<?php
function filesize_tag($size,$full=false){
$sizes = array();
$tags = array(' B', ' KB', ' MB', ' GB', ' TB');
$i = 0;
$sizes[$i] = $size;
$i++;
while($size > 1024){
$size = floor($size / 1024);
$sizes[$i] = $size;
$i++;
}
foreach($sizes as $key=>$size){
$subtract = array_key_exists(($key + 1), $sizes) ? $sizes[$key + 1] * 1024 : 0;
$sizes[$key] = $sizes[$key] - $subtract . $tags[$key];
}
return $full ? implode(', ',array_reverse($sizes)) : $sizes[sizeof($sizes) - 1];
}
?>
Example
filesize_tag Can be used in the following way:
<?php
echo filesize_tag(123456); // 120 KB
echo filesize_tag(123456789); // 117 MB
echo filesize_tag(12345,true); // 12 KB, 57 B
echo filesize_tag(filesize('somefile.txt')); // size of the file...
?>
upload_files
Information
(PHP Version >= 4.0.3)Functions used in this snippet: strlen, is_array, is_dir, is_writable, isset, sizeof, in_array, substr, strtolower, strrpos, move_uploaded_file.
Description
array upload_files( str form, str location, mixed types )
Upload any file that matches types filetypes, and send the files from the form name form to the file location of location.
form is the name of the form from the page where the uploads are coming from. This is the only required arguement to the function.
location defaults to the current working directory, but it can be set to any other directory that can be written to.
types are filetype extensions that are allowed. These must be lower case. The types can be either a array or a str. If you want to set types but leave the default location you can set location to NULL
The function returns an array of what happened with the upload. The returned values are mimetype, error code, whether the file was uploaded, file matches the types, filename, and filesize.
Note: This only checks file extensions. You may want to add additional checks of mimetypes to restrict data being loaded onto your server.
Snippet
<?php
function upload_files($form, $location='./', $types=array()){
$ret = array();
$location = $location === NULL || $location === '' ? './' : $location;
$location = $location{(strlen($location) - 1)} === '/' ? $location : $location . '/';
$types = is_array($types) ? $types : array($types);
if(!is_dir($location) || !is_writable($location) || !isset($_FILES[$form])){
return;
}
if(is_array($_FILES[$form]['error'])){
foreach($_FILES[$form]['error'] as $key=>$value){
$ret[$key] = array('uploaded'=>false, 'error'=>$value, 'typematch'=>false, 'name'=>$_FILES[$form]['name'][$key], 'mime'=>$_FILES[$form]['type'][$key], 'size'=>$_FILES[$form]['size'][$key]);
if($value == UPLOAD_ERR_OK) {
$ret[$key]['uploaded'] = true;
if(sizeof($types) === 0 || in_array(substr($_FILES[$form]['name'][$key], strtolower(strrpos($_FILES[$form]['name'][$key], '.')+1)), $types)){
move_uploaded_file( $_FILES[$form]['tmp_name'][$key], $location . $_FILES[$form]['name'][$key]);
$ret[$key]['typematch'] = true;
}
}
}
}else{
$ret = array('uploaded'=>false, 'error'=>$_FILES[$form]['error'], 'typematch'=>false, 'name'=>$_FILES[$form]['name'], 'mime'=>$_FILES[$form]['type'], 'size'=>$_FILES[$form]['size']);
if($_FILES[$form]['error'] == UPLOAD_ERR_OK) {
$ret['uploaded'] = true;
if(sizeof($types) === 0 || in_array(strtolower(substr($_FILES[$form]['name'], strrpos($_FILES[$form]['name'], '.')+1)), $types)){
move_uploaded_file( $_FILES[$form]['tmp_name'], $location . $_FILES[$form]['name']);
$ret['typematch'] = true;
}
}
}
return $ret;
}
?>
Example
upload_files Can be used in the following way:
<?php
// make form as a single file upload with formname 'file'
$files = upload_files('file');
// uploads the single file to the current directory no matter the filetype
// make form as a single upload with formname 'image'
$files = upload_files('image', './images/', array('gif', 'jpg', 'jpeg', 'png'));
// uploads the single file to the images directory with file extensions of gif, jpg, jpeg, or png
// make a form as an array upload with formname 'images'
$files = upload_files('images',NULL,'png');
// uploads multiple files to the current directory as long as the file extension is png
?>
site_is_up
Information
(PHP Version >= 3)Functions used in this snippet: fsockopen, str_replace, fclose.
Description
bool site_is_up( str site, int port0, int timeout )
checks site on port port to see if the site is accepting connections (whether the site is up or not). If site is down it will take [i]timeout[i] seconds to timeout. Returns true on success and false on failure.
Snippet
<?php
function site_is_up($site,$port=80,$timeout=1){
$port = $port === NULL ? 80 : $port;
$fp = @fsockopen(str_replace('http://','',$site), $port, $errno, $errstr, $timeout);
if($fp === false){
return false;
}
fclose($fp);
return true;
}
?>
Example
site_is_up Can be used in the following way:
<?php
if(site_is_up('www.bigtoach.com')){
echo 'Site is up!!';
}else{
echo 'Site is down.';
}
// self explanitory I hope
if(site_is_up('bigtoach.com',NULL,5)){ echo 'UP!'; }
// check bigtoach.com with a 5 second timeout (still on port 80)
if(site_is_up('bigtoach.com',2082)){ echo 'UP!'; }
// check bigtoach.com on port 2082
?>
« 1 2 3 »