Projet

Général

Profil

« Précédent | Suivant » 

Révision 2858c9fe

ID2858c9fe7b27cd69b90715c5e67f4e0d7ba642e1
Parent ffa856dc
Enfant afe20e9f

Ajouté par Jocelyn Delande il y a plus de 10 ans

allow upload with same names

Fichiers

  • ajouté
  • modifié
  • copié
  • renommé
  • supprimé

Voir les différences

Révisions

class/utils.class.php
63 63
    }
64 64
    return $v;
65 65
  }
66

  
67
  public static function get_unique_filepath($path) {
68
    /** To handle uploads with same name : for a given path, suffix it with _<n>
69
    (keeping trailing extension)
70
    * till it find a non-preexistant_path and returns it.
71
    */
72
    if (file_exists($path)) {
73
      $info = pathinfo($path);
74
      $extension = $info['extension'];
75
      $remain = self::strip_extension($path);
76
      $n = 0;
77
      do {
78
        $n++;
79
        $fn = sprintf('%s_%d.%s', $remain, $n, $extension);
80
      } while (file_exists($fn));
81
      return $fn;
82

  
83
    } else {
84
      return $path;
85
    }
86
  }
87

  
66 88
}
67 89

  
68 90
?>
uploadReceive.php
2 2
 
3 3
require_once('class/FormValidator.class.php');
4 4
require_once('class/site_point.class.php');
5
require_once('class/utils.class.php');
5 6
require_once('constants.inc.php');
6 7

  
7 8
class UploadReceiveError extends Exception {}
......
19 20
  foreach ($_FILES['files']['name'] as $i => $file) {
20 21
    $file_err = $_FILES['files']['error'][$i];
21 22
    $file_tmp = $_FILES['files']['tmp_name'][$i];
22
    $file_finalpath = UPLOAD_PATH.'/'.basename($file);
23
    $file_finalpath = utils::get_unique_filepath(UPLOAD_PATH.'/'.basename($file));
23 24

  
24 25
    if(!empty($file)) {
25 26
      if(isset($file) && UPLOAD_ERR_OK === $file_err) {