2

I currently have an upload option for the user and it will upload to a folder, and update the database with the files name to then be retrieved later on.

I would like to be able to do multiple uploads and each uploaded file go into specific columns per the upload field they've selected and uploaded said file.

3 Browse sections (upload options), 1 being badgephoto, 2 being drivers license, 3 being social security card.

if possible upload each file into their own specified folder location.

I would like to also restrict all files in exception for image types, PDF, or document files.

I currently have the following.

if(isset($_POST['btn-upload']))
{    

    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="badge/";

    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ',$FirstName,$new_file_name);

    if(move_uploaded_file($file_loc,$folder.$final_file))
    {
        $sql="UPDATE users t1 SET t1.badgephoto='$final_file', t1.badgetype='$file_type', t1.badgesize='$new_size' WHERE t1.API='$API'";

        mysql_query($sql);
        ?>
        <script>
        alert('successfully uploaded');
        window.location.href='badgephoto.php?success';
        </script>
        <?php
    }
    else
    {
?>
        <script>
        alert('error while uploading file');
        window.location.href='badgephoto.php?fail';
        </script>
        <?php
    }
}

HTML

    <form action="badgephoto.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" accept="application/msword, text/plain, application/pdf, image/*" /> <!-- would be badgephoto -->
    <input type="file" name="file_drivers" accept="application/msword, text/plain, application/pdf, image/*" />
    <input type="file" name="file_social" accept="application/msword, text/plain, application/pdf, image/*" />
    <button type="submit" name="btn-upload">Upload Badge Photo</button>
    </form>

Any help would be greatly appreciated.

3
  • stackoverflow.com/questions/6755192/… --- refer this post Commented Dec 22, 2015 at 3:19
  • Dont really understand how I would construct that into my code. @pTi Commented Dec 22, 2015 at 3:22
  • 1
    check the full code given below Commented Dec 22, 2015 at 3:30

1 Answer 1

1
if(isset($_POST['btn-upload']))
{    
    foreach ($_FILES as $key => $value) {
    $file = rand(1000,100000)."-".$_FILES[$key]['name'];
    $file_loc = $_FILES[$key]['tmp_name'];
    $file_size = $_FILES[$key]['size'];
    $file_type = $_FILES[$key]['type'];
    $folder="badge/";

    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ',$FirstName,$new_file_name);

    $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
    $detectedType = exif_imagetype($file_loc);
    $fileTypecorrect = in_array($detectedType, $allowedTypes);


        if($new_size < 5120 && $fileTypecorrect){

            if(move_uploaded_file($file_loc,$folder.$final_file))
            {
                $sql="UPDATE users t1 SET t1.badgephoto='$final_file', t1.badgetype='$file_type', t1.badgesize='$new_size' WHERE t1.API='$API'";

                mysql_query($sql);
                ?>
                <script>
                alert('successfully uploaded');
                window.location.href='badgephoto.php?success';
                </script>
                <?php
            }
        }
    }
    else
    {
?>
        <script>
        alert('error while uploading file');
        window.location.href='badgephoto.php?fail';
        </script>
        <?php
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

your html form should use "<input type="file" name="files[]" multiple/>"
okay, I understand that. With the edit you've made it doesnt upload the path of the image to new columns within the db table. The way its structured, it would write the 3 names to the one column, and over right the previous uploads name. right? @pTi
essentially I would just restructure my if statements to run separate queries on the submit, or ? how would I do it all as one. if you can show me what I would do for the 2nd, I can expand it form there.
change looping logic as it incorrect according to the HTML form you used

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.