Learning PHP and MySQL
Saturday, August 19, 2006
Example 11-27 Checking the file type
Example 11-27 checks the file type to make sure it’s either a JPEG or a GIF file.
<?php
if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND
$HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND
$HTTP_POST_FILES['upload_file']['type'] !="image/jpeg") {
$error = "You may only upload .gif or .jpeg files";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
} else {
//the file is the correct format
}
?>
The following line copies the file from the temporary directory into the uploads direc- tory using the same filename.
copy($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".
$HTTP_POST_FILES['upload_file']['name']);
Using unlink, let’s remove the temporary file.
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
Chapter 11 Code • (1) Comments • (0) Trackbacks • Permalink
Example 11-28 Processing an uploaded file
<?php
$maxsize=28480; //set the max upload size in bytes
if (!$HTTP_POST_VARS['submit']) {
//print_r($HTTP_POST_FILES);
$error=" ";
//this will cause the rest of the processing to be skipped
//and the upload form displays
}
if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
$error = "<b>You must upload a file!</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND
$HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND
$HTTP_POST_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
$error = "<b>You may only upload .gif or .jpeg files.</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
copy($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".$HTTP_POST_FILES
['upload_file']['name']);
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
print "Thank you for your upload.";
exit;
}
else
{
echo ("$error");
}
?>
<html>
<head></head>
<body>
<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
Choose a file to upload:<br>
<input type="file" name="upload_file" size="80">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Chapter 11 Code • (26) Comments • (2) Trackbacks • Permalink
Example 11-29 Executing df and displaying the results
<?php exec("df",$output_lines,$return_value);
echo ("Command returned a value of $return_value.");
echo "</pre>";
foreach ($output_lines as $output) {
echo "$o";
}
echo "</pre>";
?>
For our system, we get the screen in Figure 11-23.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Friday, August 18, 2006
Figure 10-03 Text boxes
<form>
<input type="text" name="search" size="10" maxlength="30" />
</form>
This creates the Figure 10-03 in the Learning PHP & MySQL book.
Chapter 10 Code • (0) Comments • (1204) Trackbacks • Permalink
Figure 10-04 A simple form with a text area element
<form>
<label>Suggestion: <textarea name="suggestions" cols="40" rows="5"></textarea>
</label>
<input type="submit" value="Go! />
</form>
This creates the example in Figure 10-04 in the book.
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Figure 10-05 Checkboxes
<form>
<fieldset>
<label>Italian <input type="checkbox" name="food[]" value="Italian" /></label>
<label>Mexican <input type="checkbox" name="food[]" value="Mexican" /></label>
<label>Chinese <input type="checkbox" name="food[]" value="Chinese"
checked="checked" /></label>
</fieldset>
<input type="submit" value="Go! />
</form>
This code displays Figure 10-05 in the book.
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Figure 10-06 Radio buttons
<form>
<fieldset>
<label>Italian <input type="radio" name="food" value="Italian" /></label>
<label>Mexican <input type="radio" name="food" value="Mexican" /></label>
<label>Chinese <input type="radio" name="food" value="Chinese"
checked="checked" /></label>
</fieldset>
<input type="submit" value="Go! />
</form>
This code generates the example in Figure 10-06 in the book.
Chapter 10 Code • (12) Comments • (0) Trackbacks • Permalink
Figure 10-09 Selecting Italian and Chinese
<html>
<head>
<title>Using Default Checkbox Values</title>
</head>
<body>
<?php
$food=$_GET[food];
$self=$_SERVER['PHP_SELF'];
if (!empty($food))
{
echo "The foods selected are:<br />";
foreach($_GET[food] as $foodstuf)
{
}
}
else
{
echo ("<form action=\"$self\" ");
echo ('method="get">
<fieldset>
<label>Italian <input type="checkbox" name="food[]" value="Italian" /></label>
<label>Mexican <input type="checkbox" name="food[]" value="Mexican" /></label>
<label>Chinese <input type="checkbox" name="food[]" value="Chinese"
checked="checked" /></label>
</fieldset>
<input type="submit" value="Go!" >
');
}
?>
</body>
</html>
This code displays the graphic Figure 10-09 in the book.
Chapter 10 Code • (19) Comments • (4) Trackbacks • Permalink
Thursday, August 03, 2006
Question 05-01 What’s wrong with this function call?
This code is for Question 5-1.
<?php
// define a function
function Response {
echo "Have a good day!<br/><br/>";
}
// driving to work
echo "Are you going to merge? <br/>";
Response;
// at the office
echo "I need a status report on all your projects in the next 10 minutes for my management meeting.<br/>";
Response;
// at the pub after work
echo "Did Bill get everything he needed today? He was sure crabby!<br/>";
Response;
?>
Statistics
This page has been viewed 375221 times
Page rendered in 0.2931 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 8
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:49 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
