图片上传php的问题:未定义索引


Issue with image upload php: undefined index

我在让图像上传功能处理下面的代码时遇到了问题,出现了以下错误:

'脚本'/database.php'第135行出现错误:未定义的索引:图像'

是这部分:'if(is_uploaded_file($_FILES['image']['tmp_name'])){'

我以前做过一些php/mysql表单,但这是我第一次尝试使用图像上传。如果可能的话,我正在寻找一个只允许pdf和jpegs并将上传限制在1.5 meg的表单。

表单的其余部分工作正常,除此之外,我已将服务器上传文件夹中的CHMOD更改为777。有什么想法或建议吗?

<?php # index.php
require_once ('./includes/config.inc.php');
$page_title = 'N-Club Battery: Add a Sale';
include ('./includes/header.html');
if (!isset($_SESSION['sp_user_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '''') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/login.php'; 
ob_end_clean(); // Delete the buffer.
header("Location: $url"); 
exit(); // Quit the script.
}
$users = $_SESSION['sp_user_id'];
?>
<h1>Heading</h1>
<?php
if (isset($_POST['submitted'])) {// Handle the form.
require_once ('/database.php');
$errors = array(); // Initialize error array.
// Check for a quantity.
if (empty($_POST['npc_quantity'])) {
$errors[] = '<p>• You forgot to enter the Quantity.</p>';
} else {
$quantity = escape_data($_POST['npc_quantity']);
}
// Check for a customer name.
if (empty($_POST['npc_customer_name'])) {
$errors[] = '<p>• You forgot to enter the Customer Name</p>';
} else {
$customers = escape_data($_POST['npc_customer_name']);
}
// Check for an registration number
if (empty($_POST['npc_registration_no'])) {
$errors[] = '• You forgot to enter the Customer Rego Number.';
} else {
$rego = escape_data($_POST['npc_registration_no']);
}
// Check for an SUO number
if (empty($_POST['npc_suo_no'])) {
$errors[] = '• You forgot to enter the SUO Number';
} else {
$suo_no = escape_data($_POST['npc_suo_no']);
}
// Check for an A-Club number
if (empty($_POST['npc_amb_club_no'])) {
$errors[] = '• You forgot to enter the A-Club Number';
} else {
$amb_no = escape_data($_POST['npc_amb_club_no']);
}
// Check for a Repair Order
if (empty($_POST['npc_repair_order'])) {
$errors[] = '• You forgot to enter the Repair Order';
} else {
$rep_no = escape_data($_POST['npc_repair_order']);
}
// Check for a Invoice Number.
if (empty($_POST['npc_invoice_no'])) {
$errors[] = '<p>• You forgot to enter the Invoice Number.</p>';
} else {
$inv = escape_data($_POST['npc_invoice_no']);
}
// Check for a userdate.
if (empty($_POST['npc_entry_userdate'])) {
$errors[] = '<p>• You forgot to enter the entry userdate.</p>';
} else {
$inv_date = escape_data($_POST['npc_entry_userdate']);
}
// Check for an image.
if (is_uploaded_file ($_FILES ['image']['tmp_name'])) {
if (move_uploaded_file($_FILES ['image']['tmp_name'], 
"/uploadsfolder/{$_FILES['image']['name']}")) { // Move the file over.
echo '<p>The file has been uploaded!</p>';
} else { // Couldn't move the file over.
echo '<p><font color="red"> The thumbnail image could not be uploaded.</font></p>';
$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
$i = FALSE;
}
if (empty($errors)) { 
$uid = @mysql_insert_id(); 
// Add the user.
$query = "INSERT INTO npc_sales_list 
(npc_item_id , sp_user_id, npc_battery_n_code, npc_battery_other_code, npc_quantity , 
npc_customer_name, npc_registration_no , npc_suo_no, npc_amb_club_no , npc_repair_order, 
npc_invoice_no, npc_entry_userdate, npc_image_upload, npc_usertype, 
npc_points, npc_bonus_points, npc_entry_date) VALUES
('$uid', '$users', '$_POST[npc_battery_n_code]' , 
'$_POST[npc_battery_other_code]' , '$quantity' , '$customers', 
'$rego' , '$suo_no' , '$amb_no', '$rep_no', '$inv', '$inv_date', 
'$i' , Battery' , '5' , '0' , NOW())";
$result = @mysql_query ($query); // Run the query.
if (mysql_affected_rows() == 1) {
//Finish the page.
echo '<p>The sale with invoice number: <strong>' . $inv . '</strong><br />
is now registered into the system.</p>';
include ('./includes/footer.html'); // Include the HTML footer.
exit();
} else { // If it did not run OK.
echo '<p><font color="red" size="+1">You could not be
registered due to a system error. We apologize for any
inconvenience.</font></p>';
}
} else { // If one of the data tests failed.
echo '<p><font color="red" size="+1">Please check all mandatory fields are complete and try again.
</font></p>';
}
mysql_close(); // Close the database connection.
} // End of the main Submit conditional.
?>                          
<form action="logsales.php" method="post">
<table width="520" border="0" cellspacing="3" cellpadding="2">
<tr>
<td><div align="right">N-Club Battery No<span class="style2">*</span></div></td>
<td>            
<select  name="npc_battery_n_code" id="npc_battery_n_code">
<option value="None" <?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == 'None') 
{echo 'selected="selected"';} ?>>None</option>
<option value="55D23L" <?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23L') 
{echo 'selected="selected"';} ?>>55D23L</option>
<option value="55D23R" <?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23R') 
{echo 'selected="selected"';} ?>>55D23R</option>                                
<option value="75D23R" <?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '75D23R') 
{echo 'selected="selected"';} ?>>75D23R</option>
</select>
</td>
</tr>  
<tr>
<td width="184"><div align="right">Other Battery No<span class="style2">*</span></div></td>
<td width="323">
<select  name="npc_battery_other_code" id="npc_battery_other_code">
<option value="None"  <?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == 'None') 
{echo 'selected="selected"';} ?>>None</option>
<option value="3"  <?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '3') 
{echo 'selected="selected"';} ?>>3</option>
<option value="26" <?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '26') 
{echo 'selected="selected"';} ?>>26</option>                                                               
<option value="29" <?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '29') 
{echo 'selected="selected"';} ?>>29</option>
<option value="30" <?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '30') 
{echo 'selected="selected"';} ?>>30</option>
</select></td>
</tr>
<tr>
<td><div align="right">Quantity <span class="style2">*</span></div></td>
<td><input type="text" name="npc_quantity" size="10" maxlength="10" 
value="<?php if (isset($_POST['npc_quantity'])) echo $_POST['npc_quantity']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Customer Name <span class="style2">*</span></div></td>
<td><input type="text" name="npc_customer_name" size="30" maxlength="30" 
value="<?php if (isset($_POST['npc_customer_name'])) echo $_POST['npc_customer_name']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Customer Rego number <span class="style2">*</span></div></td>
<td><input type="text" name="npc_registration_no" size="10" maxlength="7" 
value="<?php if (isset($_POST['npc_registration_no'])) echo $_POST['npc_registration_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">SUO Number <span class="style2">*</span></div></td>
<td><input type="text" name="npc_suo_no" size="20" maxlength="30" 
value="<?php if (isset($_POST['npc_suo_no'])) echo $_POST['npc_suo_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">A-Club ID <span class="style2">*</span></div></td>
<td><input type="text" name="npc_amb_club_no" size="20" maxlength="30" 
value="<?php if (isset($_POST['npc_amb_club_no'])) echo $_POST['npc_amb_club_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Repair Order<span class="style2">*</span></div></td>
<td><input type="text" name="npc_repair_order" size="20" maxlength="30" 
value="<?php if (isset($_POST['npc_repair_order'])) echo $_POST['npc_repair_order']; ?>" /></td>
</tr>

<tr>
<td><div align="right">Invoice Number <span class="style2">*</span></div></td>
<td><input type="text" name="npc_invoice_no" size="20" maxlength="30" 
value="<?php if (isset($_POST['npc_invoice_no'])) echo $_POST['npc_invoice_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Date of Invoice <span class="style2">*</span></div></td>
<td><input type="text" name="npc_entry_userdate" size="20" maxlength="10" 
value="<?php if (isset($_POST['npc_entry_userdate'])) echo $_POST['npc_entry_userdate']; ?>" /></td>
</tr>
<tr>
<td></td>
<td>
<span class="sml_italics">MM-DD-YYYY </span></td>
</tr>
<tr>
<td><div align="right">Invoice copy<span class="style2">*</span></div></td>
<td> 
<input type="file" name="image"  size="30" maxlength="60" /> <br />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></p><input type="hidden" name="submitted" value="TRUE" />
&nbsp;</td>
</tr>
</table>
</form>
<p>footer</p>
<?php
include ('./includes/footer.html');
?> 

您的<form>没有enctype属性。

<form action="logsales.php" method="post">

它需要:

<form action="logsales.php" method="post" enctype="multipart/form-data">

如果没有enctype,你的浏览器就不知道如何将文件发送到服务器,所以如果没有它,它就无法工作。有关更多信息,请访问W3。

if (is_uploaded_file ($_FILES ['image']['tmp_name']))之上添加另一个if条件,如下所示:

if (isset($_FILES['image']) && $_FILES['image']['tmp_name'] != ''){
  if(is_uploaded_file($_FILES['image']['tmp_name'])){  
  ................your logic here.............
  }
}