SNpn
SNpn

Reputation: 2207

How to upload files from form to server and store in a new folder

I'm having trouble trying to upload files from a form and create a new folder and store those forms.

My code is:

for($i=0; $i<count($_FILES['uploadfiles']['name']); $i++) {
    $temp = explode(".", strtolower($_FILES['uploadfiles']["name"][$i]));
    $extension = end($temp);
    //var_dump($extension);
    if ((($_FILES['uploadfiles']["type"][$i] == "image/pdf")
        || ($_FILES['uploadfiles']["type"][$i] == "image/jpeg")
        || ($_FILES['uploadfiles']["type"][$i] == "image/jpg")
        || ($_FILES['uploadfiles']["type"][$i] == "image/pjpeg")
        || ($_FILES['uploadfiles']["type"][$i] == "image/png")
        || ($_FILES['uploadfiles']["type"][$i] == "image/x-png"))
        && in_array($extension, $allowedExtensions)) {
          if ($_FILES['uploadfiles']["error"][$i] > 0) {
            echo "Return Code: " . $_FILES['uploadfiles']["error"][$i] . "<br>";
          } else {
            move_uploaded_file($_FILES['uploadfiles']["tmp_name"][$i],
               $folder_destination . $_FILES['uploadfiles']["name"][$i]);
          }
    }
  }

I would like to create a folder or check if the folder exists and write the files submitted via the form to that folder.

Upvotes: 0

Views: 61

Answers (3)

leopik
leopik

Reputation: 2351

Based on the warning you're getting, I would guess that the problem is that you don't have a permission to write to the folder or the folder doesn't exist.

You can check chmod function to change the rights or do it manually on the server.

Upvotes: 0

Gipsz Jakab
Gipsz Jakab

Reputation: 431

Before the for() loop, you should do:

if (!is_dir($folder_destination)) { mkdir($folder_destination,0777); }

This will create the folder with global write permissions in case if it's not yet present.

Upvotes: 1

Ram Sharma
Ram Sharma

Reputation: 8819

You can create folder like that but make sure you have write permission

if (!file_exists($folder_destination)) {
    mkdir($folder_destination, 0777, true); 
}

copy your file to there like that

move_uploaded_file($_FILES['uploadfiles']["tmp_name"][$i], $folder_destination . $_FILES['uploadfiles']["name"][$i]);

Upvotes: 1

Related Questions