Projet

Général

Profil

Révision 137a4cfc

Ajouté par Jocelyn Delande il y a presque 11 ans

does the same upload job, bet refactored the form and receive action for future

Voir les différences:

class/FormValidator.class.php
40 40
						$sanitized_f = $request[$fieldname];
41 41
					}
42 42
				} else {
43
					$val = $request[$fieldname];
44
					try {
45
						$sanitized_f = $this->validate_field($validator, $val);
46
					} catch (FieldValidationError $e) {
47
						$err = $e->getMessage();
48
						break;
43
					if (isset($request[$fieldname]) and $request[$fieldname]) {
44
						$val = $request[$fieldname];
45
						try {
46
							$sanitized_f = $this->validate_field($validator, $val);
47
						} catch (FieldValidationError $e) {
48
							$err = $e->getMessage();
49
							break;
50
						}
49 51
					}
50 52
				}
51 53
			}
......
92 94
  'numeric',
93 95
  function ($v) {
94 96
	  $sanitized = floatval($v);
95
    if ($sanitized === false) {
97
	  if (($sanitized === false) || (!is_numeric($v))) {
96 98
	  	throw new FieldValidationError('n\'est pas une valeur numérique');
97 99
	  } else {
98 100
		  return $sanitized;
envoyer.php
33 33
			<ul>
34 34
			  <li><input type="file" name="files[]" id="file" multiple="multiple"/></li>
35 35

  
36
			  <li><input type="checkbox" name="loop">Panorama bouclant
36
			  <li><input type="checkbox" name="loop" value="true">Panorama bouclant
37 37
			  (360°)</li>
38 38
			</ul>
39 39
		<p class="help">
uploadReceive.php
1
<!DOCTYPE html>
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
3
<head>
4
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
5
   <title>Transfert de panoramique</title>
6
</head>
7
<body>
8 1
<?php
9 2
 
10
$dest_dir ='upload';
3
require_once('class/FormValidator.class.php');
4
require_once('constants.inc.php');
5

  
6
class UploadReceiveError extends Exception {}
7

  
8

  
9
////////////////////// actions //////////////////////////////////////////
11 10

  
12
if (! is_dir($dest_dir)) {
13
    if (! mkdir($dest_dir)) {
14
        $message = 'Dossier "'.$dest_dir.'" non inscriptible ou inexistant.';
11
function handle_upload() {
12
  if (! is_dir(UPLOAD_PATH)) {
13
    if (! mkdir(UPLOAD_PATH)) {
14
      throw new UploadReceiveError(
15
        'Dossier "'.UPLOAD_PATH.'" non inscriptible ou inexistant.');
15 16
    }
16
}
17
  }
18
  foreach ($_FILES['files']['name'] as $i => $file) {
19
    $file_err = $_FILES['files']['error'][$i];
20
    $file_tmp = $_FILES['files']['tmp_name'][$i];
21
    $file_finalpath = UPLOAD_PATH.'/'.basename($file);
17 22

  
18
if(!empty($_POST) && !empty($_FILES)) {
19
  foreach (array_keys($_FILES['files']['name']) as $i) {
20
    if(!empty($_FILES['files']['name'][$i])) {
21
      if(isset($_FILES['files']['error'][$i]) && UPLOAD_ERR_OK === $_FILES['files']['error'][$i]) {
22
	move_uploaded_file($_FILES['files']['tmp_name'][$i],$dest_dir.'/'.basename($_FILES['files']['name'][$i]));
23
	printf("<h2>transfert de %s réalisé</h2>\n", $_FILES['files']['name'][$i]);
23
    if(!empty($file)) {
24
      if(isset($file) && UPLOAD_ERR_OK === $file_err) {
25
	      move_uploaded_file($file_tmp, $file_finalpath);
26
        return sprintf("transfert de %s réalisé",  $file);
24 27
      } else {
25
	$message = 'Une erreur interne a empêché l\'upload de l\'image : '. $_FILES['files']['error'][$i];
28
        throw new UploadReceiveError(
29
          'Une erreur interne a empêché l\'envoi de l\'image :'. $file_err);
26 30
      }
27 31
    } else {
28
      $message = 'Veuillez passer par le formulaire svp !';
32
      throw new UploadReceiveError(
33
        'Veuillez passer par le formulaire svp !');
29 34
    }
30 35
  }
31
  if (isset($message)) {
32
    echo "<h2>$message</h2>\n";
36
}
37

  
38
////////////////////// main //////////////////////////////////////////
39

  
40
$fields_spec = array('lat'         => array('required', 'numeric', 'positive'),
41
                     'lon'         => array('numeric', 'positive'),
42
                     'alt'  => array('numeric'),
43
);
44

  
45
$validator = new FormValidator($fields_spec);
46
$upload_success = false;
47

  
48
////// STEP 1 : UPLOAD ////////
49

  
50
if ($validator->validate($_REQUEST)) {
51
  try {
52
    $message = handle_upload();
53
    $upload_success = true;
54
  } catch (UploadReceiveError $e) {
55
    $message = $e->getMessage();
56
  }
57
} else {
58
  $message = 'paramètres invalides';
59
}
60

  
61
////// STEP 2 : PARAMETERS ////////
62

  
63
$params_success = false;
64

  
65
if ($upload_success) {
66
  //pass
67
}
68

  
69

  
70
?>
71

  
72
<!DOCTYPE html>
73
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
74
<head>
75
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
76
   <title>Transfert de panoramique</title>
77
</head>
78
<body>
79
<?php
80
if (isset($message)) {
81
  echo "<h2>$message</h2>\n";
82
  if ($validator->errors()) {
83
    foreach($validator->errors() as $key => $error) {
84
      printf('<p>"%s" : %s</p>', $_REQUEST[$key], $error);
85
    }
86

  
87
  } else {
88
?>
89
    <p>Pour acceder à la liste des images transférées afin de convertir en panorama <a href="creerPano.php">cliquer ici</a></p>
90
<?php 
33 91
  }
34 92
}
35 93
?>
36
<p>Pour acceder à la liste des images transférées afin de convertir en panorama <a href="creerPano.php">cliquer ici</a></p>
37 94
</body>
38 95
</html>

Formats disponibles : Unified diff