dreamingdigital
09-10-2002, 05:31 AM
I made this site to learn a few more PHP tricks and to help out my University proffesor friend Dr. Fowler.
This is a Web site that Dr. Fowler can use to upload his lectures... easily and no-computer-brains required.
I have a problem with my modify.php and modifyme.php files. I do not know how to fix the extra lines php is throwing into my .dat file when it writes the data back into the file.
What happens:
Php reads in the ppt.dat file as a 2D array in modify.php
User inputs which string to delete.
Php sends that number to the modifyme.php file which reads the ppt.dat file to an array, omits the string which it is told to delete, then continues writing the rest of the array.
The Problem:
The 1st modification works flawlessly. The 2nd modification PHP throws in phantom extra lines every other line in the ppt.dat file!! This messes up how the ppt.dat file gets read in the arrays.
I have included a zip file which you can download from my site. http://www.dreamingdigital.ca/www.zip the passwords to login are both "demo"
I'm sure there is somebody smart enough to fix this "little" glitch!
Thank you in advance.
Colin Puttick
Webmaster Dreaming Digital
www.dreamingdigital.ca
dreamingdigital
09-11-2002, 03:28 PM
I was thinking maybe the trouble is before the modify part and when I write the data to the file in the first place.
I write it like this:
fputs ($fo, "<stuff>\n");
fputs ($fo, "<stuff>\n");
so it goes on single lines in the dat file.
now if I read all of my information I need to write to the dat file in an array, then wrote each array string to the dat file It would put them on new lines anyways.
then maybe I'd get rid of the phantome extra new lines after I modify it?
What do you think??l
I think you should actually post your code here rather than ask people to download/extract a zip.
If you could just post the pertinent parts of the pages, I'm pretty sure that someone would help. I'd certainly try to, whereas (and I don't have a very good reason for this) I wouldn't download a zip file on the offchance of knowing an answer.
dreamingdigital
09-16-2002, 02:14 AM
Ok here is some code. There is lots of it though.
ppt.dat
-----------------------------------------
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Cardiac Surgery</b></font> <small>( September 9, 2002, 1:38 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/cardiac_surgery.2002.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Ear Surgery</b></font> <small>( September 9, 2002, 1:38 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/ear_surgery.2002.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Cancer Surgery</b></font> <small>( September 9, 2002, 1:38 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/oncologic_surgery.2002.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Condensed Reconstructive Notes</b></font> <small>( September 9, 2002, 1:39 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/reconstructive_condensed.2002.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Expanded Reconstructive Notes</b></font> <small>( September 9, 2002, 1:39 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/reconstructive_expanded.2002.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
<!---------- start ----------->
<dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>Oncology Cases</b></font> <small>( September 9, 2002, 1:44 pm )</small>
<dd><font face='Verdana, Arial, Tahoma'><a href='files/onco.cases.doc' target='_blank'>Download</a></font>
</dl>
<!---------- end ----------->
add.php
-------------------------------------------------
<HTML>
<HEAD>
<title>Upload Complete</title>
</head>
<body text="white" bgcolor="#000066" link="white" alink="red" vlink="white">
<center><a href="index.html"><img src="fowleronline.jpg" border=0 width="660" height="120" alt="Dr. Fowler Online"></a></center>
<p> </p>
<?php
/*
***************************************************************************
* Copyright (C) 2001 - Viktor "PRiSCO" Bezdek - prisco@prisco.cz *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, *
* USA. *
***************************************************************************
*/
// rcs_id('$Id: upload.php,v 1.5 2001/11/12 18:14:26 jerobins Exp $');
// general configuration
$upload_path = "files/"; // path to your upload directory
$extval_use = 1; // turns on/off extension validation
// forbidden extensions
$extval = array("php", "php3", "asp", "bat", "exe", "cgi", "bin", "htaccess",
"com", "jsp", "cfml", "shtml", "dtcl", "pl", "ini", "dll");
$filesize_limit_use = 1; // turns on/off size check
$filesize_limit = 8; // file size limit (in kB)
// message customization
$message["fileisbig"] = "Hey! Your file is bigger than upload limit ("
. $filesize_limit . "kB)";
$message["invext"] = "Files of this type are not allowed.";
$message["incomplete"] = "Incomplete.";
$message["complete"] = "Success!";
$message["fileexists"] = "Your file already exists.";
$today = date("F j, Y, g:i a");
$rc = 0;
if ( isset($HTTP_POST_VARS["upload"]) && $permission=="true" ) {
$orig_name = $HTTP_POST_FILES['userfile']['name'];
$filename = ereg_replace("[^a-z0-9._]", "",
ereg_replace (" ", "_",
ereg_replace("%20", "_",
strtolower($orig_name))));
// <filesize>
if($filesize_limit_use=1) {
$filesize = $HTTP_POST_FILES['userfile']['name'] / 1024; //filesize in kB
if($filesize_limit < $filesize) {
echo "<p><font color='red'><center>"
. $message["fileisbig"]."</font></center></p>";
$rc = 1;
}
}
// </filesize>
if ( $rc == 0 ) {
// <extension_validate>
if($extval_use=1) {
$extget = substr( strrchr($filename, "."), 1);
$found = in_array($extget, $extval);
if ( $found ) {
echo "<p><font color='fuchsia' size=4><center>"
. $message["invext"]."</font></center></p>";
$rc = 2;
}
}
// </extension_validate>
}
if ( $rc == 0 ) {
echo "<p><center><font face='Verdana,Arial,Tahoma'>Uploading .<blink>.</blink>.<blink>.</blink>. " . $upload_path . $filename . "</font></center></p>\n";
if( move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $upload_path.$filename) ) {
$description = str_replace("\'", "'", $description);
$description = str_replace('\"', '"', $description);
$fo = fopen("ppt.dat","a");
fputs($fo,"<!---------- start ----------->\n");
fputs($fo," <dl><dt><font face='Verdana, Arial, Tahoma' color='#FFCC66'><b>$description</b></font> <small>( $today )</small>\n");
fputs($fo," <dd><font face='Verdana, Arial, Tahoma'><a href='files/$filename' target='_blank'>Download</a></font>\n");
fputs($fo," </dl>\n");
fputs($fo,"<!---------- end ----------->\n");
fclose($fo);
echo "<center><p><font face='Verdana,Arial,Tahoma'><font color=green><b>" . $message["complete"]."</font>";
print "<br>Click <a href='javascript:history.go(-1)'>HERE</a> to upload another file</b></p>";
print "<form name=backtoadmin action='admin.php' method=post><input type=hidden name=permission value=true>";
print "<input type=submit name=submit value=' --- Back to Admin Page --- '></form>";
print "<form name=backtoadmin action='adminview.php' method=post><input type=hidden name=permission value=true>";
print "<input type=submit name=submit value=' --- View --- '></form>";
}
else {
echo "<center><font face='Verdana,Arial,Tahoma'><p><font color='red' size=4>" . $message["incomplete"]."</font></p><p>Your file must be under 1.2 Mb.<br>Click <a href='javascript:history.go(-1)'>HERE</a> to upload another file</p>";
print "<form name=backtoadmin action='admin.php' method=post><input type=hidden name=permission value=true>";
print "<input type=submit name=submit value=' --- Back to Admin Page --- '></form>";
}
}
}
else {
print "<p> </p><font color=red face='Verdana, Arial, Tahoma'><b>You are not logged in<br>Locate login page and login.</b></font>";
}
?>
<p><font color=white><font face=Verdana size=1><b>© Dreaming Digital 2002</b></font></center>
</body>
</html>
(read next reply for more) (reaching max message length)
dreamingdigital
09-16-2002, 02:24 AM
modify.php
--------------------------------------------
<html>
<head>
<title>Modify Presentation Listings</title>
</head>
<body text="white" bgcolor="#000066" link="white" alink="red" vlink="white">
<center><a href="index.html"><img src="fowleronline.jpg" border=0 width="660" height="120" alt="Dr. Fowler Online"></a></center>
<?php
$logged = "true";
$inside = 0;
$number = 0;
if($logged == $permission) {
$entries = "colin";
print "<center><form method='post' name=modifyme action='modifyme.php'>";
// print "<textarea name='modified' wrap=off rows=24 cols=94>";
$entries = file("ppt.dat","r");
for($counter = 0; $counter < sizeof($entries); $counter++)
{
for($inside = 0; $counter < sizeof($entries); $inside++) {
if($counter % 5 != 0) {
$myarray[$number][$inside] = $entries[$counter];
$counter++;
}
else break;
}
$number++;
}
// $number--;
for($i = 1; $i < $number; $i++) {
for($j = 0; $j < 5; $j++) {
echo $myarray[$i][$j];
}
print "<font size=5 color=red><b>$i</b></font><hr>";
}
// print "</textarea>";
print "<input type=hidden name=permission value=true>";
print "<font face='Verdana,Arial,Tahoma'>Enter the <font color=red>number</font> of the listing to remove: </font>";
print "<input type=text size=2 name='deleteme'>";
print "<p><input type=submit name=submit value=' --- Submit for Modification --- '>";
print "</form>";
print "<form name=backtoadmin action='admin.php' method=post><input type=hidden name=permission value=true>";
print "<input type=submit name=submit value=' Cancel '></form>";
}
else {
print"<p> </p><center><font color=red face='Verdana, Arial, Tahoma'><b>You are not logged in<br>Locate login page and login.</b></font>";
}
?>
<p><font color=white><font face=Verdana size=1><b>© Dreaming Digital 2002</b></font></center>
</BODY>
</HTML>
modifyme.php
------------------------------------------------------
<html>
<head>
<title>Modify Presentation links</title>
</head>
<body text="white" bgcolor="#000066" link="white" alink="red" vlink="white">
<center><a href="index.html"><img src="fowleronline.jpg" border=0 width="660" height="120" alt="Dr. Fowler Online"></a></center>
<?php
$logged = "true";
$inside = 0;
$number = 0;
if($logged == $permission) {
$entries = "colin";
$entries = file("ppt.dat","r");
for($counter = 0; $counter < sizeof($entries); $counter++)
{
for($inside = 0; $counter < sizeof($entries); $inside++) {
if($counter % 5 != 0) {
$myarray[$number][$inside] = $entries[$counter];
$counter++;
}
else break;
}
$number++;
}
$k = 0;
for($i = 1; $i < $number; $i++) {
for($j = 0; $j < 5; $j++) {
if ($deleteme != $i) {
$modarray[$k] = $myarray[$i][$j];
echo $modarray[$k];
$k++;
}
}
}
$fo = fopen("ppt.dat","w");
for($counter = 0; $counter < sizeof($modarray); $counter++) {
fputs($fo,"$modarray[$counter]");
}
fclose($fo);
print "<center>Success!<p>";
print "<form name=backtoadmin action='admin.php' method=post>";
print "<input type=hidden name=permission value=true>";
print "<input type=submit name=submit value=' --- Return to Admin Page --- '>";
print "</form>";
}
else {
print"<p> </p><center><font color=red face='Verdana, Arial, Tahoma'><b>You are not logged in<br>Locate login page and login.</b></font>";
}
?>
<p><font color=white><font face=Verdana size=1><b>© Dreaming Digital 2002</b></font></center>
</BODY>
</HTML>
dreamingdigital
09-16-2002, 02:25 AM
just for fun's sake. this forum has the same bug that my code does. note the extra blank lines in the code in the reply above.
dreamingdigital
09-20-2002, 04:27 PM
Hi
So if my problem is a common problem (ie this forum has it) why is there nobody that knows how to fix it?
CP
Spookster
09-20-2002, 07:43 PM
Just glancing through your code I see one thing that could result in extra lines:
for($counter = 0; $counter < sizeof($modarray); $counter++) {
fputs($fo,"$modarray[$counter]");
}
Array indexes begin at 0. When you grab the size of an array you will get how many elements are in there.
If you had 10 elements in your array and then looped from 0 to the size of your array it is going to loop 11 times.
0,1,2,3,4,5,6,7,8,9,10
That's eleven numbers. So typically you will want to say size-1 when
using for loops to loop through an array:
for($counter = 0; $counter < sizeof($modarray)-1; $counter++) {
fputs($fo,"$modarray[$counter]");
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.