4 Replies - 311 Views - Last Post: 01 October 2019 - 11:22 AM Rate Topic: -----

#1 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Multiple file causing an error in file upload system

Posted 30 September 2019 - 01:04 PM

I have a form that allows 1 to multiple file uploads. I just noticed that when making multiple file uploads with a submission there is an error.

Quote

Invalid argument supplied for foreach()


For this line:

foreach ($filename as $indFile) {


The files upload, but it is effecting other areas in the file due to the error.

I added in var_export($filename); to do some debugging and this printed out:

Quote

array ( 0 => 'aboveFold.PNG', 1 => 'afterglow.png', )0<br /> <b>Warning</b>: Invalid argument supplied for foreach()


From this var_export what I gather is the file upload is occuring through the first loop and then when it loops again, it creates a 0.

Does anyone have any suggestions that will fix this multiple file upload issue, while preserving the form to work with a single or no file.

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if($file['error'][$key] != 4) { //error code is not #4
                $fu = new fileUpload();
                $filename = $fu->upload();
                //var_export($filename);
                $out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). '  uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
                foreach ($filename as $indFile) {
                    //$template = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$indFile}'>{$indFile}</a>", $template);
                    //print_r($template);
                    $out .= "<li><a href='https://mbkit.com/php/uploads/{$indFile}'>{$indFile}</a></li>";
                }
                $out .= '</ul>';
                $template = str_replace("{filename}", $out, $template);

                //echo "name is NOT empty AND error is NOT 4";
            } else { //error code IS #4
                //echo "error code is 4";
            }
        } else {
            //echo "name is empty!";
            $template = str_replace("{filename}", '', $template);
        }
    }
}
clearstatcache();


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple file causing an error in file upload system

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2458
  • View blog
  • Posts: 7,488
  • Joined: 15-January 14

Re: Multiple file causing an error in file upload system

Posted 30 September 2019 - 02:00 PM

It sounds like your fileupload class will do multiple files, you might not need to run that in a loop.
Was This Post Helpful? 1
  • +
  • -

#3 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: Multiple file causing an error in file upload system

Posted 01 October 2019 - 06:43 AM

Are you saying to put:

$filename = $fu->upload();


in the foreach loop?

This is the fileUpload class:

<?php

class fileUpload
{

    public function __construct()
    {}
    public function upload() {

		$file_count = count($_FILES['uploadedFile']['name']);
		$results = [];
		for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

		    $target_dir = "uploads/";
			$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
			$uploadOk = 1;
			$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));		

			// Check if $uploadOk is set to 0 by an error
			if ($uploadOk == 0) {
				return 0;
	// if everything is ok, try to upload file
			} else {
				if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
					$results[] = basename($_FILES["uploadedFile"]["name"][$i]);
				} else {
					return 0;
				}
			}
		}
		return $results;

// Check if image file is a actual image or fake image
//if(isset($_POST["submit"])) {
//    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
//    if($check !== false) {
//        echo "File is an image - " . $check["mime"] . ".";
//        $uploadOk = 1;
//    } else {
//        echo "File is not an image.";
//        $uploadOk = 0;
//    }
//}
// Check if file already exists
//if (file_exists($target_file)) {
//    echo "Sorry, file already exists.";
//    $uploadOk = 0;
//}
//// Check file size
//if ($_FILES["fileToUpload"]["size"] > 500000) {
//    echo "Sorry, your file is too large.";
//    $uploadOk = 0;
//}
//// Allow certain file formats
//if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
//    && $imageFileType != "gif" ) {
//    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
//    $uploadOk = 0;
//}
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2458
  • View blog
  • Posts: 7,488
  • Joined: 15-January 14

Re: Multiple file causing an error in file upload system

Posted 01 October 2019 - 10:06 AM

I'm saying that you need to look at that class and figure out what it's doing. You already ARE running it inside a loop, and your output shows that during the first time in the loop, it returns an array of filenames that it processed, and during the second time in the loop it returned the number 0. So, if it's going to process multiple files, then do you really need to put it in a loop?
Was This Post Helpful? 1
  • +
  • -

#5 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: Multiple file causing an error in file upload system

Posted 01 October 2019 - 11:22 AM

Ahhh, makes sense. I took it out of the loop and it now works. Thanks again for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1