-2

I can't upload multiple files. I have a table of 4 items, each may have an uploaded file.

How should the code be?

$target_dir = "uploads/";
    foreach ($_FILES as $f => $a) {
        if ($a["name"]) {
            $target_file = $target_dir . "paper_" . $id . "_" . $f[12];
            $file_name = basename($a["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
             && $imageFileType != "pdf" && $imageFileType != "doc" && $imageFileType != "docx") {
                echo "Sorry, only JPG, JPEG, PNG, PDF, DOC and DOCX files are allowed. Your type is $imageFileType ";
                continue;
            }
            if ($a["size"] > 1024 * 1024 * 4) {
                echo "Sorry, your file is too large. Max is 4 MB";
                continue;
            }
            $col = "";
            switch ($f[12]) {
                case 1:
                    $col = "path_salary_def";
                    break;
                case 2:
                    $col = "path_social";
                    break;
                case 3:
                    $col = "path_identification";
                    break;
                case 4:
                    $col = "path_accom";
                    break;
                default:
                    die("invalid column");
            }
            $bol = substr($col, 5);
            if (move_uploaded_file($a["tmp_name"], $target_file)) {
                $query = "update papers SET $col = '$target_file', $bol = 1 WHERE id = " . $id;
                $result = mysqli_query($con,$query) or die ("Error in the data table 5");
                echo "The file $file_name has been uploaded.<br/>";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }
andrew
  • 9,313
  • 7
  • 30
  • 61
FindOut_Quran
  • 728
  • 3
  • 10
  • 27

1 Answers1

1

If your $_FILES array is empty perhaps you omitted the enctype attribute form the form.

It should look like:

<form action="yourAction.php" method="POST" enctype="multipart/form-data">
andrew
  • 9,313
  • 7
  • 30
  • 61
  • Must the method be `"POST/GET"` too?? – FindOut_Quran Dec 03 '14 at 12:45
  • I can't find any thing to confirm this I'd stick with `method ="POST"` – andrew Dec 03 '14 at 12:47
  • Actually, it must be POST. – Funk Forty Niner Dec 03 '14 at 12:49
  • now it says `Warning: move_uploaded_file(uploads/paper_4_1): failed to open stream: No such file or directory in C:\xampp\htdocs\cms\admin\showroom_papers_save.php on line 79 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php5E05.tmp' to 'uploads/paper_4_1' in C:\xampp\htdocs\cms\admin\showroom_papers_save.php on line 79 Sorry, there was an error uploading your file.` – FindOut_Quran Dec 03 '14 at 12:52
  • you should be moving the file from `['tmp_name']` element in the files array, do a `var_dump($_FILES)` you'll see what I mean, also see [here](http://stackoverflow.com/questions/14596832/move-uploaded-file-failed-to-open-stream) – andrew Dec 03 '14 at 12:55
  • Thank you very much... I needed to change `$target_dir = "/uploads/";` to `$target_dir = "../uploads/";` – FindOut_Quran Dec 03 '14 at 12:58