I have compiled some code, some written by me, some compiled from
various sources online, and basically i've got a very simple flat file
photo gallery. An upload form, to upload the photos and give them a
caption, storing the caption and filename in a text file. It's a bit
buggy when removing the photos and captions from the file, and also in
displaying them on the delete page. you can see it in action at
www.4am.com.au/gallery/upload.php - read.php - delete.php.
You can download the source at www.patoncreative.com.au/gallery.zip, and
I have posted it here below... i would appreciate some help in debugging
it.. i'm not the best with the 'foolproofing' of it before putting it
into use.
UPLOAD.PHP######################################## #
<form action="upload.php" method="post" enctype="multipart/form-data">
Select a Photo to Upload:<br>
<input type="file" name="filetoupload" size='50' class='form'><br><br>
Enter Photo Caption:<br>
<input type="text" name="caption" size='50' class='form'><br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<input type="Submit" value="Upload File" class='form'>
</form>
<br><br>
<?php
/* Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.
So if I filetoupload the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be
'application/msword'.
---------------------------------------------------------------------*/
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "../gallery/images/"; //change to whatever you want.
$captionFile = "captions";
// files less than 1MB
$size_bytes = 1048576; //bytes will be uploaded
//check if the directory exist or not.
if (!is_dir("$upload_dir")) {
die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory <b>($upload_dir)</b> is NOT writable,
Please Chmod (777)");
}
//Check first if a file has been selected
//is_filetoupload_file('filename') returns true if
//a file was filetoupload via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{
//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that $size is less than 1MB (1000000 bytes)
if ($size > $size_bytes)
{
echo "<b>Error!</b><br>";
echo "File Too Large. Please try again.";
exit();
}
// $filename will hold the value of the file name submitted from the form.
// $caption holds the value of the caption for the photo that the user
has written on the form.
$filename = $_FILES['filetoupload']['name'];
$caption = $HTTP_POST_VARS['caption'];
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
echo "<b>Error!</b><br>";
echo "File <b>$filename </b>already exists";
exit();
}
//Move the File to the Directory of your choice
//move_filetoupload_file('filename','destination') Moves an filetoupload
file to a new location.
if
(move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename))
{
//tell the user that the file has been uploaded
echo "<b>Upload Complete!</b><br>";
echo "File: <a href='$upload_dir$filename'>$filename</a><br>";
echo "Caption: $caption <br>";
// Open captions file in Append mode
$imageFile = $filename;
$captionText = $caption;
// Set the string to be written to the file
$values = "$imageFile|$caption\r\n";
// Open the file for truncated writing
$fp = @fopen("$captionFile", "a") or die("Couldn't open data file for
writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");
@fclose($fp);
// echo "Wrote $numBytes bytes to data file successfully!";
exit();
}
else
{
//Print error
echo "<b>Error!</b><br>";
echo "There was a problem moving your file<br>";
echo "Please check the filename and try again.<br>";
exit();
}
}
?>
READ.PHP########################################## ######
<?
$data = file('captions');
foreach ($data as $line) {
list ($q, $a) = explode('|' , trim($line) );
echo "<img src='images/$q' height=100><br>";
echo "$a<br>";
}
?>
DELETE.PHP######################################## #####
<?
$file = "captions";
// Generate some test data
if (!file_exists($file)) {
$count = 0;
$fp = fopen($file, "w+");
while (++$count < 20) {
fputs($fp, time() . "$count\n");
}
fclose($fp);
}
// Delete function; works both ways
// to increase speed
function delete (&$f, $line) {
global $file;
$match_a = false;
$match_b = false;
for ($n = count($f), $a = 0, $b = $n - 1;
$a < $n && $b >= 0 && !$match_a && !$match_b;
$a++, $b--) {
$match_a = $f[$a] == $line;
$match_b = $f[$b] == $line;
if ($match_a) {
unset($f[$a]);
} else if ($match_b) {
unset($f[$b]);
}
}
// Update the file contents
$fp = fopen($file, "w+");
$fString = implode("\r\n", $f);
fputs($fp, $fString, strlen($fString));
// extract the image filename from the data file line string
list ($q, $a) = explode('|' , trim($line) );
// append images directory to filename - not sure of a way to do this
with a global variable.
$filetoerase = "images/$q";
// erase file or print error
if (! unlink ($filetoerase))
{
echo "Error deleting file <b>$q</b><br>";
}
else
{
echo ("File <b> $q </b>removed successfully.<br>");
}
fclose($fp);
// Get the contents of the updated file
$f = explode("\r\n", file_get_contents($file));
}
$f = explode("\r\n", file_get_contents($file));
if (isset($_REQUEST['delete']))
{
delete($f, $_REQUEST['line']);
// echo "Data file line deleted successfully<br>";
}
else
{
echo "Please select a file to delete:";
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>">
<?
$data = file($file);
foreach ($f as $o)
{
print "<input type=radio name=line value=$o>";
$g = $o;
list ($q, $a) = explode('|' , trim($g) );
echo "<img src='images/$q' height=100><br>";
}
?>
<input type="submit" name="delete" value="delete" />
<input type="reset" name="reset" value="cancel selection" />
</form>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$
I also have a settings.inc file that I wanted to hold all variables in,
for use across each of the pages, but have been unsucsessful in doing
so.. do I need to declare the variables in it in a special way???
SETTINGS.INC###################################### #######
<?php
// Settings for Image Gallery:
// This is the database file that fill store your filenames and image
captions
$file = "captions";
// This is the directory you wish to store your photos in
$upload_dir = "../gallery/images/";
// Because the script is fussy, this is the directory above the base
which this script is in.
$images_dir = "images/";
// Photo Maximum Filesize
$size_bytes = 1048576;
?>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$
All help appreciated.
Thanks
Matt