PHP CRUD - 文件阵列更新

时间:2017-10-13 08:28:28

标签: php mysql arrays database

更新文件数组

我有一个PHP Crud,一个部分有文件上传。 对于具有不同名称的各种文档,存在不同的文件输入。并且他们在数据库中有各自的列。

这些文件上传字段的一部分要求动态添加输入字段。它工作正常,文件被保存到数据库中。 enter image description here

以下是将文件添加到数据库的代码:

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();

这很完美。我将数组中contract_array []输入的文件添加到数据库中,我也可以在索引页面上检索它们(视图)。

然而,问题是当我尝试更新配置文件时。当我更改个人资料图片或任何其他文档时,更新工作正常。但我无法使用 contract_array []

更新这些部分

这是个人资料图片的更新代码(有效):

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
    }  

现在,当我想从contract_array []更新Contract 2文件时,它不会更新。我尝试的其他事情,删除所有文件值,我没有记录任何记录。以下是更新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
}

0 个答案:

没有答案