-1

以下是我的代码:

    <?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];
        }

当我使用回声“

“.print_r($_FILES,true)。”
“;我得到:

数组([img1] => 数组([名称] => IMG_6527.JPG [类型] => [tmp_name] => [错误] => 2 [大小] => 0)

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

)

请帮帮我。。

谢谢..

4

2 回答 2

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
}
于 2013-11-07T06:59:45.497 回答
0

用于isset检查值是否已设置

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

除此之外,我找不到任何具有 name 的字段file_upload

它应该是:

$error = $_FILES['img2']['error'];
于 2013-11-07T07:01:36.913 回答