为什么我的代码未将FILE_NAME和FILE_TYPE写入数据库?

时间:2019-02-11 16:52:44

标签: php mysql

脚本运行时,数据库中的FILE_NAME和FILE_TYPE为0。有人知道任何建议吗?

我整个周末都在Google上搜索,但没有发现问题所在。 但是,在image_uploads文件夹中,有图片。

if(isset($_FILES['files'])){
    $query = "INSERT into auktion_images(`ANGEBOT_ID`, `FILE_NAME`, `FILE_SIZE`,`FILE_TYPE`)
             VALUES(:ANGEBOT_ID, :FILE_NAME, :FILE_SIZE, :FILE_TYPE)";
    $stmt  = $pdo->prepare($query);

    foreach($_FILES['files']['tmp_name'] as $key => $error ){
        if ($error != UPLOAD_ERR_OK) {
            $errors[] = $_FILES['files']['name'][$key] . ' was not uploaded.';
            continue;
        }

        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp  = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[] = 'File size must be less than 2 MB';
            continue;
        }
        try{
            $stmt->bindParam( ':ANGEBOT_ID', $change, PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_NAME', $file_name , PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_SIZE', $file_size, PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_TYPE', $file_type, PDO::PARAM_STR );
            $stmt->execute();

            $desired_dir="image_uploads";

            if(is_dir($desired_dir)==false){
                mkdir($desired_dir, 0700);// Create directory if it does not exist
            }
            if(is_file($desired_dir.'/'.$file_name)==false){
                move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
            }else{    //rename the file if another one exist
                $new_file=$desired_dir.'/'.$file_name.time();
                move_uploaded_file($file_tmp,$new_file) ;               
            }
        }catch(PDOException $e){
            $errors[] = $file_name . 'not saved in db.';
            echo $e->getMessage();
        }   
    }
}

0 个答案:

没有答案