BillHung.Net


powered by FreeFind     sms

PHP

Include Sub PHP Files

06 Feb 2007

<a href="?page=search">[Search]</a>

<?php
//1.initialize everything
// Get the variable from URL
$page = $_GET[page];


//2.include subpage according to URL arguments
if ( $page == search ){

include("search.php");

}
?>

Uploader

August 21. 2006

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

Reference
PHP File Upload Tutorial

Flickr Picture API

Flickr PHP Alpha Test

strip_tags - Get rid of HTML and PHP tags

Reference: http://us3.php.net/strip-tags
21 Jan 2006

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";

// Allow <p>
echo strip_tags($text, '<p>');
?> 

Output

Test paragraph. Other text
<p>Test paragraph.</p> Other text

strtok - String Token

Reference: http://us3.php.net/manual/en/function.strtok.php

<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");

while ($tok !== false) {
echo "Word=$tok<br />";
$tok = strtok(" \n\t");
}
?>

fwrite - Write to a File

Reference: http://us3.php.net/fwrite

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

?>

Open a Socket and Get Files

Reference: http://us3.php.net/manual/en/function.fsockopen.php

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>

GD - Image Manipulation

Reference:
http://www.phpfreaks.com/tutorials/101/1.php
http://www.icant.co.uk/articles/phpthumbnails/

Text Image

<?php
header ("Content-type: image/jpeg");
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 233, 233);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImageJPEG ($img_handle);
?>

Output:

  The output file size is the same if you use ImagePNG or imageGIF. The output image format will be different.

Thumbnail

Reference: http://www.php.net/manual/en/function.imagecopyresampled.php

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

Output

XML Parser

Reference: http://www.zend.com/zend/tut/tutbarlach.php

Parsing XML with PHP Procedures
・Create the XML parser
・Assign the handlers to the parser.
・Feed the XML file to the parser and parse it.
・Free the parser.

3 Functions of the Handler
・startElement()
・ endElement()
・ characterData()

Flickr API Flickr API PHP Page

Reference
Flickr API http://www.flickr.com/services/api/
Flickr Photo Search API http://www.flickr.com/services/api/flickr.photos.search.html
Photo URL http://www.flickr.com/services/api/misc.urls.html
1ceb6552d2b5595b94a72d4183c8e4e1

//Example returned XML
<photos page="1" pages="4703" perpage="100" total="470265">
    <photo id="84343371" owner="71481771@N00" secret="9f60bc35de" server="37" title="PICT0115" ispublic="1" isfriend="1" isfamily="1" />
</photos>

Format
    http://static.flickr.com/{server-id}/{id}_{secret}.jpg
Example
    http://static.flickr.com/37/84343371_9f60bc35de.jpg

http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=(api_key)&tags=japan

Form

Reference: http://www.php.net/manual/en/tutorial.forms.php

<form action="form.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.

Strpos

Reference: http://www.php.net/manual/en/tutorial.useful.php

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla') !== FALSE) {   //finding the needle "Mozilla" in the haystack $_SERVER['HTTP_USER_AGENT']
echo 'You are using Mozilla.<br />';
}
?>

Array

Source: http://www.php.net/manual/en/language.types.array.php

Simple Array

<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>

    Two Dimensions Array

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>

    Array Short-Hand

<?php
// This array is the same as ...
array(5 => 43, 32, 56, "b" => 12);

// ...this array
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>

    Modify Array

<?php
$arr = array(5 => 1, 12 => 2);

$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script

$arr["x"] = 42; // This adds a new element to
// the array with key "x"

unset($arr[5]); // This removes the element from the array

unset($arr); // This deletes the whole array
?> 

    Re-index Array

<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);

// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);

// Append an item (note that the new key is 5, instead of 0 as you
// might expect).
$array[] = 6;
print_r($array);

// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>  

//Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )

    Loop

<?php
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {
echo "Do you like $color?\n";
}

?>

    Everything in directory

<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>

    Sort an array

<?php
sort($files);
print_r($files);
?>

 

Hello World

25 Dec 2005

<?php
 echo 'Hello World!';
?>

My Installation of PHP 4.4.1 on Windows XP

27 Dec 2005

  This article is a log of how I got Apache 2.0 and PHP 4.4.1 works on my Windows XP. There were many failure attempts, and many binaries for some reasons didn't work at all. This is an example for my installation of PHP 4.4.1, and installation of Apache 2.0 is here.

Install PHP 4.4.1
    Download
        http://www.php.net/downloads.php
        php-4.4.1-installer.exe
    Unzip
        php-4.4.1-Win32.zip to c:/PHP/
    Copy c:/PHP/php4ts.dll to C:/WINDOWS/system32
    Copy c:/PHP/php.ini-dist to C:/WINDOWS/system32/dist.ini
    Open C:\Program Files\Apache Group\Apache2\conf\httpd.conf
    Added 2 lines of code in appropriate places
        LoadModule php4_module "c:/PHP/sapi/php4apache2.dll"
            //Notice for apache 2, it is php4apache2.dll, for apache 1.x, it is php4apache.dll
        AddType application/x-httpd-php .php .php3 .php4 .phtml
    Made a file in C:\Program Files\Apache Group\Apache2\htdocs\test.php
        put <?php phpinfo(); ?> inside only
    Open the Browser
        open http://localhost/test.php
            //this should show you info about the PHP you installed. An indication of successful install   

Reference

    1. A Tutorial, but not everything works as described
        http://www.mjmwired.net/resources/mjm-apache-windows.html
    2. Linux install of apache 2 and php, since my apache 2 didn't work on Cygwin, so I didn't do most of the things described
         http://dan.drydog.com/apache2php.html
    3. Another Apache 2 and PHP installation Guide
        http://www.petefreitag.com/item/516.cfm
    4. PHP.net Official guidline for installation on Apache
        http://www.php.net/manual/en/install.unix.php#install.unix.apache   

Hosting RSS Parser

Hosting RSS Parser Code from http://jade.mcli.dist.maricopa.edu/feed/index.php?s=build
Working Version at bill_hung_photo_blog.htm, which is XML from flickr.com