PHP – Create directory

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) ;

Advertisements

PHP – File Upload

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

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

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 />’;

}

?>

Directory lister

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);

?>