Ik heb een fileupload script die ik al vaak gebruikt heb en dus goed werkt. Dit script wil ik nu in een functie wil gaan gebruiken. Alleen gaat er iets niet helemaal goed met mijn $HTTP_POST_FILES variable. Ik kan die namelijk wel uitlezen maar zodra ik er een element uit wil halen, bijv. mime type werkt dit niet.
Allereerst het bestand met de function: file_upload.php
En dan het bestand waarin de functie wordt aangeroepen: test.php
print_r laat gewoon de in houd van $HTTP_POST_FILES zien en het echo'n van $uploaded_file gaat ook goed.
Allereerst het bestand met de function: file_upload.php
PHP:
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
| <?php function fileupload($uploaded_file, $location){ global $HTTP_POST_FILES; print_r($HTTP_POST_FILES); echo $uploaded_file . "<br>"; // check if the var $file has been set // if (($file != '') && ($file != 'none')){ // create a timestamp for the filename $timestamp = mktime(); echo "mime type: " . $HTTP_POST_FILES['uploaded_file']['type']; // check the file for the mimetype to give the right extension if ($HTTP_POST_FILES['uploaded_file']['type'] == 'image/x-png'){ $filename = $timestamp . ".png"; }elseif ($HTTP_POST_FILES['uploaded_file']['type'] == 'image/pjpeg'){ $filename = $timestamp . ".jpg"; }elseif ($HTTP_POST_FILES['uploaded_file']['type'] == 'image/jpeg'){ $filename = $timestamp . ".jpg"; }elseif ($HTTP_POST_FILES['uploaded_file']['type'] == 'image/gif'){ $filename = $timestamp . ".gif"; } // placing the file from the tmp directory to the right location $destination = $location + $filename; echo $filename; move_uploaded_file($HTTP_POST_FILES['uploaded_file']['tmp_name'], "$destination"); // } } ?> |
En dan het bestand waarin de functie wordt aangeroepen: test.php
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <?php include ("file_upload.php"); if ($submit){ fileupload($file_upload, ""); } ?> <html> <head> </head> <body> <form enctype="multipart/form-data" method="post" action="test.php"> <input type="file" name="file_upload"> <input type="submit" name="submit" value="Submit"> </form> </body> </html> |
print_r laat gewoon de in houd van $HTTP_POST_FILES zien en het echo'n van $uploaded_file gaat ook goed.