Tuesday, October 23, 2007

Upload Image


<?
if(isset($_POST['submit'])) { //see if submit button is pressed.

//check if they decided to upload a pic:
if($_FILES['userfile']['size'] > 1) {

$max_size = 100000;
$max_height = 300;
$max_width = 300;

$info = getimagesize($_FILES['userfile']['tmp_name']);

//check file-size (in bytes):
if(($_FILES['userfile']['size'] > $_POST['MAX_FILE_SIZE']) ||
($_FILES['userfile']['size'] > $max_size)) {
die("<BR><BR>Error: Upload file size too large: (<b>" .
$_FILES['userfile']['size'] . "</b>). Must not exceed XX kb.");
}

//check the extension.
$array = explode(".", $_FILES['userfile']['name']);
$nr = count($array);
$ext = $array[$nr-1];
if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png"))
die("<BR><BR>Error: file extension un-recognized.
Be sure your image follows the correct extension (.JPG or .PNG)");

//CHECK TYPE: (what the browser sent)
if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] !=
"image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) {
die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG
images allowed.");
}

//DOUBLE CHECK TYPE: if image MIME type from GD getimagesize()
//-In case it was a FAKE!
if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") &&
($info['mime'] != "image/png")) {
die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG
images allowed.");
}

//check file size (length & width)
if(($info[0] > $max_width) || ($info[1] >$max_height)) {
die("<BR><BR>Error: Image size error (<b>" . $info[0] .
"</b> x <b>" . $info[1] . "</b>). Must not exceed ". $max_height .
" x ". $max_width .".");
}

//rename file, move it to location.
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

//get max number of images the user has uploaded
$m = mysql_query("SELECT max(user_images) as `total_images`
FROM `images` WHERE `user_id` = '".$_SESSION['user_id']."'");
if(!$m) die('An Error Occurred.');
$result = mysql_fetch_object($m);
if($result->total_images <= 0) {
$image_number = 1;
} else {
$image_number = $result->total_images + 1;
} //end if

$filename = strtolower($_SESSION['username']) . $image_number;

if(move_uploaded_file($_FILES['userfile']['tmp_name'] ,
$_SERVER['DOCUMENT_ROOT']."/path/to/image/".$filename . '.' . $ext)) {
echo("File uploaded successfully.");
} else {
echo("An error occurred while uploading.");
}//end upload
} //end is_uploaded_file

} else { //display form ?>

<form enctype="multipart/form-data" action="<? $_SERVER['PHP_SELF']; ?>"
method="post" name="uploadImage" />
<input type="hidden" MAX_UPLOAD_SIZE = "10000" />
<input type="file" name="userfile" size="35" />
<input type="submit" name="submit" value="Upload Image">

<? } //end else ?>

Tuesday, September 18, 2007

php Script to fetch RSS info

/**
* Class name: RSS
* Author : LeadStar Team
* website : http://www.leadstar.com.cn/
* CopyRight : LeadStar Team
*/
if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);

class RSS {
//public
var $rss_ver = "2.0";
var $channel_title = '';
var $channel_link = '';
var $channel_description = '';
var $language = 'zh_CN';
var $copyright = '';
var $webMaster = '';
var $pubDate = '';
var $lastBuildDate = '';
var $generator = 'RedFox RSS Generator';

var $content = '';
var $items = array();

/**************************************************************************/
// function name: RSS
// function description: construct
// parameter: $title
// $link
// $description
/**************************************************************************/
function RSS($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubDate = Date('Y-m-d H:i:s',time());
$this->lastBuildDate = Date('Y-m-d H:i:s',time());
}
/**************************************************************************/
// function name: AddItem
// function description: add a new node
// parameter: $title
// $link
// $description $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array('title' => $title ,
'link' => $link,
'description' => $description,
'pubDate' => $pubDate);
}
/**************************************************************************/
// function name: BuildRSS
// function description: generate rss xml file content
/**************************************************************************/
function BuildRSS() {
$s = "
\n\n";
// start channel
$s .= "\n";
$s .= "channel_title}]]>\n";
$s .= "<1ink>channel_link}]]>\n";
$s .= "channel_description}]]>\n";
$s .= "{$this->language}\n";
if (!empty($this->copyright)) {
$s .= "copyright}]]>\n";
}
if (!empty($this->webMaster)) {
$s .= "webMaster}]]>\n";
}
if (!empty($this->pubDate)) {
$s .= "{$this->pubDate}\n";
}

if (!empty($this->lastBuildDate)) {
$s .= "{$this->lastBuildDate}\n";
}

if (!empty($this->generator)) {
$s .= "{$this->generator}\n";
}

// start items
for ($i=0;$iitems);$i++) {
$s .= "\n";
$s .= "items[$i]['title']}]]>\n";
$s .= "<1ink>items[$i]['link']}]]>\n";
$s .= "items[$i]['description']}]]>\n";
$s .= "{$this->items[$i]['pubDate']}\n";
$s .= "
\n";
}

// close channel
$s .= "
\n
";
$this->content = $s;
}
/**************************************************************************/
// function name: Show
// function description: print out the RSS xml file content
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// function name: SaveToFile
// function description: save the RSS content to a file
// parameter: $fname the file name to be saved
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}

