Jump to content

A little PHP help with this script


Padgoi

Recommended Posts

I keep getting this error message:

Notice: Undefined index: image in /upload.php on line 128

Notice: Undefined index: image in /upload.php on line 129
Error: Invalid image. Only images of the types .jpeg, .jpg, .png, .gif are allowed.

Here is the script:
[code]
<?php
ob_start();
error_reporting(E_ALL|E_STRICT);

// Connect to your DB.
mysql_connect("database","user","password") or die("Could not connect to MySQL server!");
mysql_select_db("table") or die("Could not select database!");

define('TABLE', 'HIGHSCORE_TBL'); //Table name
$table = array(
0 => 'name', //Name of person
1 => 'score', //Score
2 => 'game', //Game scored in
3 => 'image', //Screenshot
4 => 'accepted' //Whether or not the score has been checked
);

$games = array(
'Soccer',
'FIFA 07',
'Some Flash Game'
);

//Admin access.
$admin = 'name';
$apass = 'pass';

//Max size allowed to upload in bytes
define('MAX_SIZE', 2000000);
//Directory to upload to
define('UPLOAD_DIR', 'screens');

//Allowable file Mime
$MIME_TYPES = array('image/jpeg', 'image/jpg', 'image/gif', 'image/png');

//Allowable file ext. names
$EXTENSIONS = array('.jpeg', '.jpg', '.png', '.gif');

/**** END CONFIGURATION ****/

function title($title)
{
//Modify this to your needs.
echo '
<html>
<head>
<title>' . $title . '</title>
</head>
<body>
<h1>' . $title . '</h1>
';
}

function upload()
{
global $table;
$new_file = intval(@mysql_result(mysql_query('SELECT MAX(`' . $table[3] . '`) FROM `' . TABLE . '`'), 0)) + 1;
$path = UPLOAD_DIR . '/' . $new_file . end(explode('.', $_FILES[$table[3]]['name']));

if(move_uploaded_file($_FILES[$table[3]]['tmp_name'], $path))
{
if(!chmod($path, '0777'))
{
'<h3>Warning - image could not be CHMODed correctly. Upload progressing.</h3>';
}
return true;
}
else
{
echo '<h3>Error in upload. Error unknown. Please try again.</h3>';
return false;
}
}

function do_link($link)
{
return '<a href="?game=' . $link . '">' . $link . '</a>';
}

/*** END FUNCTIONS ***/

//Check for login
if(isset($_COOKIE['admin']))
{
if($_COOKIE['admin'] == md5($admin . $apass))
{
define('ADMIN', true);
}
else
{
setcookie('admin', 'lol', 1);
define('ADMIN', false);
}
}
elseif(isset($_POST['login']))
{
if($_POST['u'] == $admin && $_POST['p'] == $apass)
{
setcookie('admin', md5($admin . $apass), time() + 24 * 3600);
define('ADMIN', true);
}
else
{
setcookie('admin', 'lol', 1);
define('ADMIN', false);
}

header('location: ' . $_SERVER['PHP_SELF']);
}
else
{
define('ADMIN', false);
}


