bonjour à tous,
j'ai trouvé un script sur le net mais il ne semble pas fonctionner sur mon site.
Voici le code source
Code html : 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 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>AJAX Image Uploading</title> </head> <body> <p>Image uploader</p> <form id="formAjax" action="uploadHandling.php" method="POST"> <input type="file" id="fileAjax" name="fileAjax" /><br /><br /> <input type="submit" id="submit" name="submit" value="Upload" /> </form> <p id="status"></p> <script type="text/javascript" src="imageUpload.js"></script> </body> </html>
voici le uploadHandling.php
et le fichier imageUpload.js
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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 <?php $currentDir = getcwd(); $uploadDirectory = "./uploads/"; // Store all errors $errors = []; // Define available file extensions $fileExtensions = ['jpeg','jpg','png','gif']; //if(!empty($_POST['fileAjax']) || $_FILES['image']) { $fileName = $_FILES['myFile']['name']; $fileTmpName = $_FILES['myFile']['tmp_name']; $fileType = $_FILES['myFile']['type']; $fileExtension = strtolower(pathinfo($fileName,PATHINFO_EXTENSION)); $uploadPath = $currentDir . $uploadDirectory . basename($fileName); //echo $uploadPath; if (isset($fileName)) { if (! in_array($fileExtension,$fileExtensions)) { $errors[] = "JPEG, JPG, PNG and GIF images are only supported"; } if (empty($errors)) { $didUpload = move_uploaded_file($fileTmpName, $uploadPath); if ($didUpload) { echo "The image " . basename($fileName) . " has been uploaded."; } else { echo "An error occurred while uploading. Try again."; } } else { foreach ($errors as $error) { echo $error . "The following error occured: " . "\n"; } } } //} } ?>
Code javascript : 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
45 var myForm = document.getElementById('formAjax'); // Our HTML form's ID var myFile = document.getElementById('fileAjax'); // Our HTML files' ID var statusP = document.getElementById('status'); myForm.onsubmit = function(event) { event.preventDefault(); statusP.innerHTML = 'Uploading...'; // Get the files from the form input var files = myFile.files; // Create a FormData object var formData = new FormData(); // Select only the first file from the input array var file = files[0]; // Check the file type if (!file.type.match('image.*')) { statusP.innerHTML = 'The file selected is not an image.'; return; } // Add the file to the AJAX request formData.append('fileAjax', file, file.name); // Set up the request var xhr = new XMLHttpRequest(); // Open the connection xhr.open('POST', '/uploadHandling.php', true); // Set up a handler for when the task for the request is complete xhr.onload = function () { if (xhr.status == 200) { statusP.innerHTML = 'Upload complete!'; } else { statusP.innerHTML = 'Upload error. Try again.'; } }; // Send the data. xhr.send(formData); }
j'ai essayé de contacter son concepteur, malheureusement sans réponse.
J'ai essayé de mettre toutes les autorisations du repertoire "./uploads/"
j'ai systématiquement le message "Upload error. Try again."
Si vous avez du courage et désirez m'aider : merci
Partager