Toon posts:

[php] JPG upload script

Pagina: 1
Acties:
  • 67 views sinds 30-01-2008

Verwijderd

Topicstarter
Ik ben bezig een site om te zetten. Ik heb hem gemaakt met register_globals op on, en nu heb ik die op off gezet, en werkt er niets meer. Dit ben ik dus aan het veranderen.

Nu heb ik een upload script om een jpg up te loaden. Alleen ik krijg hem met geen mogelijkheid aan de praat.
Ik moet volgens mij $_FILE gebruiken maar dan doet ie helemaal niets. Als ik $_REQUEST gebruik dan krijg ik altijd dezelfde melding dat er geen bestand is geselecteerd. Weet iemand hoe dit kan?. (ik ben maar een php newbie)

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
include "auth.php";

$USER=new user('',$_COOKIE['user_name'],$_COOKIE['id_hash'],'','','','');
if ($USER->islogged_in())
{
      include ("header-in.inc.php");
      $letter1 = substr($user_name,0,1);
      $letter2 = substr($user_name,1,1);
?>
    <form method="POST" action="<?php echo $PHP_SELF; ?>?upload=doupload"
    enctype="multipart/form-data">
    <p align="center">
    <input type=file name=file size=30><br>
    <br>
     <input type="SUBMIT" name="submit" value="Insert picture">
    </p>
    </form>
<?php
    switch($_REQUEST['upload'])
    {
        case "doupload":

        $ext = strrchr($file_name,'.');
        $extensions = array(".jpg");
        $limit_size = "100000";
        $image_max_width = "300";
        $image_max_height = "300";
        $size = GetImageSize($file_name);
        list($foo,$width,$bar,$height) = explode("\"",$size[3]);

        $endresult = "<font size=\"2\">File Was Uploaded</font>";

        if ($file_name == "")
        {
            $endresult = "<font size=\"2\" color=\"red\">No file selected</font>";
        }
        else
        {
            if(file_exists("c:/apache/htdocs/pic/$letter1/$letter2/$user_name.jpg"))
            {
                $endresult = "<font size=\"2\" color=\"red\">File Already Existed.
                Click REMOVE PICTURE first to add another picture</font>";
            }
            else
            {
                if ($limit_size < $file_size)
                {
                    $endresult = "<font size=\"2\" color=\"red\">File was to big (Max 100 KB)</font>";
                }
                else
                {
                    if ($width > $image_max_width)
                    {
                        $endresult = "<font size=\"2\" color=\"red\">
                        Your image should be no wider than 300 pixels</font>";
                    }
                    else
                    {
                        if ($height > $image_max_height)
                        {
                            $endresult = "<font size=\"2\" color=\"red\">>
                            Your image should be no higher than 300 pixels</font>";
                        }
                        else
                        {
                            if (!in_array($ext,$extensions) )
                            {
                                $endresult = "<font size=\"2\" color=\"red\">
                                File is wrong type</font>";
                            }
                            else
                            {
                                @copy($file, "c:/apache/htdocs/pic/$letter1/$letter2/$user_name$ext") or
                                $endresult = "<font size=\"2\">Couldn't Copy File To Server</font>";
                            }
                        }
                    }
                }
            }
        }

        echo '<CENTER>';
        echo $endresult;
        echo '<BR><BR>';
        echo '</CENTER>';
        break;

        case "remove":
        if (file_exists("c:/apache/htdocs/pic/$letter1/$letter2/$user_name.jpg"))
        {
            $filename = "c:/apache/htdocs/pic/$letter1/$letter2/$user_name.jpg"; unlink($filename);
        }
        break;
    }
}
else
{
    echo '<CENTER><FONT COLOR="red">Niet ingelogd</FONT></CENTER>';
}
?>

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
File upload handling werkt inderdaad heel anders als je register globals uit hebt staan.

De PHP manual zegt het volgende:
The Variables defined for uploaded files differs depending on the PHP version and configuration. The autoglobal $_FILES exists as of PHP 4.1.0 The $HTTP_POST_FILES array has existed since PHP 4.0.0. These arrays will contain all your uploaded file information. Using $_FILES is preferred. If the PHP directive register_globals is on, related variable names will also exist. register_globals defaults to off as of PHP » 4.2.0.

