2017-05-25 84 views
0

上傳文件時出現問題,文件運行不正常 這是我的php文件,有沒有寫錯誤或者什麼?在PDO中上傳文件時出現問題

警告:的unlink(DIST/IMG /):權限被拒絕在C:\ XAMPP \ htdocs中\ project_admin \ edit_user.php上線

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> 

有沒有人誰可以幫我?

回答

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."; 
} 
+0

文件成功上傳代碼中的註釋,只是它沒有記錄在數據庫上 – Kabuki29