473,416 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Help debugging Flat File Picture Gallery

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
Jul 17 '05 #1
2 3900
On Thu, 13 Jan 2005 20:58:36 +1000, matt wrote:
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.


Not likely. Much too broad a question.

--
Jeffrey D. Silverman | je**********@jhu.edu
Website | http://www.newtnotes.com

Drop "PANTS" to reply by email

Jul 17 '05 #2
In the following script, When i delete all lines from the data text
file, it displays as a broken image at the end, when there's nothing
left in the file (except possibly a line break) it displays that as a
broken image.... Is there any way of stopping this from happening?

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>
All help appreciated.
Thanks
Matt

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simone Winkler | last post by:
Hello, I don't know if my question is out of topic...so I apologize if I am. I use the coppermine picture gallery for my web (actually I want to use it but the server doesn't seem to have GD...
1
by: matt | last post by:
I don't have a database available, so i'm writing an Image Gallery with a flat file database. I have an Add Page: Uploads Image file, and saves $filename and $caption to text file: captions.txt...
4
by: sepgy | last post by:
Can anyone help me to use a python to create an HTML photo gallery generator. When it's finished, it will be able find all the picture files (i.e. .jpg, .gif. .png files) in any given folder on the...
2
by: the other john | last post by:
This should be fairly basic but I can't think of how to do this and I'm running out of time! I am developing a picture gallery and I can't figure out how to select "one" picture from each...
2
by: Lyn | last post by:
What is the best way to import a picture (bmp format) via VBA from a Windows file and embed it into a Bound Object Frame (and thence to an OLE Object in a table)? I found an MS knowledge base...
13
by: matt | last post by:
I have this bit of code, that isn't valid, and I need some help getting it to work... Currently, I get an error because I am calling the next() function within a foreach loop. I am opening a flat...
3
by: Chuck Renner | last post by:
Please help! This MIGHT even be a bug in PHP! I'll provide version numbers and site specific information (browser, OS, and kernel versions) if others cannot reproduce this problem. I'm...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: kalyke | last post by:
I'm not sure if I have the syntax right or if I'm using the wrong code or what, but I just can't seem to get this to work. Please someone point me in the right direction. function ajaxRequest(){...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.