upload image
Omdat ik niet zo vindingrijk ben, maar toch op zoek ben naar een script om afbeeldingen up te loaden, heb ik een scriptje van het web geplukt. Nu komt het natuurlijk: het werkt niet echt bij mij... De foto wordt niet opgeladen of getoond.
Dit zijn de errors:
Notice: Undefined variable: imgfile in e:\programma's\easy php\www\upload\index.php on line 97
Notice: Undefined variable: imgfile in e:\programma's\easy php\www\upload\index.php on line 132
Notice: Undefined variable: imgfile in e:\programma's\easy php\www\upload\index.php on line 146
Warning: unlink(): Permission denied in e:\programma's\easy php\www\upload\index.php on line 146
... En dit script gebruik ik:
Code (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
<html>
<head>
<title>Upload and Resize an Image</title>
[code]<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
/* SUBMITTED INFORMATION - use what you need
* temporary filename (pointer): $imgfile
* original filename : $imgfile_name
* size of uploaded file : $imgfile_size
* mime-type of uploaded file : $imgfile_type
*/
/*== upload directory where the file will be stored
relative to where script is run ==*/
$uploaddir = ".";
$imgfile_name= @$_POST['imgfile'];
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 250) || ($imgsize[1] > 200))
{
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpimg = tempnam("/tmp", "MKUP");
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
/*== remove temp image ==*/
unlink($tmpimg);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
print("<img src=\"$final_filename\">");
/*== DO WHATEVER ELSE YOU WANT
SUCH AS INSERT DATA INTO A DATABASE ==*/
}
?>
</head>
<body bgcolor="#FFFFFF">
<h2>Upload and Resize an Image</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
</body>
</html>
<?php
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
?>
Wat loopt er fout?
<html>
<head>
<title>Upload and Resize an Image</title>
[code]<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
/* SUBMITTED INFORMATION - use what you need
* temporary filename (pointer): $imgfile
* original filename : $imgfile_name
* size of uploaded file : $imgfile_size
* mime-type of uploaded file : $imgfile_type
*/
/*== upload directory where the file will be stored
relative to where script is run ==*/
$uploaddir = ".";
$imgfile_name= @$_POST['imgfile'];
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 250) || ($imgsize[1] > 200))
{
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpimg = tempnam("/tmp", "MKUP");
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
/*== remove temp image ==*/
unlink($tmpimg);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
print("<img src=\"$final_filename\">");
/*== DO WHATEVER ELSE YOU WANT
SUCH AS INSERT DATA INTO A DATABASE ==*/
}
?>
</head>
<body bgcolor="#FFFFFF">
<h2>Upload and Resize an Image</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
</body>
</html>
<?php
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
?>
Wat loopt er fout?
EDIT:
Het bestand dat je probeert te verwijderen is geuploaded dat PHP er niet aan mag komen ;) Try a CHMod.
Gewijzigd op 01/01/1970 01:00:00 door Jeroen Langenberg
Stuur mij morgenavond even een berichtje, zal ik even wat maken.
Kan ik de grote dan opslaan als $naam-groot
($naam = date("dmYHis"). $x;)
Hier is het complete, werkende script
Code (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
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
<?
set_time_limit(120);
//kijken of er een bestand is geselecteerd
if(isset($_FILES['bestand'])) {
//Rootmap van de images
@$album = $_GET["album"];
$root = "images/$album/";
//kijken of het bestand wel jpg is
if($_FILES['bestand']['type'] == "image/pjpeg" || $_FILES['bestand']['type'] == "image/jpeg") {
//kijken of er een nieuwe naam aan het bestand is gegeven
//strip de extensie om die achter de nieuwe bestandsnaam te plakken
$x = strrchr($_FILES['bestand']['name'], ".");
$naam = date("dmYHis"). $x;
//het bestand uploaden met de nieuwe of oude naam
move_uploaded_file($_FILES['bestand']['tmp_name'], "$root" . $naam);
//voor linux of unix hosts chmodden we het bestand naar 777 zodat iedereen um kan zien
chmod("$root" . $naam, 0777);
//Resize van image
$image = "$root$naam";
$size = getimagesize($image);
$maxsize = 200;
$nul = $maxsize / $size[0];
$one = $maxsize / $size[1];
if ($nul < $one){
$width = $size[0] * $nul;
$height = $size[1] * $nul;
}
elseif ($size[1] > 200){
$width = $size[0] * $one;
$height = $size[1] * $one;
} ;
$img = imagecreatefromjpeg($image);
$imh = imagecreatetruecolor($width, $height);
imagecopyresampled($imh,$img,0,0,0,0,$width,$height,$size[0],$size[1]);
imagejpeg($imh,$image,100);
echo "Geupload! <img src=\"images/$naam\">";
} else {
echo "<span class=\"style2\">Het bestand is geen jpg</span>";
}
} else {
echo "<span class=\"style2\">Kies een afbeelding</span>";
}
?>
set_time_limit(120);
//kijken of er een bestand is geselecteerd
if(isset($_FILES['bestand'])) {
//Rootmap van de images
@$album = $_GET["album"];
$root = "images/$album/";
//kijken of het bestand wel jpg is
if($_FILES['bestand']['type'] == "image/pjpeg" || $_FILES['bestand']['type'] == "image/jpeg") {
//kijken of er een nieuwe naam aan het bestand is gegeven
//strip de extensie om die achter de nieuwe bestandsnaam te plakken
$x = strrchr($_FILES['bestand']['name'], ".");
$naam = date("dmYHis"). $x;
//het bestand uploaden met de nieuwe of oude naam
move_uploaded_file($_FILES['bestand']['tmp_name'], "$root" . $naam);
//voor linux of unix hosts chmodden we het bestand naar 777 zodat iedereen um kan zien
chmod("$root" . $naam, 0777);
//Resize van image
$image = "$root$naam";
$size = getimagesize($image);
$maxsize = 200;
$nul = $maxsize / $size[0];
$one = $maxsize / $size[1];
if ($nul < $one){
$width = $size[0] * $nul;
$height = $size[1] * $nul;
}
elseif ($size[1] > 200){
$width = $size[0] * $one;
$height = $size[1] * $one;
} ;
$img = imagecreatefromjpeg($image);
$imh = imagecreatetruecolor($width, $height);
imagecopyresampled($imh,$img,0,0,0,0,$width,$height,$size[0],$size[1]);
imagejpeg($imh,$image,100);
echo "Geupload! <img src=\"images/$naam\">";
} else {
echo "<span class=\"style2\">Het bestand is geen jpg</span>";
}
} else {
echo "<span class=\"style2\">Kies een afbeelding</span>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<span class="style2">Bestand:</span> <input type="file" name="bestand"><br>
<input type="submit" name="submit" value="Upload">
</form>