1

In my code i created a folder and sub folders for that directory. So when a user sends files, I create a directory with their "userid_fullname",loop through the files, and submit to fullname folder. But every time a user submits files, the first one would go to the folder and others would disappear. I believe the problem is this my "$key" variable because it controls the count. Here is my code.

PHP

<?php

if($_SERVER['REQUEST_METHOD'] =="POST"){
    $currentDirectory = getcwd();

    $userid = "8503";
    $fullname = "peopl39e";    


    foreach($_FILES['file']['tmp_name'] as $key => $error){

        $file_tmp = file_get_contents($_FILES['file']['tmp_name'][$key]);
         //keep only A-Z and 0-9 and everything else KILL
        $file_name = preg_replace("/[^a-z0-9\.]/", "_", strtolower($_FILES['file']['name'][$key]));
        $file_name = strtotime("now")."_".$file_name;

        $dir = "devPacks/" .$userid."_".$fullname;
        if(is_dir($dir)==false){

                mkdir($dir, 0777);
            }
        if(!move_uploaded_file($_FILES['file']['tmp_name'][$key],$dir.'/'.$file_name)){

                die("File didn't send!");

    }else{


            die("GOOD");
        }
}
}

?>

<form method="post" enctype="multipart/form-data" autocomplete="off">
    <br>
    Select Pack Screenshots/Video: <input type="file" name="file[]" multiple>
<br></br>
    <input type="submit" name="submit">
</form>
5
  • whats the die("GOOD");for? if you can move the files are not your code will quit. (either with "File didn't send!" or "GOOD") Commented Jul 30, 2017 at 3:39
  • Its just for testing purposes. Commented Jul 30, 2017 at 4:15
  • Ok, but if you have that in, your loop wont run more than once because the die will end the execution Commented Jul 30, 2017 at 4:50
  • Wow i didn't even catch that... Thank you Commented Jul 30, 2017 at 5:23
  • I have posted this as a form of an answer Commented Jul 30, 2017 at 6:05

1 Answer 1

1

The following portion of your code will stop execution after the loop runs once:

 if(!move_uploaded_file($_FILES['file']['tmp_name'][$key],$dir.'/'.$file_name)){

            die("File didn't send!");

}else{


        die("GOOD");
    }
}

If I understand correctly you added the die("GOOD"); line so that you dont see the form after it is submitted once. You can do something like the below to achieve this:

$uploadCount = 0;
if(!move_uploaded_file($_FILES['file']['tmp_name'][$key],$dir.'/'.$file_name)){
$uploadCount++;
}
die("$uploadCount file(s) uploaded");

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.