The contents of $_FILES from our example script is as follows. Note that this assumes the use of the file upload name userfile, as used in the example script above.

$_FILES['userfile']['name']
The original name of the file on the client machine.

$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif".

$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error']
The error code associated with this file upload. ['error'] was added in PHP 4.2.0

Staat er echt niets in de $_FILES array?

Edit:
Welke PHP versie draai je eigenlijk?

[ Voor 8% gewijzigd door pjonk op 19-01-2003 20:27 ]

It’s nice to be important but it’s more important to be nice


Verwijderd

Topicstarter
JonkieXL schreef op 19 January 2003 @ 20:26:
Edit:
Welke PHP versie draai je eigenlijk?
Ik draai php versie 4.3.0 :)

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Dan zou je met $_FILES moeten werken.
Doe anders eens een print_r($_FILES); in het begin van je script dan kan je kijken of er uberhaupt iets in de array staat.

Deze code komt ook rechtstreeks uit de php manual
PHP:
1
2
3
4
5
6
7
8
// In PHP earlier then 4.1.0, $HTTP_POST_FILES  should be used instead of $_FILES.
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   copy($_FILES['userfile']['tmp_name'], "/place/to/put/uploaded/file");
} else {
   echo "Possible file upload attack. Filename: " . $_FILES['userfile']['name'];
}
/* ...or... */
move_uploaded_file($_FILES['userfile']['tmp_name'], "/place/to/put/uploaded/file");


Ook raadt de PHP manual aan om move_uploaded_file te gebruiken:
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

Zie www.php.net/move_uploaded_file

[ Voor 72% gewijzigd door pjonk op 19-01-2003 20:41 ]

It’s nice to be important but it’s more important to be nice


  • nxt
  • Registratie: November 2001
  • Laatst online: 12-06 10:00

nxt

PHP:
1
@copy($file, "c:/apache/htdocs/pic/$letter1/$letter2/$user_name$ext") or


ik zie je nergens iets in de variabele $file stoppen?
ook kan het helpen om voor het debuggen de @ voor copy weg te halen.

maar probeer het eens met $_FILES['userfile']['tmp_name'] in plaats van $file

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
nxt schreef op 19 januari 2003 @ 20:40:
PHP:
1
@copy($file, "c:/apache/htdocs/pic/$letter1/$letter2/$user_name$ext") or


ik zie je nergens iets in de variabele $file stoppen?
ook kan het helpen om voor het debuggen de @ voor copy weg te halen.

maar probeer het eens met $_FILES['userfile']['tmp_name'] in plaats van $file
Weer refereer ik even aan de PHP manual:
When register_globals is turned on in php.ini, additional variables are available. For example, $userfile_name will equal $_FILES['userfile']['name'], $userfile_type will equal $_FILES['userfile']['type'], etc. Keep in mind that as of PHP 4.2.0, register_globals defaults to off. It's preferred to not rely on this directive.

Dus als je register globals op uit hebt staan moet je inderdaad met $_FILES gaan werken. Deze code is echter gemaakt voor register_globals is on.

It’s nice to be important but it’s more important to be nice


Verwijderd

Topicstarter
Als ik dit boven aan het script zet: print_r($_FILES)
komt er dit te staan:

Array ( [file] => Array ( [name] => site.gif [type] => image/gif [tmp_name] => C:\WINDOWS\php96.tmp [error] => 0 [size] => 44052 ) )

Er zit dus wel iets in, maar geeft een of andere vage fout :?

  • Genoil
  • Registratie: Maart 2000
  • Laatst online: 12-11-2023
vage fout? dit ziet er gezond uit hoor...
PHP:
1
copy($_FILES['file']['tmp_name'], $finaldestination)

Verwijderd

Ik wilde even doorgeven dat ik dit topic zojuist via de search gevonden heb.. maar het is een perfecte uitleg! Via de zoekmachine op PHP.net kom je nl. niet makkelijk bij de juiste pagina, maar op Tweakers wel.

  • crisp
  • Registratie: Februari 2000
  • Laatst online: 09:16

crisp

Devver

Pixelated

