-1

I'm a PHP beginner and i managed to muscle up a code which has a person upload up to 4 documents to a designated folder on a server. I'm now having trouble writing code which takes these 4 document names and adds them to that person's column with the rest of input data. I believe the right approach is to go with a "foreach" loop which increments variable name every time it goes through uploaded file names. I've tried doing this with $documentname[$i] = $file_name; but it's not working. This is what I have so far:

   $upload_dir    = 'uploads/';
$allowed_types = array(
    'doc',
    'docx'
);
$maxsize       = 4 * 1024 * 1024;
if (!empty(array_filter($_FILES['files']['name']))) {
    // var_dump($_FILES);
    // die();
    $i=1;
    foreach ($_FILES['files']['tmp_name'] as $key => $value) {
        $file_tmpname = $_FILES['files']['tmp_name'][$key];
        $file_name    = $_FILES['files']['name'][$key];
        $file_size    = $_FILES['files']['size'][$key];
        $file_ext     = pathinfo($file_name, PATHINFO_EXTENSION);
        $filepath     = $location . $file_name;
        $documentname[$i] = $file_name;
       
        
        if (in_array(strtolower($file_ext), $allowed_types)) {
            
            if ($file_size > $maxsize)
                echo "Greška, datoteke su veće od dozvoljene vrijednosti (4MB)";
            
            if (file_exists($filepath)) {
                $filepath = $location . time() . $file_name;
                
                if (move_uploaded_file($file_tmpname, $filepath)) {
                    echo "{$file_name} uspješno uploadan <br />";
                } else {
                    echo "Error uploading {$file_name} <br />";
                }
            } else {
                
                if (move_uploaded_file($file_tmpname, $filepath)) {
                    echo "{$file_name} uspješno uploadan <br />";
                    
                } else {
                    echo "Error uploading {$file_name} <br />";
                }
            }
        } else {
            
            // If file extention not valid 
            echo "Error uploading {$file_name} ";
            echo "({$file_ext} file type is not allowed)<br / >";
        }
    }
} else {
    
    // If no files selected 
    echo "No files selected.";
}}

And this is the sql code:

 if (isset($_POST['signup'])) {
$fname                = $_POST['fname'];
$lname                = $_POST['lname'];
$documentname1        = $_POST['documentname1'];
$documentname2        = $_POST['documentname2'];
$documentname3        = $_POST['documentname3'];
$documentname4        = $_POST['documentname4'];
$msg = mysqli_query($con, "insert into users(fname,lname,documentname1,documentname2,documentname3,documentname4)
values('$fname','$lname','$documentname1','$documentname2','$documentname3','$documentname4')");

So the question is: is it possible to iterate through the uploaded files name array and assign each file name a variable like #documentname1,#documentname2,... to write these names in the database? Thank you in advance!

Čedomir Babić
  • 522
  • 4
  • 12
  • Why would you want to do this? This is a really bad design. You need to store them in a table not in separate columns – Dharman Jun 23 '20 at 10:38

1 Answers1

1

Change your code to look like this. And also use prepared statement - PDO

if (isset($_POST['signup'])) {

$upload_dir    = 'uploads/';
$allowed_types = array(
    'doc',
    'docx'
);
$fname                = $_POST['fname'];
$lname                = $_POST['lname'];
$countfiles = count($_FILES['files']['name']);
$doc_name = [];
$maxsize       = 4 * 1024 * 1024;
if (!empty(array_filter($_FILES['files']['name']))) {
    // var_dump($_FILES);
    // die();
  
    for ($i=0;$i<$countfiles;$i++) {
        $file_tmpname = $_FILES['files']['tmp_name'][$i];
        $file_name    = $_FILES['files']['name'][$i];
        $file_size    = $_FILES['files']['size'][$i];
        $file_ext     = pathinfo($file_name, PATHINFO_EXTENSION);
        $filepath     = $location . $file_name;
        $doc_name[]    += $_FILES['files']['name'][$i];
       
        
        if (in_array(strtolower($file_ext), $allowed_types)) {
            
            if ($file_size > $maxsize)
                $error[] = "Greška, datoteke su veće od dozvoljene vrijednosti (4MB)";
            
            if (file_exists($filepath)) {
                $filepath = $location . time() . $file_name;
                
                if (move_uploaded_file($file_tmpname, $filepath)) {
                    $success[] = "{$file_name} uspješno uploadan <br />";

                } else {
                    $error[] = "Error uploading {$file_name} <br />";
                }
            } 
        } else {
            
            // If file extention not valid 
            $error[] = "Error uploading {$file_name} ";
            $error[] = "({$file_ext} file type is not allowed)<br / >";
        }
    }
} else {
    
    // If no files selected 
    $error[] = "No files selected.";
}}

if (!isset($error)) {
$documentname1        = $doc_name[0]
$documentname2        = $doc_name[1]
$documentname3        = $doc_name[2]
$documentname4        = $doc_name[3]
$msg = mysqli_query($con, "insert into users(fname,lname,documentname1,documentname2,documentname3,documentname4)
values('$fname','$lname','$documentname1','$documentname2','$documentname3','$documentname4')");
}

}

to show errors on your html
if(isset($error)){
                    foreach($error as $error){
                      echo '<div class="alert alert-danger" role="alert">
                      <button class="close" data-dismiss="alert"></button>' .$error.'<br /> </div>';
                    }
                  }  

to show success
if(isset($success)){
                    foreach($success as $success){
                      echo '<div class="alert alert-success" role="alert">
                      <button class="close" data-dismiss="alert"></button>' .$success.'<br /> </div>';
                    }
                  }