Archive

Archive for the ‘PHP Tips’ Category

PHP – Create directory

August 16, 2008 Adeel Ansari Leave a comment

PHP – Create directory

$restna=”100001″;
function FtpMkdir($path, $newDir) {

$server=’ftp.doubledots.com’; // ftp server
$connection = ftp_connect($server); // connection

// login to ftp server
$user = “me”;
$pass = “pass”;
$result = ftp_login($connection, $user, $pass);

// check if connection was made
if ((!$connection) || (!$result)) {
return false;
exit();
} else {
ftp_chdir($connection, $path); // go to destination dir
if(ftp_mkdir($connection,$newDir)) { // create directory
return $newDir;
} else {
return false;
}
ftp_close($conn_id); // close connection
}

}
FtpMkdir(“/public_html/Restaurant/images/Restaurant”, $restna) ;

PHP – File Upload

August 16, 2008 Adeel Ansari Leave a comment

PHP – File Upload

$uploaddir = ‘images/Restaurant/’.$restna.’/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
echo($uploadfile);
print “<pre>”;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$msg=”File Uploaded Successfully”;
} else {
$msg=”File Uploaded Faild! <br>Please try again” ;
}
print “</pre>”;

PHP – Image Resize

August 16, 2008 Adeel Ansari Leave a comment

PHP – Image Resize

// Config Options
$method = “gd2″;                 // Which method you wish to use to generate the thumbnail
// only GD1 and GD2 are spported.
$new_size = “128″;               // Width of the thumbnail
$jpeg_qual = “80″;               // Quality of the generated thumbnail.
$file_mode = “0644″;             // File permissions of the thumbnail default setting is 0644.
// Chaning it may cause permission problems//
$SessionID = md5( uniqid( rand () ) );
$original_pic = “images/Restaurant/”.$restna.”/”.$_FILES['userfile']['name'];
$resized_pic = $pathname.$SessionID.$_FILES['userfile']['name'];
$newname1 =$SessionID.$_FILES['userfile']['name'];
$newname=list($imgdot,$imgdot1) = split(‘[..-]‘, $newname1);
if($imgdot1==”gif” || $imgdot1==”GIF”)
{
$imgdot1=$_FILES['userfile']['name'];
}
else
{
$imgdot1=$newname1;
}
resize_image($original_pic, $resized_pic, $new_size, $method);
// Modified from Coppermine gallery
function resize_image($src_file, $dest_file, $new_size, $method) {
global $CONFIG, $method, $file_mode, $jpeg_qual;
$imginfo = getimagesize($src_file);
if ($imginfo == null)
return false;
// GD can only handle JPG & PNG images
if ($imginfo[2] != 2 && $imginfo[2] != 3 && ($method == ‘gd1′ || $method == ‘gd2′)){
//echo “Invalid filetype”;
return false;
}
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
$ratio = max($srcWidth, $srcHeight) / $new_size;
$ratio = max($ratio, 1.0);
$destWidth = (int)($srcWidth / $ratio);
$destHeight = (int)($srcHeight / $ratio);
// Method for thumbnails creation
switch ($method) {
case “gd1″ :
if (!function_exists(‘imagecreatefromjpeg’)) {
die(“PHP running on your server does not support the GD image library, check with your webhost if they can setup PHP with GD Library.”);
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
//echo “Invalid_image format!”;
return false;
}
$dst_img = imagecreate($destWidth, $destHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
case “gd2″ :
if (!function_exists(‘imagecreatefromjpeg’)) {
die(“PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed”);
}
if (!function_exists(‘imagecreatetruecolor’)) {
die(“PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page”);
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
// echo “invalid_image”;
return false;
}
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
}
// Set mode of uploaded picture
chmod($dest_file, octdec($file_mode));
// We check that the image is valid
$imginfo = getimagesize($dest_file);
if ($imginfo == null){
echo “resize_failed”;
@unlink($dest_file);
return false;
} else {
return true;
}
}

Image handling| list image folder – PHP

August 16, 2008 Adeel Ansari Leave a comment

Image handling| list image folder – PHP

<?php

$imgdir = ‘/public_html/logs/’; // the directory, where your images are stored

$allowed_types = array(‘png’,'jpg’,'jpeg’,'gif’); // list of filetypes you want to show

$dimg = opendir($imgdir);

while($imgfile = readdir($dimg))

{

if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))

{

$a_img[] = $imgfile;

sort($a_img);

reset ($a_img);

}

}

$totimg = count($a_img); // total image number

for($x=0; $x < $totimg; $x++)

{

$size = getimagesize($imgdir.’/’.$a_img[$x]);

// do whatever

$halfwidth = ceil($size[0]/2);

$halfheight = ceil($size[1]/2);

echo ‘name: ‘.$a_img[$x].’ width: ‘.$size[0].’ height: ‘.$size[1].’<br />’;

}

?>

Move / Rename File

August 16, 2008 Adeel Ansari Leave a comment

Move / Rename File

<?php

rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");

?>

Create Folder Using PHP

August 16, 2008 Adeel Ansari 1 comment

Create Folder Using PHP
mkdir(“Documents”, 0700);

Directory lister

August 15, 2008 Adeel Ansari Leave a comment

Directory lister

Description

Displays a hyperlinked list of all the files contained in a specified folder. A really simple way to have your own directory browser, just drop the file in the folder and it will show an index of all files in the directory on your webspace.

The code

?

/**

* Change the path to your folder.

*

* This must be the full path from the root of your

* web space. If you're not sure what it is, ask your host.

*

* Name this file index.php and place in the directory.

*/

// Define the full path to your folder from root

$path = "/home/user/public/foldername/";

// Open the folder

$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files

while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" )

continue;

echo "<a href=\"$file\">$file</a><br />";

}

// Close

closedir($dir_handle);

?>


Categories: PHP Tips Tags:

Random image

August 15, 2008 Adeel Ansari Leave a comment

Random image

Displays a random image on a web page. Images are selected from a folder of your choice.

<?php

/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* appear: <?php include “randomimage.php”; ?>
*/

// Change this to the total number of images in the folder
$total = “11″;

// Change to the type of files to use eg. .jpg or .gif
$file_type = “.jpg”;

// Change to the location of the folder containing the images
$image_folder = “images/random”;

// You do not need to edit below this line

$start = “1″;

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo “<img src=\”$image_folder/$image_name\” alt=\”$image_name\” />”;

?>

Categories: HTML Tips, PHP Tips Tags:

Display a different image for each day of the week

August 15, 2008 Adeel Ansari Leave a comment

Display a different image for each day of the week

Description
This PHP script will get the day of the week from the server date and then display an image (jpg or gif) to match.
The code

<?php

/**

* Change the name of the image folder

*

* Images must be named Monday.gif, Tuesday.gif etc

*/

// Change to the location of the folder containing the images

$image_folder = "images/days";

// You do not need to edit below this line

$today = date('l');

if (file_exists($image_folder."/".$today.".gif")) {

echo "<img src=\"$image_folder/".$today.".gif\">";

}

else {

echo "No image was found for $today";

}

?>

PHP: Random Quote

August 15, 2008 Adeel Ansari Leave a comment

PHP: Random Quote

Make a file called quotes.txt

Put each quote on it’s on line.

Here’s a script:

<?
$r_array=file(‘quotes.txt’);
shuffle($r_array);
echo $mQuotePath[0];
echo $r_array[0];
?>

Categories: HTML Tips, PHP Tips