Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Wednesday, June 10, 2009

Multiple Upload

Create file multiple_upload.php



<table width="500" border="0" align="center" cellpadding="0" cellspacing="1"
bgcolor="#CCCCCC">

<tr>

<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data"
name="form1" id="form1">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td><strong>multiple Files Upload </strong></td>

</tr>

<tr>

<td>Select file

<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>

</tr>

<tr>

<td>Select file

<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>

</tr>

<tr>

<td>Select file

<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>

</tr>

<tr>

<td align="center"><input type="submit" name="Submit" value="Upload" /></td>

</tr>

</table>

</td>

</form>

</tr>

</table>





Create file multiple_upload_ac.php



############### Code



<?php

//set where you want to store files

//in this example we keep file in folder upload

//$HTTP_POST_FILES['ufile']['name']; = upload file name

//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];

$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];

$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];



//copy file to where you want to store file

copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);

copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);

copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);



//$HTTP_POST_FILES['ufile']['name'] = file name

//$HTTP_POST_FILES['ufile']['size'] = file size

//$HTTP_POST_FILES['ufile']['type'] = type of file

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";

echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";

echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";

echo "<img src=\"$path1\" width=\"150\" height=\"150\">";

echo "<P>";



echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";

echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";

echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";

echo "<img src=\"$path2\" width=\"150\" height=\"150\">";

echo "<P>";



echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";

echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";

echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";

echo "<img src=\"$path3\" width=\"150\" height=\"150\">";



///////////////////////////////////////////////////////



// Use this code to display the error or success.



$filesize1=$HTTP_POST_FILES['ufile']['size'][0];

$filesize2=$HTTP_POST_FILES['ufile']['size'][1];

$filesize3=$HTTP_POST_FILES['ufile']['size'][2];



if($filesize1 && $filesize2 && $filesize3 != 0)

{

echo "We have recieved your files";

}



else {

echo "ERROR.....";

}



//////////////////////////////////////////////



// What files that have a problem? (if found)



if($filesize1==0) {

echo "There're something error in your first file";

echo "<BR />";

}



if($filesize2==0) {

echo "There're something error in your second file";

echo "<BR />";

}



if($filesize3==0) {

echo "There're something error in your third file";

echo "<BR />";

}



?>



1. Create file multiple_upload.php

2. Create file multiple_upload_ac.php

3. Create folder "upload" for store uploaded files.

4. CHMOD your upload folder to "777" by using your ftp software(change
permission).

Wednesday, March 5, 2008

PAGING WITH PHP

<html>

<head>

<title>Implementing Paging with next and prev</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<?php

include '../library/config.php';

include '../library/opendb.php';


// how many rows to show per page

$rowsPerPage = 20;


// by default we show first page

$pageNum = 1;


// if $_GET['page'] defined, use it as page number

if(isset($_GET['page']))

{

$pageNum = $_GET['page'];

}


// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;


$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";

$result = mysql_query($query) or die('Error, query failed');


// print the random numbers

while($row = mysql_fetch_array($result))

{

echo $row['val'] . '<br>';

}

echo '<br>';


// how many rows we have in database

$query = "SELECT COUNT(val) AS numrows FROM randoms";

$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];


// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);


// print the link to access each page

$self = $_SERVER['PHP_SELF'];

$nav = '';

for($page = 1; $page <= $maxPage; $page++)

{

if ($page == $pageNum)

{

$nav .= " $page "; // no need to create a link to current page

}

else

{

$nav .= " <a href=\"$self?page=$page\">$page</a> ";

}

}


// creating previous and next link

// plus the link to go straight to

// the first and last page


if ($pageNum > 1)

{

$page = $pageNum - 1;

$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";



$first = " <a href=\"$self?page=1\">[First Page]</a> ";

}

else

{

$prev = '&nbsp;'; // we're on page one, don't print previous link

$first = '&nbsp;'; // nor the first page link

}


if ($pageNum < $maxPage)

{

$page = $pageNum + 1;

$next = " <a href=\"$self?page=$page\">[Next]</a> ";



$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";

}

else

{

$next = '&nbsp;'; // we're on the last page, don't print next link

$last = '&nbsp;'; // nor the last page link

}


// print the navigation link

echo $first . $prev . $nav . $next . $last;


// and close the database connection

include '../library/closedb.php';

?>

</body>

</html>

PAGING WITH PHP

<html>

<head>

<title>Implementing Paging with next and prev</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<?php

include '../library/config.php';

include '../library/opendb.php';


// how many rows to show per page

$rowsPerPage = 20;


// by default we show first page

$pageNum = 1;


// if $_GET['page'] defined, use it as page number

if(isset($_GET['page']))

{

$pageNum = $_GET['page'];

}


// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;


$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";

$result = mysql_query($query) or die('Error, query failed');


// print the random numbers

while($row = mysql_fetch_array($result))

{

echo $row['val'] . '<br>';

}

echo '<br>';


// how many rows we have in database

$query = "SELECT COUNT(val) AS numrows FROM randoms";

$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];


// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);


// print the link to access each page

$self = $_SERVER['PHP_SELF'];

$nav = '';

for($page = 1; $page <= $maxPage; $page++)

{

if ($page == $pageNum)

{

$nav .= " $page "; // no need to create a link to current page

}

else

{

$nav .= " <a href=\"$self?page=$page\">$page</a> ";

}

}


