-1

Below is my Code:

    <?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");
?>

I am getting error:

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

Which is:

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

When I use echo "

".print_r($_FILES,true)."
"; I get:

Array ( [img1] => Array ( [name] => IMG_6527.JPG [type] => [tmp_name] => [error] => 2 [size] => 0 )

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

)

Please help me out..

Thank you..

2 Answers2

1

Your file is stored under img2 in the $_FILES array and not under file_upload.

So your line has to be

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

That's because you give the index name for the $_FILES array here with the attribute name

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

You have to check if a file is uploaded as moeed-farooqui mentioned in his answer.

If it's optional

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

Or if it's required

if(isset($_FILES['img2'])){
    // handle the uploaded file
} else {
   // genereate an error message cause no file was uploaded
}
Community
  • 1
  • 1
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
0

use isset to chech either the value is set or not

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

In addition to this, I cant find any field having name file_upload.

It should be:

$error = $_FILES['img2']['error'];
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23