[PHP] functie voor het uploaden van files

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • PluueeR
  • Registratie: December 2000
  • Laatst online: 07:01
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

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.

Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

In regel 5 van je tweede script roep je de functie aan met $file_upload als eerste argument. Wat staat daar in?

Eventueel kun je trouwens de superglobal $_FILES gebruiken, die sinds 4.1.2 in gebruik is (geloof ik). Daarmee kun je de 'global' aanroep eruit knikkeren. (r.3 script 1).

edit:
Verder ontgaat het me nog een beetje wat er niet goed gaat en wat je al hebt geprobeerd...

[ Voor 16% gewijzigd door drm op 21-02-2003 13:36 ]

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Acties:
  • 0 Henk 'm!

  • PluueeR
  • Registratie: December 2000
  • Laatst online: 07:01
Wanneer ik in de functie van 'uploaded_file ' 'file_upload' (zoals in het form wordt gebruikt) gebruik werkt het wel. Maar niet met $uploaded_file.


=====

Wat er niet goed gaat is dat $uploaded_file zoals die wordt gebruikt in mijn functie wel de waarde heeft van het tijdelijke bestand. Maar wanneer ik die gebruik in combinatie met de $HTTP_POST_FILES krijg ik dus geen output.

PHP:
1
echo $HTTP_POST_FILES['$uploaded_file']['type'];


Hier hoor ik dus het mime type mee te krijgen. En die output komt er niet uit.

[ Voor 129% gewijzigd door PluueeR op 21-02-2003 13:51 ]


Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

Moet dit
PHP:
1
fileupload($file_upload, "");
dan niet gewoon
PHP:
1
fileupload('file_upload', "");
zijn :?

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Acties:
  • 0 Henk 'm!

  • dusty
  • Registratie: Mei 2000
  • Laatst online: 15-09 18:24

dusty

Celebrate Life!

Probleem is simpel.

PHP:
1
$HTTP_POST_FILES['uploaded_file']['type']


Let op de : 'uploaded_file' , er bestaat geen 'uploaded_file'.

$ :P

Back In Black!
"Je moet haar alleen aan de ketting leggen" - MueR


Acties:
  • 0 Henk 'm!

Verwijderd

PHP:
1
$destination = $location + $filename;


Ik wist niet dat dit kon.

Acties:
  • 0 Henk 'm!

  • PluueeR
  • Registratie: December 2000
  • Laatst online: 07:01
Verwijderd schreef op 21 February 2003 @ 14:27:
PHP:
1
$destination = $location + $filename;


Ik wist niet dat dit kon.
Het kan ook niet :)

kom nog uit mijn basic tijd denk ik :)
Pagina: 1