0

Updating an file array

I have a PHP Crud, and one section has file uploads. There are different file inputs for various documents with different 'names' and they have their separate columns in the database.

One section of those file upload fields requires that the input fields are added dynamically. And it works, the files are being saved to the database. enter image description here

Here is the code for adding the files to the database:

if(isset($_FILES['contract_array'])){
    $name_array = $_FILES['contract_array']['name'];
    $tmp_name_array = $_FILES['contract_array']['tmp_name'];
    $type_array = $_FILES['contract_array']['type'];
    $size_array = $_FILES['contract_array']['size'];
    $error_array = $_FILES['contract_array']['error'];

    $ucontract=  "";

    for($i = 0; $i < count($tmp_name_array); $i++){

        $name_array[$i] = str_replace(' ', '_', $name . "-contract-" . $name_array[$i]);

        if(move_uploaded_file($tmp_name_array[$i], "upload_contract/".$name_array[$i])){
            $uontract.=$name_array[$i].",";

        } else {
               echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }
}


if(!isset($errMSG))
{
    $stmt = $DB_con->prepare('INSERT INTO Table(userContract)VALUES(:ucontract)');
    $stmt->bindParam(':ucontract',$ucontract);
    $stmt->execute();

This works perfect. I add files from the contract_array[] input in an array to the database, and I can also retrieve them (view) on the index page.

However, the problem is when I try to update a profile. When I change the profile picture, or any other document, the update works fine. But I can't update these sections, with the contract_array[]

This is the update code for profile image (which works):

if(isset($_POST['btn_save_updates']))
{
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];

if($imgFile)
{
        $upload_dir = 'user_images/'; // upload directory
        $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
        $userpic = $name . "-image-" . rand(1000,1000000).".".$imgExt;
        if(in_array($imgExt, $valid_extensions))
        {
            if($imgSize < 5000000)
            {

                move_uploaded_file($tmp_dir,$upload_dir.$userpic);
            }
            else
            {
                $errMSG = "Sorry, your file is too large it should be less then 5MB";
            }
        }
        else
        {
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        }
    }
    else
    {
        // if no image selected the old image remain as it is.
        $userpic = $edit_row['userPic']; // old image from database
    }  

Now when I want to update the Contract 2 file from contract_array[], it doesn't update. Other things I try, delete all file values, and I'm left with no records at all. Here is the code for updating the contract_array:

        if($name_array)
        {

        $name_array = $_FILES['contract_array']['name'];
        $tmp_name_array = $_FILES['contract_array']['tmp_name'];
        $type_array = $_FILES['contract_array']['type'];
        $size_array = $_FILES['contract_array']['size'];
        $error_array = $_FILES['contract_array']['error'];

        $ucontract=  "";

        for($i = 0; $i < count($tmp_name_array); $i++){

            $name_array[$i] = str_replace(' ', '_', $name . "-contract-new" . $name_array[$i]);

            if(move_uploaded_file($tmp_name_array[$i], "upload_contract/".$name_array[$i])){

                $uucontract.=$name_array[$i].",";
            } else {
                echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
            }
        }
    }
    else
    {
    // if no image selected the old image remain as it is.
    $ucontract = $edit_row['userContract']; // old image from database
}
xerox
  • 93
  • 3
  • 11
  • What error do you get? – mega6382 Oct 13 '17 at 08:30
  • @mega6382 There is no error. The just don't update the current files. In one instance that I tried, when I upload 1 file (from 6) only 1 file remains, the other 5 get deleted – xerox Oct 13 '17 at 08:32
  • Is error reporting [enabled](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? – Script47 Oct 13 '17 at 08:33
  • I enabled now error reporting, it says: Notice: Undefined variable: name_array in ... Notice: Undefined variable: name_array_2... Notice: Undefined variable: name_array_3.... – xerox Oct 13 '17 at 08:41
  • When I place the "$name_array = $_FILES['contract_array']['name'];" before the if statement, and I choose any file to update, the last else statement is started: "move_uploaded_file function failed for Name-contract-" – xerox Oct 13 '17 at 08:49

0 Answers0