if(isset($_GET['add']))
{ //Add a new high score
title('Add a New High Score');

if(isset($_POST['add']))
{
$image = $_FILES[$table[3]];
$ext = explode('.', $_FILES[$table[3]]['name']);

//Check for errors
if($image['size'] > MAX_SIZE)
{
echo '<h3>Error: Image too big. It can be at most ' . MAX_SIZE . ' bytes.</h3>';
}
elseif(!in_array($ext, $EXTENSIONS))
{
echo '<h3>Error: Invalid image. Only images of the types <strong>' . implode(', ', $EXTENSIONS) . '</strong> are allowed.';
}
else
{
if(upload())
{
$q = 'INSERT INTO `' . TABLE . '`(' . implode(', ', $table) . ') VALUES(';

foreach($table as $key)
{
if($key == $table[4])
{
break;
}

$q .= ($key == $table[1]) ? intval($_POST[$key]) : mysql_real_escape_string($_POST[$key]);
$q .= ', ';
}

$q .= '0)';

mysql_query($q) or print('<h3>Error with MySQL query. Please try again</h3>');
}
}
}

echo '

<form action="?add=true" method="post">
UserName: <input type="text" name="' . $table[0] . '"><br />
High Score: <input type="text" name="' . $table[1] . '"><br />
Game: <select name="' . $table[2] . '">
';

foreach($games as $game)
{
echo '<option value="' . $game . '">' . $game . "</option><br />\n";
}

echo '</select><br />
Screenshot/Image <input type="file" id="' . $table[3] . '" name="' . $table[3] . '" /><br />
<input type="submit" name="add" value="Submit Score!" />
</form>';


}
else
{ //List scores
title('High Scores' . (isset($_GET['list']) ? ' for ' . $_GET['list'] : ''));

echo '<div class="add"><a href="?add">Add your own high score!</a></div>';

echo '<div class="filter">Filter: ' . implode(' | ', array_map('do_link', $games));
if(ADMIN) echo ' | <a href="?filter=-1">Rejected</a> | <a href="?filter=0">In Queue</a> | <a href="?filter=1">Accepted</a>';
echo '</div>';

if(ADMIN && (isset($_GET['reject']) || isset($_GET['accept'])))
{

}

echo '<table><tr><th>Name</th><th>Score</th><th>Game</th>' . (ADMIN ? '<th>Screenshot</th><th>Status</th>' : '') . "</tr>\n";
for($q = mysql_query('SELECT ' . implode(',', $table)
. ' FROM ' . TABLE . ' WHERE '
. (isset($_GET['game']) ? '`game` = \''
. mysql_real_escape_string($_GET['game']) . '\'' : '1')
. ' AND `' . $table[4] . '` = '
. (ADMIN && isset($_GET['filter']) ? intval($_GET['filter']) : '1') . ' ORDER BY ' . $table[1] . ' DESC');
$row = mysql_fetch_row($q);)
{
echo '<tr><td>' . $row[0] . '</td><td>' . $row[1] . '</td><td>' . $row[2] . '</td>';

if(ADMIN)
{
echo '<td><a href="' . UPLOAD_DIR . '/' . $row[3] . '">Image</a></td><td><a href="?accept=' . $row[3] . '">Accept</a> | <a href="?reject=' . $row[4] . '">Reject</a></td>';
}

echo "</tr>\n";
}
echo '</table>';
}
?>

<?php
if(!ADMIN)
{
?>
<form action="?" method="post">
Admin: <input type="text" name="u" /> Key: <input type="password" name="p" /> <input type="submit" name="login" />
</form>
<?php
}
else
{
echo 'You are admin. Clear your cookie to logout.';
}
?>
</body>
</html>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26028-a-little-php-help-with-this-script/
Share on other sites

It helps if you point out which are lines 128 and 129... you might also try indenting your code to make it more readable. Other than that, your code looks pretty good, this error normally occurs when your trying to referenece an array index that doesn't yet exist.
you need to add:

<form action="?add=true" method="post" [b]enctype="multipart/form-data"[/b]>

to your form tag. after that, you're probably going to have some issues with using the $_FILE array properly, starting with your incorrect file extension error handling.

going to have to at the very least change this line:

elseif(!in_array($ext, $EXTENSIONS))

to this:

elseif(!in_array(".$ext[1]", $EXTENSIONS))

but you will probably want to alter other code instead. for instance, remove the dots "." from your extensions in your $EXTENSIONS array and then remove then simply do this:

elseif(!in_array($ext[1], $EXTENSIONS))

Crayon, thanks for the help, but another problem now:

Notice: Undefined index: image in /home/content/P/a/d/Padgoi/html/upload.php on line 159
Error with MySQL query. Please try again

line 159 is:

[code]
$q .= ($key == $table[1]) ? intval($_POST[$key]) : mysql_real_escape_string($_POST[$key]);
[/code]
I cannot efficiently debug your code when I have no idea what info it's trying to work with.  And you seem to have an aweful lot of bugs.  I'm not going to sit here and debug literally every single line of your code, because that would be the same as me (re)writing it for you. Only it's even worse, because I'm working blind here, cuz I don't have the actual data from queries and stuff sitting in front of me like you do.

here is a debugging tip:

before doing something like making a function call, running a query, etc.. echo all of the variables you are using so that you can see what they are holding, and therefore what your function calls and queries are trying to work with. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.