在PDO中上传文件时出现问题

时间:2017-05-25 07:40:58

标签: php mysql pdo

我在上传文件时遇到问题,文件运行不正常 这是我的php文件,有没有写错误或什么?

  

警告:取消关联(dist / img /):在 C:\ xampp \ htdocs \ project_admin \ edit_user.php 上的权限被拒绝    45

第45行:

 unlink($upload_dir.$avatar);

php代码:

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

      $id        = $_GET['id'];
      $email     = isset($_POST['_em']) ? $_POST['_em'] : null;
      $fname     = isset($_POST['_fn']) ? $_POST['_fn'] : null;
      $lname     = isset($_POST['_ln']) ? $_POST['_ln'] : null;
      $web_usr   = isset($_POST['_web']) ? $_POST['_web'] : null;
      $usr_note  = isset($_POST['_note']) ? $_POST['_note'] : null;
      $usr_edu   = isset($_POST['_edu']) ? $_POST['_edu'] : null;
      $usr_skill = isset($_POST['_skill']) ? $_POST['_skill'] : null;
      $imgFile   = isset($_FILES['user_image']['name']);
      $tmp_dir   = isset($_FILES['user_image']['tmp_name']);
      $imgSize   = isset($_FILES['user_image']['size']);

    if ($imgFile) {
      $upload_dir   = 'dist/img/';
      $imgExt   = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
      $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
      $avatar  = rand(1000, 1000000).".".$imgExt;

      if(in_array($imgExt, $valid_extensions)) {
        if ($imgSize < 1000000) {
          unlink($upload_dir.$avatar);
          move_uploaded_file($tmp_dir, $upload_dir.$avatar);
        } else {
          $errMSG = "Sorry, your file is too large it should be less then 1MB";
        }
      } else {
        $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      }
    }

        //Retrieve the user account information by id.
        if(!isset($errMSG)) {
          $object->Update($id, $email, $fname, $lname, $web_usr, $usr_note, $avatar, $usr_edu, $usr_skill, $avatar);
          $sucMSG = "<strong>WOW!</strong> Record was updated successfully";
        } else {
          $errMSG = "Sorry Data Could Not Updated !";
        }
      }

HTML代码

    <form class="form-horizontal" method="POST" >
      <div class="box-body">
        <div class="form-group">
        <input type="hidden" name="id" value="<?php echo $id; ?>">
          <label for="inputUserName" class="col-sm-2 control-label">Username</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="inputUserName" value="<?php echo $username; ?>" disabled>
            <p class="help-block"><em>You require permission from Administrators to change it.</em></p>
          </div>
        </div>
        <div class="form-group">
          <label for="inputFirstName" class="col-sm-2 control-label">First Name</label>
          <div class="col-sm-10">
            <input type="text" name="_fn" class="form-control" id="inputFirstName" value="<?php echo $fname; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputLastName" class="col-sm-2 control-label">Last Name</label>
          <div class="col-sm-10">
            <input type="text" name="_ln" class="form-control" id="inputLastName" value="<?php echo $lname; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail" class="col-sm-2 control-label">Email</label>
          <div class="col-sm-10">
            <input type="email" name="_em" class="form-control" id="inputEmail" value="<?php echo $email; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail" class="col-sm-2 control-label">Website</label>
          <div class="col-sm-10">
            <input type="text" name="_web" class="form-control" id="inputEmail" value="<?php echo $web_usr; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEducation" class="col-sm-2 control-label">Education</label>
          <div class="col-sm-10">
            <input type="text" name="_edu" class="form-control" id="inputEducation" placeholder="Education" value="<?php echo $usr_edu; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputNote" class="col-sm-2 control-label">Notes</label>
          <div class="col-sm-10">
            <textarea class="form-control" id="inputNote" placeholder="Note" name="_note"><?php echo $usr_note; ?></textarea>
          </div>
        </div>
        <div class="form-group">
          <label for="inputSkill" class="col-sm-2 control-label">Skill</label>
          <div class="col-sm-10">
            <select class="form-control select2" name="_skill" value="<?php echo $usr_skill; ?>" multiple="multiple" data-placeholder="Skill" >
            </select>
          </div>
        </div>
        <div class="form-group">
          <label for="inputRole" class="col-sm-2 control-label">Role</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="inputRole" value="<?php echo $id_admrole; ?>" disabled>
            <p class="help-block"><em>You require permission from Administrators to change it.</em></p>
          </div>
        </div>
        <div class="form-group">
          <label for="inputFile3" class="col-sm-2 control-label">Foto Profil</label>
          <div class="col-sm-10">
            <div class="input-group">
              <label class="input-group-btn">
                <span class="btn btn-primary">
                  Browse&nbsp; <input type="file" style="display: none;" accept="image/*" id="file" name="user_image">
                </span>
              </label>
              <input type="text" class="form-control" value="<?php echo $avatar; ?>" readonly>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" name="update" class="btn btn-danger" value="update">UPDATE</button>
          </div>
        </div>
      </div>
      <!-- /.box-body -->
    </form>

有没有人可以帮助我?

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试删除目录,但我很确定您打算从该目录中删除图像文件。

问题出在这里,请参阅代码中的注释

if ($imgFile) {
    $upload_dir   = 'dist/img/';
    $imgExt   = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
    $avatar  = rand(1000, 1000000).".".$imgExt;

    if(in_array($imgExt, $valid_extensions)) {
        if ($imgSize < 1000000) {

        // ERROR here
        //unlink($upload_dir);

        // add the filename to the directory name
        unlink($upload_dir.$imgFile);

        // Or maybe it should be 
        unlink($upload_dir.$avatar);

        move_uploaded_file($tmp_dir, $upload_dir.$avatar);
    } else {
        $errMSG = "Sorry, your file is too large it should be less then 1MB";
    }
} else {
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}