Verwijderd schreef op 15 april 2003 @ 08:11:
Ik wilde even doorgeven dat ik dit topic zojuist via de search gevonden heb.. maar het is een perfecte uitleg! Via de zoekmachine op PHP.net kom je nl. niet makkelijk bij de juiste pagina, maar op Tweakers wel.
linkje naar php.net ;)

Intentionally left blank


Verwijderd

Zelfde probleem dunkt me, heb van alles geprobeerd (ook met andere scripts) dus graag even pointers hoe ik dit kan oplossen. Zoals het er nu voor staat herkent de php script mijn input niet en krijg dus altijd een no file submitted ofzo.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?

//user defined variables
$abpath = "/Rottertours/upload"; //Absolute path to where images are uploaded. No trailing slash
$sizelim = "no"; //Do you want size limit, yes or no
$size = "2500000"; //What do you want size limited to be if there is one
$number_of_uploads = 5;  //Number of uploads to occur

if ($_POST['submitted']){ // Begin processing portion of script

//all image types to upload
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type
$cert4 = "image/ief"; //Ief type
$cert5 = "image/png"; //Png type
$cert6 = "image/tiff"; //Tiff type
$cert7 = "image/bmp"; //Bmp Type
$cert8 = "image/vnd.wap.wbmp"; //Wbmp type
$cert9 = "image/x-cmu-raster"; //Ras type
$cert10 = "image/x-x-portable-anymap"; //Pnm type
$cert11 = "image/x-portable-bitmap"; //Pbm type
$cert12 = "image/x-portable-graymap"; //Pgm type
$cert13 = "image/x-portable-pixmap"; //Ppm type
$cert14 = "image/x-rgb"; //Rgb type
$cert15 = "image/x-xbitmap"; //Xbm type
$cert16 = "image/x-xpixmap"; //Xpm type
$cert17 = "image/x-xwindowdump"; //Xwd type

$log = "";

for ($i=0; $i<$number_of_uploads; $i++) {

    //checks if file exists
    if ($img_name[$i] == "") {
        $log .= "No file selected for upload $i<br>";
    }

    if ($img_name[$i] != "") {
        //checks if file exists
        if (file_exists("$abpath/$img_name[$i]")) {
            $log .= "File $i already existed<br>";
        } else {

            //checks if files to big
            if (($sizelim == "yes") && ($img_size[$i] > $size)) {
                $log .= "File $i was too big<br>";
            } else {


                //Checks if file is an image
                if (($img_type[$i] == $cert1) or ($img_type[$i] == $cert2) or ($img_type[$i] == $cert3) or ($img_type[$i] == $cert4) or ($img_type[$i] == $cert5) or ($img_type[$i] == $cert6) or ($img_type[$i] == $cert7) or ($img_type[$i] == $cert8) or ($img_type[$i] == $cert9) or ($img_type[$i] == $cert10) or ($img_type[$i] == $cert11) or ($img_type[$i] == $cert12) or ($img_type[$i] == $cert13) or ($img_type[$i] == $cert14) or ($img_type[$i] == $cert15) or ($img_type[$i] == $cert16) or ($img_type[$i] == $cert17)) {
                    @copy($img[$i], "$abpath/$img_name[$i]") or $log .= "Couldn't copy image 1 to server<br>";
                    if (file_exists("$abpath/$img_name[$i]")) {
                        $log .= "File $i was uploaded<br>";
                    }
                    } else {
                        $log .= "File $i is not an image<br>";
                    }
                }
            }
        }


    }

?>

<html>
<head>
<title>Image Report</title>
</head>
<body>
<p>Log:<br>
<?

echo "$log";

?>
</p>
<body>
</html>
<? 
exit;
} // End processing portion of script
?>

<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form method=POST action=uploadmulti.php enctype=multipart/form-data>
<p>Files to upload:<br>
<? 

for ($j=0; $j<$number_of_uploads; $j++) {
?>
<input type=file name=img[] size=30><br>
<?
}
?>
<input type="hidden" name="submitted" value="true">
<input type="submit" name="submit" value="Upload"> 
</form>
</body>
</html>

  • dusty
  • Registratie: Mei 2000
  • Laatst online: 21-02 00:06

dusty

Celebrate Life!

Seiruu: Wij zijn geen debug forum, lees de FAQ eens.

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

Pagina: 1

Dit topic is gesloten.