From 5d7b14322776425062ed6763a0ac60afbce6c10d Mon Sep 17 00:00:00 2001
From: nicrausaz <n.crausaz99@gmail.com>
Date: Wed, 6 Mar 2019 10:51:58 +0100
Subject: [PATCH] fix file replace

---
 canapEPFL/helpers.php        | 10 ++++------
 canapEPFL/templates/head.php |  4 ++--
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/canapEPFL/helpers.php b/canapEPFL/helpers.php
index 7f0a781..75482e5 100644
--- a/canapEPFL/helpers.php
+++ b/canapEPFL/helpers.php
@@ -293,16 +293,17 @@ function deletePostulation ($pdo, $id, $sciper, $rep1, $rep2) {
   foreach ($filesToRemove as $key => $file) {
     unlink($file['file_path']);
   }
+  // delete applicant folder
   $dir = dirname($file['file_path']);
   rmdir($dir);
 
-  // delete files (DB only)
+  // delete files from DB
   $sqlreq = "DELETE FROM file WHERE fk_applicant_id=:id";
   $query = $pdo->prepare($sqlreq);
   $query->bindParam(':id', $id, PDO::PARAM_STR);
   $query->execute();
 
-  echo "delete :$id";
+  // delete applicant personal data
   $sqlreq = "DELETE FROM applicant WHERE applicant_id=:id";
   $query = $pdo->prepare($sqlreq);
   $query->bindParam(':id', $id, PDO::PARAM_STR);
@@ -484,8 +485,8 @@ function updatePostulation ($pdo, $posted_data) {
     $file_data = getOneFile($pdo, $file['id'], $posted_data->id);
     $extension = strtolower("." . pathinfo($file['name'])['extension']);
     $filename = $key . $extension;
-    $oldpath = $file_data['file_path'];
     $path = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_data['file_path']) . $extension;
+    unlink($file_data['file_path']);
 
     if (move_uploaded_file($file['tmp_name'], $path)) {
       // success, update db
@@ -495,11 +496,8 @@ function updatePostulation ($pdo, $posted_data) {
       $query->bindParam(':file_name', $filename, PDO::PARAM_STR);
       $query->bindParam(':file_id', $file_data['file_id'], PDO::PARAM_STR);
       $query->execute();
-      // TODO: check if working
-      unlink($path);
     }
   }
-// header('Location: viewpostulation.php');
 }
 #endregion
 ?>
\ No newline at end of file
diff --git a/canapEPFL/templates/head.php b/canapEPFL/templates/head.php
index 1639fee..1169763 100644
--- a/canapEPFL/templates/head.php
+++ b/canapEPFL/templates/head.php
@@ -23,8 +23,8 @@
 <?php
   ini_set("display_errors", 0);
   error_reporting(0);
-  // ini_set("display_errors", 1);
-  // error_reporting(E_ALL);
+  ini_set("display_errors", 1);
+  error_reporting(E_ALL);
   require_once('configs/config.php');
   require('configs/db.php');
   if (!isset($_SESSION)) { session_start(); }
-- 
GitLab