Saturday, September 8, 2007

Ajaxian.com 2006 Survey Results

Ajaxian.com 2006 Survey Results

The results of our second annual Ajaxian.com survey, prepared by Richard Monson-Hafael from the Burton Group, are in. And the winner is… Prototype, the most popular Ajax framework, by a considerable margin: 43% of you use it. Script.aculo.us is next, at 33%, confirming observations that many of made of the popularity of that duo.

The full results of our framework survey follow:

Framework Survey Results

Note that multiple responses per participant were allowed; we’ve also thrown out any result with less than 3% of responses in the above graphic.

We also asked you about the server-side platform you’re using. The big winner here was PHP, with 50% of you using it:

Platform Survey Results

Some other interesting factoids:

  • 25% of you eschew frameworks and work with XMLHttpRequest directly (wow!)
  • 11% of you are using JSON to transfer data; unfortunately, we didn’t ask enough questions to determine how this compares with XML or other data formats
  • 3% of you are still using Microsoft’s “classic” ASP framework; five of you (~0.6%) are using C++ (+2 points for increased performance, -100 for increased complexity?)
  • 2% of you wrote in to say that you’re using Adobe’s Flex toolkit (hey, those banner ads are working out…); 2% also indicated that they use the Flex/Ajax bridge. Unfortunately, the survey software we used doesn’t let us correlate these entries, so we can only say that 2%-4% of you are using Flex in some way
  • One participant uses Delphi (how’s that working out for you?), and another is using LISP (can we hire you?).

Tuesday, August 28, 2007

getElementsByClassName My Eesy Edition

Take it easy

function getElementsByClassName(className, tag){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";

var elements = (tag == "*" && document.all)? document.all : document.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i
current = elements[i];
if(testClass.test(current.className)){
returnElements.push(current);
}
}
return returnElements;
}

getElementsByClassName Delux Edition

GET it FROM http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/

function getElementsByClassName(strClass, strTag, objContElm) {
strTag = strTag || "*";
objContElm = objContElm || document;
var objColl = objContElm.getElementsByTagName(strTag);
if (!objColl.length && strTag == "*" && objContElm.all) objColl = objContElm.all;
var arr = new Array();
var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
var arrClass = strClass.split(delim);
for (var i = 0, j = objColl.length; i < j; i++) {
var arrObjClass = objColl[i].className.split(' ');
if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
var c = 0;
comparisonLoop:
for (var k = 0, l = arrObjClass.length; k < l; k++) {
for (var m = 0, n = arrClass.length; m < n; m++) {
if (arrClass[m] == arrObjClass[k]) c++;
if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
arr.push(objColl[i]);
break comparisonLoop;
}
}
}
}
return arr;
}

Tuesday, May 22, 2007

Javascript for updating one windows from another

window.onload = initWindows;

function initWindows() {
if (document.getElementById("childField")) {
document.getElementById("childField").onchange = updateParent;
}
else {
newWindow = window.open("child.html","newWin","status=yes,width=300,height=300");
}
}

function updateParent() {
opener.document.getElementById("msgLine").value = "Hello " + this.value + "!";
}

Saturday, May 12, 2007

PHP DataTable Displaying and Paging



I have made a function to fetch data from database and show it in a HTML table. It's easy to use but hard to read. HTML text has been linked together.




function getResultAsTable ($actions, $result)
{
if ($rowcount =pg_num_rows($result)>0)
{
$resultHTML= "<table border="'1'" align="'center'">\n<tr bgcolor="'#e8eefa'">\n";

//Output the table header
$fieldCount = pg_num_fields($result);
for ($i=0; $i < $fieldCount; $i++)
{
$rowName = pg_field_name($result,$i);
$resultHTML .= "<th valign="'middle'">$rowName</th>\n";
}

$resultHTML .= "<th>Actions</th></tr>\n";

//output the table body
while ($row = pg_fetch_row($result))
{
$resultHTML .="\n";
for ($i=0; $i < $fieldCount; $i++)
{
$resultHTML .="<td>".htmlentities($row[$i])."</td>\n";
}

//Replace VALUE with proper primary key
$action = str_replace("VALUE", $row[0], $actions);
//Add action cell to the end of each row
$resultHTML .= "<td>$action</td>\n</tr>\n";

}

//Close table
$resultHTML .="</table>\n";
}
else
{
$resultHTML = "No Result Found";
}
return $resultHTML;
}


function pageLinks($totalpages, $currentpage, $pagesize, $parameter)
{
// Start at page one
$page = 1;
// Start at record 0
$recordstart = 0;

// Initialize page links
$pageLinks = '';
while ($page <= $totalpages) {
// Link the page if it isn't the current one
if ($page != $currentpage) {
$pageLinks .= "<a href="http://www.blogger.com/%5C"
parameter="$recordstart\">$page</a> ";
// If the current page, just list the number
}
else {
$pageLinks .= "$page ";
}
// Move to the next record delimiter
$recordstart += $pagesize;
$page++;
}

return $pageLinks;
}