dtlc84438 2017-02-02 10:44
浏览 23

PHP - 具有多个不同文件输入的用户配置文件

So I have searched a lot about this, but can't find any good answer. I have a hrm, where I add Users and each of them has their info like name, age, address, etc etc. I also add couple of files, like: image, ID card (jpg/pdf), Tax card (pdf), Agreement document (pdf) and there are couple of more.

Each of these documents have their seperate input field, and each will be uploaded in a different folder on the server. I got it working, but I am not sure if my method is the right way:

Basic Info (there are lot of inputs here so I won't list them all):

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

    $id  = $_POST['w_rid'];
    $name  = $_POST['w_name'];
    $surname  = $_POST['w_surname'];
    $position  = $_POST['w__position'];

after these infos, I have my file uploads:

    $userID = $_FILES['user_id']['name'];
    $id_tmp_dir = $_FILES['user_id']['tmp_name'];
    $idSize = $_FILES['user_id']['size'];

    $userTaxCard = $_FILES['user_taxcard']['name'];
    $taxCard_tmp_dir = $_FILES['user_taxcard']['tmp_name'];
    $taxCardSize = $_FILES['user_taxcard']['size'];

    $userAgreement = $_FILES['user_agreement']['name'];
    $agreement_tmp_dir = $_FILES['user_agreement']['tmp_name'];
    $agreementSize = $_FILES['user_agreement']['size'];

    $imgFile = $_FILES['user_image']['name'];
    $img_tmp_dir = $_FILES['user_image']['tmp_name'];
    $imgSize = $_FILES['user_image']['size'];

Currently I have 18 different file upload inputs, with different names

Now I have the part to upload the files, and for each of those 18 file inputs, I have this block of code which I just repeat and replace the names:

  if($_FILES['user_id']['error'] == 0) {

  $upload_dir_id = 'user_id/';

  $idExt = strtolower(pathinfo($userID,PATHINFO_EXTENSION));
  $valid_extensions_id = array('pdf'); // valid extensions
  $userid = $name . "-id-" . rand(1000,1000000).".".$idExt;

  if(in_array($idExt, $valid_extensions_id)){

    if($idSize < 5000000)        {

      move_uploaded_file($id_tmp_dir,$upload_dir_id.$userid);
    }
    else{
      $errMSG = "Sorry, your file is too large.";
    }
  }
  }

You can imagine how big the code is

And this is the last chunk of code:

    // if no error occured, continue ....
    if(!isset($errMSG))
    {
        $stmt = $DB_con->prepare('INSERT INTO Radnici2(id, name, surname, position, userTaxCard, userAgreement, imgFile, userID) VALUES(:urid, :uime, :uprezime, :uroditelj, :usprema, :uzdravlje, :uljekarski, :upradnika)');

        $stmt->bindParam(':uid',$id);
        $stmt->bindParam(':uname',$name);    
        $stmt->bindParam(':usurname',$surname);
        $stmt->bindParam(':uposition',$position);;
        $stmt->bindParam(':utaxc',$usertaxc);
        $stmt->bindParam(':uagreement',$useragreement);
        $stmt->bindParam(':upic',$userpic);

        if($stmt->execute())
        {
            $successMSG = "Success";
            header("refresh:3;index.php"); // redirects image view page after 5 seconds.
        }
        else
        {
            $errMSG = "Error....";
        }
    }

Is this a valid way to upload files? It doesn't feel that this what I did is correct, or that it can be greatly improved. Is there any better way? Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离