-2

I m fairly new to PHP, Currently working on picture upload function, I have form where you can upload a picture, picture is not necessary to upload now the problem is code below won't let submit the form without a picture here is my code

$photoCount = count($_FILES['photo']['name'] ); 
if($photoCount > 0)  {
    for($i = 0;$i<$photoCount;$i++) {
        $name = $_FILES['photo']['name'][$i];
        $nameArray = explode('.', $name);
        $fileName = $nameArray[0];
        $fileExt = $nameArray[1];
        $mime = explode('/', $_FILES['photo']['type'][$i]);
        $mimeType = $mime[0];
        $mimeExt = $mime[1];
        $tmpLoc[] = $_FILES['photo']['tmp_name'][$i];
        $fileSize = $_FILES['photo']['size'][$i];
        $uploadName = $name;
        $uploadPath[] = BASEURL.'/admin/productimages/'.$uploadName;
        if ($i != 0) {
            $dbpath .= ',';
        }
        $dbpath .= '/project/admin/productimages/'.$uploadName;

        if($mimeType != 'image') {
            $errors[] .= 'The file must be an image.';
        }

        if(!in_array($fileExt, $allowed)) {
            $errors[] .= 'The file extension must be a png, jpg, jpeg, or gif.';
        }

        if($fileSize > 15000000) {
            $errors[] .= 'The file size must be under 15 megabytes.';
        }
        if ($fileExt != $mimeExt && ($mimeExt == 'jpeg' && $fileExt != 'jpg')) {
            $errors[] = 'File extension does not match the file';
        }
    }
}

Here is var dump of $_FILES

array(1) { ["photo"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } } 

erros i m getting

Notice: Undefined offset: 1 in C:\xampp\htdocs\project\admin\products.php on line 99

Notice: Undefined offset: 1 in C:\xampp\htdocs\project\admin\products.php on line 102

this is lines 99 and 102

$fileExt = $nameArray[1];

$mimeExt = $mime[1];
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Imran Rafique
  • 1,986
  • 2
  • 12
  • 14
  • What errors do you get when you try to submit without a picture? There have to be some and that would be an indication of what's wrong. – jonmrich Jan 20 '17 at 02:27
  • @jonmrich I have updated a question and included a erros i'm getting please have a look – Imran Rafique Jan 20 '17 at 02:28

2 Answers2

0

Before you process the name, check for errors.

if ($_FILES['photo']['error'][$i] > 0) {
    echo 'Didn\'t work';
} else {
    // Process the file
}

In this case, error is set to "4" (UPLOAD_ERR_NO_FILE). There are a range of errors, and if you have one then the file won't be there and you can't process it. So check first.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Robbie
  • 17,605
  • 4
  • 35
  • 72
-1

ooh!

The problem is your array index error;

In you code the $name is a empty string (for the result you var_dump), so explode('.', $name) will return array like this array [""]. The array length is 1, when use $nameArray[1] is out the length, so will get the error.

The second error is same as the first.

I think you should check the $name whether empty as first.

John
  • 39
  • 3