// creating previous and next link

// plus the link to go straight to

// the first and last page


if ($pageNum > 1)

{

$page = $pageNum - 1;

$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";



$first = " <a href=\"$self?page=1\">[First Page]</a> ";

}

else

{

$prev = '&nbsp;'; // we're on page one, don't print previous link

$first = '&nbsp;'; // nor the first page link

}


if ($pageNum < $maxPage)

{

$page = $pageNum + 1;

$next = " <a href=\"$self?page=$page\">[Next]</a> ";



$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";

}

else

{

$next = '&nbsp;'; // we're on the last page, don't print next link

$last = '&nbsp;'; // nor the last page link

}


// print the navigation link

echo $first . $prev . $nav . $next . $last;


// and close the database connection

include '../library/closedb.php';

?>

</body>

</html>

PHP MYSQL SEARCH

//search.htm page

<html>

<body>

Search:

<form method='post' action='search.php'>

<input type='text' name='search' size='25' maxlength='25'>

<input type='Submit' name=' Search ' value='Submit'>

</form>

</body>

</html>

//search.php page


<?php


$host="Host";

$username="Username";

$password="Password";

$db_name="Database Name";

$tbl_name="Table Name";


mysql_connect("$host", "$username", "$password")or die("Cannot connect");

mysql_select_db("$db_name")or die("Cannot select database");


$search=$_POST["search"];


search

$result = mysql_query("SELECT * FROM $tbl_name WHERE name LIKE '%$search%'");


while($r=@mysql_fetch_array($result))

{


//change the names to your columns

$topic=$r["topic"];

$message=$r["message"];

$name=$r["name"];

$date=$r["date"];

$id=$r["id"];



//display the row

echo "<b>$id</b> | <b>$topic</b> by <b>$name</b> at <b>$date</b><br><br>$message";

}

if(!search){

echo "Please enter a search.";

}

else

{

echo "Sorry, there were no searches with the criteria of <b>$search</b>.";

}

?>

Resize image

<?php

$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db('test', $conn) or die(mysql_error());

if(@$_FILES['userfile']['tmp_name']){

resize();

$q = "INSERT INTO test VALUES ('', '".$blob_arr['image']."', '".$blob_arr['thumb']."', '".$blob_arr['type']."')";

$aa = mysql_query($q,$conn) or die(mysql_error());


}


function resize(){

global $blob_arr;

$temp_name =$_FILES['userfile']['tmp_name'];

$userfile_name =$_FILES['userfile']['name'];

$userfile_size =$_FILES['userfile']['size'];

$userfile_type =$_FILES['userfile']['type'];


$thumb_width=90;

$thumb_height=90;

$image_width=300;

$image_height=400;


if (!($userfile_type =="image/pjpeg" OR $userfile_type =="image/jpeg" OR $userfile_type=="image/gif" OR $userfile_type=="image/png" OR $userfile_type=="image/x-png")){

die ("You can upload just images in .jpg .jpeg .gif and .png format!<br / >");

}

$data = fread(fopen($temp_name, "rb"), filesize($temp_name));

$src_image = imagecreatefromstring($data);

$width = imagesx($src_image);

$height = imagesy($src_image);

if ($thumb_width && ($width < $height)) {

$thumb_width = ($thumb_height / $height) * $width;

} else {

$thumb_height = ($thumb_width / $width) * $height;

}

if ($image_width && ($width < $height)) {

$image_width = ($image_height / $height) * $width;

} else {

$image_height = ($image_width / $width) * $height;

}

$dest_img = imagecreatetruecolor($thumb_width, $thumb_height);

$i_dest_img = imagecreatetruecolor($image_width, $image_height);

imagecopyresized($dest_img, $src_image,0, 0, 0, 0,$thumb_width, $thumb_height,$width, $height);

imagecopyresized($i_dest_img, $src_image,0, 0, 0, 0,$image_width, $image_height,$width, $height);

ob_start();

if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){

imagejpeg($dest_img);

}

if($userfile_type == "image/gif"){

imagegif($dest_img);

}

if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){

imagepng($dest_img);

}

$binaryThumbnail = ob_get_contents();

ob_end_clean();

ob_start();

if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){

imagejpeg($i_dest_img);

}

if($userfile_type == "image/gif"){

imagegif($i_dest_img);

}

if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){

imagepng($i_dest_img);

}

$binaryImage = ob_get_contents();

ob_end_clean();

if(!get_magic_quotes_gpc()){

$binaryThumbnail=addslashes($binaryThumbnail);

$binaryImage=addslashes($binaryImage);

}

$blob_arr['image']=$binaryImage;

$blob_arr['thumb']=$binaryThumbnail;

$blob_arr['type']=$userfile_type;

}

if(@$_POST['submit']){

$Rs = mysql_query("SELECT * FROM test WHERE id = '".mysql_insert_id()."'", $conn) or die(mysql_error());

$row_Rs = mysql_fetch_assoc($Rs);

header( "Content-type: ".$row_Rs['filetype']."");

echo $row_Rs['thumb'];

}else{

?>

<form name="form1" method="post" enctype="multipart/form-data" action="<? echo @$PHP_SELF; ?>">

<input id="userfile" name="userfile" type="file" />

<input type="submit" name="submit" value="Submit" />

</form>

<?

}

?>