Sunday, July 13, 2008

Create Album with PHP and Database (3)

Files : Table for uploading, adminup.php

1. Upload images to the server



<form method="post" enctype="multipart/form-data" action="adminup.php">
<table width="480" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width=150 class=side>Name</td>
<td width="330" class=sidetitle>
<?
if($_GET['user']){
echo $_GET['user'];
echo '<input name="name" type="hidden" value="'.$_GET['user'].'">';
}
?>
</td>
</tr>
<tr>
<td width=150 class=side>Album title</td>
<td width="330" class=sidetitle>
<?
if($_GET['albumname']){
echo $_GET['albumname'];
echo '<input name="album" type="hidden" value="'.$_GET['albumname'].'">';
}
?>
</td>
</tr>
<tr>
<td width="480" colspan=2>
<?
if($_GET['id']){
echo '<input name="index" type="hidden" value="'.$_GET['id'].'">';
}
?>
</td>
</tr>
<tr>
<td width=150 class=side>Picture 1</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile1" type="file" id="userfile1">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 2</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile2" type="file" id="userfile2">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 3</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile3" type="file" id="userfile3">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 4</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile4" type="file" id="userfile4">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 5</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile5" type="file" id="userfile5">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 6</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile6" type="file" id="userfile1">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 7</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile7" type="file" id="userfile2">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 8</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile8" type="file" id="userfile3">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 9</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile9" type="file" id="userfile4">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 10</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile10" type="file" id="userfile5">
</td>
</tr>
<tr>
<td width="480" class=sidetitle colspan=2><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>


2.when the users click the submit button, it will call adminup.php which is shown below.
adminup.php
<?php

$album=$_POST['album'];
$name=$_POST['name'];
$album_num=$_POST['index'];

$db_conn=mysql_connect("your server", "your id", "your pwd");
mysql_select_db("your database");


//看看這本相簿有多少張相片
$query = "select * from upload where album_num=$album_num";
$result=mysql_query($query);
$how_many_pic=mysql_num_rows($result);

for($i=1;$i<=10;$i++){
$temp="userfile".$i;

if(isset($_POST['upload']) && $_FILES[$temp]['size'] > 0)
{
$fileName = $_FILES[$temp]['name'];
$tmpName = $_FILES[$temp]['tmp_name'];
$fileSize = $_FILES[$temp]['size'];
$fileType = $_FILES[$temp]['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$margin = getimagesize($tmpName);
$width=$margin[0];
$height=$margin[1];

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

$how_many_pic++;

//Insert the picture into the database. And the table
//to store the pictures is called 'upload'
$query = "INSERT INTO upload (name, size, type, content, album_name, album_num, pic_num, author, width, height ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$album', '$album_num', '$how_many_pic', '$name' , '$width', '$height')";

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

echo '<meta http-equiv=refresh content=0;url="adminpic.php?id='.$album_num.'&user='.$name.'&page=1">';
exit;
?>


How to show the pictures we upload will be introduced next time.

No comments: