Bonjour,
Je suis bloqué depuis ce matin sur un problème d'envoi de fichier sur une validation AJAX.
Sachant que dans mon fichier php de traitement il ne voit pas ce que je lui envoie, je trouve ça assez bizarre.
Voici le code :
Ci-dessous la requête ajax avec le code HTML.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 $(document).ready(function() { $('#bouton_form').click(function() { // Check file selected or not $.ajax({ url: 'squelettes-melanie/lib/upload_file.php', type: 'post', dataType : "JSON", data : $('#formulaire').serialize(), enctype: 'multipart/form-data', success: function(data){alert(data.message);}, error : function(request, status, error) { alert(request.responseText); } //} }); });
Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <form name="formulaire" id="formullaire" method="POST" action="<?php echo $url;?>" enctype="multipart/form-data" > ......... <div id="contactLine"> <div class="intitule"></div> <div class="result"> <input type="file" name="identite" value="" placeholder="Fichier .pdf" accept="application/pdf" id="identite" onchange="picture_id();"/> </div> <div class="intitule"></div> </div>
Fichier php :
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 header("Access-Control-Allow-Origin: *"); // echo "ok"; try { //print_r($_FILES); // DIE('yep'); $id = "[RANDOM_GENERATED_GUID]"; $targetDir = "squelettes-melanie/lib/pdf/images/identites/"; if (!is_dir($targetDir)) { if (!mkdir($targetDir, 0777, true)) { throw new Exception("Unable to upload your document. We were unable to create the required directories"); } } $targetFile = $targetDir . $id . ".pdf"; $fileType = pathinfo($targetFile, PATHINFO_EXTENSION); if (file_exists($targetFile)) { throw new Exception("Unable to upload your document. The file already exists"); } if ($_FILES["#identite"]["size"] > 2000000) { throw new Exception("Unable to upload your document. The file is to large (Maximum of 2MB)"); } if ($fileType != "pdf") { throw new Exception("Unable to upload your document. Only PDF documents can be uploaded"); } if (!move_uploaded_file($_FILES["#identite"]["tmp_name"], $targetFile)) { //Keeps failing here with error code 0 throw new Exception("Unable to upload your document. There was an error uploading the file"); } echo json_encode(array( "error" => false, "message" => "Your document was successfully uploaded" )); } catch (Exception $ex) { echo json_encode(array( "error" => true, "message" => $ex->getMessage() )); }
Voilà et en fait il n'arrive pas à récupérer le fichier nommé identite.
Si vous avez une idée.
Merci à vous.
Ciol
Partager