未定义的索引:file_upload

时间:2013-11-07 06:53:10

标签: php

以下是我的代码:

    <?php 
    require_once("includes/initialize.php");
    if (!$session->is_logged_in()) { redirect_to("../index.php"); }
    include_template("admin_header.php");
    $product = new Products;
?>
<?php
    $upload_errors = array(
        // http://www.php.net/manual/en/features.file-upload.errors.php
        UPLOAD_ERR_OK               => "No errors.",
        UPLOAD_ERR_INI_SIZE     => "Larger than upload_max_filesize.",
      UPLOAD_ERR_FORM_SIZE  => "Larger than form MAX_FILE_SIZE.",
      UPLOAD_ERR_PARTIAL        => "Partial upload.",
      UPLOAD_ERR_NO_FILE        => "No file.",
      UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
      UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
      UPLOAD_ERR_EXTENSION  => "File upload stopped by extension."
    );

    if(isset($_POST['submit'])) {
        if($_POST['productname'] == "" || $_POST['price'] == "" || $_POST['origin'] == "" || $_POST['description'] == "") {
            $message = "All fields are compulsary";
        } else {
            $errors = array();
            $now = time();
            $product->product_name  = $_POST['productname'];
            $product->price         = $_POST['price'];
            $product->origin        = $_POST['origin'];
            $product->description   = $_POST['description'];
            $product->img1          = $now."_".basename($_FILES['img1']['name']);
            $product->img2          = ($now+1)."_".basename($_FILES['img2']['name']);
            $product->visibility    = $_POST['visibility'];

            // process the form data
            $tmp_file = $_FILES['img1']['tmp_name'];
            $target_file = $product->img1;
            $tmp_file1 = $_FILES['img2']['tmp_name'];
            $target_file1 = $product->img2;
            $upload_dir = "images";

            // move_uploaded_file will return false if $tmp_file is not a valid upload file 
            // or if it cannot be moved for any other reason
            if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file) && move_uploaded_file($tmp_file1, $upload_dir."/".$target_file1)) {
                $product->create();
                $message = "Product uploaded successfully. <br />";
            } else {
                                echo "<pre>".print_r($_FILES,true)."</pre>";
                $error = $_FILES['file_upload']['error'];
                $message = $upload_errors[$error];
            }
        }
    }
?>
  <tr>
    <td bgcolor="989797"><table width="1000" border="0">
      <tr bgcolor="#999999">
        <th width="750" valign="top" bgcolor="#999999" scope="col"><table width="100%" cellspacing="0" id="bodytable">
          <tr>
          <td></td>
          </tr>
          <tr class="bodytitle">
            <td colspan="2" valign="top"><strong>Add Product!!</strong></td>
          </tr>
          <tr>
            <td width="100%" valign="top"><div id="text2">
              <div>
                <table width="100%" cellspacing="0">
                  <tr>
                    <td colspan='2'>&nbsp;
                        <?php
                            if(isset($message)) {
                                echo "<font size='2'>".$message."</font>";
                            }
                        ?>
                    </td>
                  </tr>
                  <tr>
                    <td align='left' colspan='2'><font size='2'>File Size must be less than 1mb</font></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                   <form name="form1" enctype="multipart/form-data" action="addproduct.php" method="POST">
                   <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
                   <td align='left'><font size='2'>Product Name:</font></td>
                   <td align='left'><input name='productname' type='text' /></td>
                     </tr>
                     <tr>
                       <td align='left'><font size='2'>Price:</font></td>
                       <td align='left'><input name='price' type='text' /></td>
                     </tr>
                     <tr>
                       <td align='left'><font size='2'>Origin:</font></td>
                       <td align='left'><input name='origin' type='text' /></td>
                     </tr>
                     <tr>
                       <td align='left'><font size='2'>Description:</font></td>
                       <td align='left'><textarea name='description' rows='5' cols='35' /></textarea></td>
                     </tr>
                     <tr>
                       <td align='left'><font size='2'>Visibility</font></td><td align='left'>
                         <select name='visibility' rows='50' />
                         <option value='1'>Visible</option>
                         <option value='0'>Invisible</option>
                         </select>
                       </td>
                     </tr>
                     <tr>
                      <td align='left'><font size='2'>Image 1:</font></td>
                      <td align='left'><input name='img1' type='file' /></td>
                     </tr>
                     <tr>
                       <td align='left'><font size='2'>Image 2:</font></td>
                       <td align='left'><input name='img2' type='file' /></td>
                     </tr>
                  <tr>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                    <td align='left'><input name='submit' type='submit' value='Add!!' /></td>
                  </tr>
                  </form>
                </table>
                </div>
            </div></td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
        </table></th>
<?php
    include_template("admin_footer.php");
?>

我收到错误:

Notice: Undefined index: file_upload in ..\admin\addproduct.php on line 48
Notice: Undefined index: in ..\admin\addproduct.php on line 49

这是:

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file) && move_uploaded_file($tmp_file1, $upload_dir."/".$target_file1)) {
            $product->create();
            $message = "Product uploaded successfully. <br />";
        } else {
                            echo "<pre>".print_r($_FILES,true)."</pre>";
            $error = $_FILES['file_upload']['error'];
            $message = $upload_errors[$error];
        }

当我使用echo“

".print_r($_FILES,true)."
”时; 我明白了:

阵 (     [img1] =&gt;排列         (             [name] =&gt; IMG_6527.JPG             [type] =&gt;             [tmp_name] =&gt;             [错误] =&gt; 2             [size] =&gt; 0         )

    [img2] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

请帮帮我..

谢谢..

2 个答案:

答案 0 :(得分:1)

您的文件存储在img2数组中的$_FILES下,而不是file_upload下。

所以你的行必须是

$error = $_FILES['img2']['error'];

那是因为您在此处为$_FILES数组提供了索引名称name

<td align='left'><input name='img2' type='file' /></td>

您必须检查文件是否上传为答案中提到的moeed-farooqui

如果是可选的

if(isset($_FILES['img2'])){
    // only do something if a file is uploaded
} 

或者如果需要

if(isset($_FILES['img2'])){
    // handle the uploaded file
} else {
   // genereate an error message cause no file was uploaded
}

答案 1 :(得分:0)

使用isset来设置值是否设置

if(isset($_FILES['xyz'])){
// your code here
}

除此之外,我找不到名称为file_upload的任何字段。

应该是:

$error = $_FILES['img2']['error'];