Image upload script met voorwaarden
Ben een NooB met PHP.
Heb overal gezocht, ook hier, maar niet gevonden.
Wat ik zoek:
Een uploadscript (via ftp, explorer look a like) voor images met de volgende voorwaarden:
1) Images mogen niet groter dan 500 kB zijn
2) Maximaal te uploaden images is 1
3) Webruimte gebruik (per map) is dus ongeveer 500 kB (controle hierop ivm onjuist gebruik webruimte)
4) De image die geupload wordt gaat image1.jpg heten (of png/gif)
5) Indien er al een image1 bestaat in de map wordt deze overschreven
6) Mogelijkheid om de image te tonen (mag een apparte html-pagina zijn)
7) Image mag extentie jpg, png of gif hebben
8) Indien de map geen image bevat dient er een standaard plaatje, b.v. leeg.png, getoond te worden
Uitleg waarom deze vraag:
Ik heb een website waar leden kunnen inloggen (een gekochte applet die dit regeld) op een eigen pagina. Nu wil ik dat zij op deze pagina 1 plaatje met eventueel een tekst kunnen plaatsen (en het plaatje ook kunnen wijzigen).
Zoals al gezegd heb ik overal al rond gekeken maar (nog) niet kunnen vinden wat hieraan voldoet.
Hoop dat jullie mij kunnen helpen.
Groet en alvast dank.
Dirk.
Gewijzigd op 01/01/1970 01:00:00 door Dirk
Behalve punt 1 is het geen probleem, wat bedoel je precies met 'ftp, explorer look a like'?
Dit dus? Ook belangrijk om te vermelden, waar haal je de informatie vandaan mbt de gebruiker? Sessie? Kan je nog iets controlleren in een database ofzo :).
Gewijzigd op 01/01/1970 01:00:00 door Arjan Kapteijn
Link: http://www.coffeecup.com/password-wizard/help/applet-password-wizard-layout.225.html alleen ziet het er nu anders uit dan jaren terug.
Kan ik dit scriptje eens zelf op mijn website zetten en proberen?
Wanneer en waarom komt deze melding "Sorry, dit type accepteer ik niet!"?
Dirk.
Gewijzigd op 01/01/1970 01:00:00 door Dirk
Gewijzigd op 01/01/1970 01:00:00 door Arjan Kapteijn
Dirk.
Ik gebruik nu hetvolgende 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
error_reporting(E_ALL);
/**********************************************************************************
* Multifile files upload at once with a form,
* optional image resize for jpeg gif and png during upload
* Author: scripts[AT ][ .]titude.nl - NLD - 2007
* For use with resize Requires GD ( 2.0.28 or up for gif support )
# example .htaccess
php_value max_execution_time = 3600
php_value max_input_time = 3600
php_value post_max_size 100Kb
php_value upload_max_filesize 100Kb
**********************************************************************************/
$m_cnfg['max_size'] = 102400; // size per file in bytes - 0 is unlimited
$m_cnfg['max_files'] = 1; // max files to upload - 0 is unlimited
$m_cnfg['ext_oke'] = array('png','jpg','gif');
$m_cnfg['upload_dir'] = $_SERVER['DOCUMENT_ROOT'] . "Steenenkamer/Uploads/uploaddir"; // upload dir
$m_cnfg['overwrite'] = true; // allow files overwrite on true
$m_cnfg['newname'] = "test"; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['imggd'] = array('jpeg', 'jpg', 'gif', 'png'); // resize with GD
$m_cnfg['imgw'] = 448; // max width for resize - 0 is no resize
$m_cnfg['imgh'] = 336; // max height for resize - 0 is no resize
$m_log = multiup($m_cnfg);
function multiup($m_cnfg) {
$upmsg = array();
if ($m_cnfg['max_size'] != 0) {
$maxdisp = ($m_cnfg['max_size'] >= 1024000) ? round($m_cnfg['max_size'] / 10240) / 100 . " Kb" : round($m_cnfg['max_size'] / 10.24) / 100 . " Kb";
// $upmsg['rules'][] = "Max file size: " . $maxdisp;
}
if (!empty($m_cnfg['ext_oke'])) {
$upmsg['rules'][] = "File types: " . implode(" ,", $m_cnfg['ext_oke']);
}
if ($m_cnfg['max_files'] != 0) {
// $upmsg['rules'][] = "Simultanious: " . $m_cnfg['max_files'];
}
if ($_SERVER["REQUEST_METHOD"] == 'POST' && !empty($_FILES[$m_cnfg['inputname']]['name'])) {
if (is_writable($m_cnfg['upload_dir'])) {
$filedir = str_replace("//", "/", $m_cnfg['upload_dir'] . "/");
$gdtrue = extension_loaded('gd') ? true : false;
$gd201 = function_exists('imagecopyresampled') ? true : false;
$gd2082 = $gd201 && function_exists('imagecreatefromgif') ? true : false;
$maxup = 1;
while(list($filenum, $filename) = each($_FILES[$m_cnfg['inputname']]['name'])) {
$uptrue = false;
if (!empty($filename)) {
$fileparts = pathinfo($filename);
$fileext = strtolower($fileparts['extension']);
// filter filename - adjust to needs - replace spaces by
// _ a-z A-Z - 0-9 _ . allowed - extension length 2-5 chars
$filename = str_replace("%20", "_", str_replace(" ", "_", $filename));
$uptrue = preg_match("/^[-a-z0-9_\.]+\.[a-z0-9]{2,5}$/i", $filename) ? true : false;
$uptrue = $uptrue && $m_cnfg['max_files'] != 0 && $m_cnfg['max_files'] < $maxup ? false : true;
$uptrue = $uptrue && (($fileext != "" && in_array($fileext, $m_cnfg['ext_oke'])) || empty($m_cnfg['ext_oke'])) ? true : false;
$uptrue = $uptrue && ($_FILES['bestanden']['size'][$filenum] < $m_cnfg['max_size'] || $m_cnfg['max_size'] == 0) ? true : false;
if (!$uptrue) {
$upmsg['errors'][] = $filename;
}
}
if ($uptrue) {
$maxup++;
$newname = "";
if ($m_cnfg['overwrite'] !== true || $m_cnfg['newname'] != "") {
$tmpname = $m_cnfg['newname'] != "" ? $m_cnfg['newname'] : str_replace ("." . $fileext, "", $filename);
if ($m_cnfg['newname'] == "timestamp") {
list($usec) = explode(" ", microtime());
$tmpname = date('YmdHis') . (string)substr($usec,2,6);
}
$fileloc = $filedir . $tmpname . "." . $fileext;
$newname = $tmpname . "." . $fileext;
$a = 1;
if ($m_cnfg['overwrite'] !== true) {
while (file_exists($fileloc)) {
$a++;
$fileloc = $filedir . $tmpname . "_" . $a . "." . $fileext;
$newname = $tmpname . "_" . $a . "." . $fileext;
}
}
} else {
$fileloc = $filedir . $filename;
}
if (move_uploaded_file($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum], $fileloc)) {
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename;
} else {
$upmsg['uploads'][] = $filename;
}
if ($gdtrue && in_array($fileext, $m_cnfg['imggd']) && $m_cnfg['imgw'] > 0 && $m_cnfg['imgh'] > 0) {
list($curw, $curh) = getimagesize($fileloc);
$p_x = $curw / $m_cnfg['imgw'];
$p_y = $curh / $m_cnfg['imgh'];
if ($p_x > 1 || $p_y > 1) {
$pfactor = ($p_x > $p_y) ? $pfactor = $p_x : $pfactor = $p_y;
$neww = round($curw / $pfactor);
$newh = round($curh / $pfactor);
$imgorg = false;
if ($fileext == "jpg" || $fileext =='jpeg') {
$imgorg = imagecreatefromjpeg($fileloc);
} else if ($gd2082 && $fileext == "gif") {
$imgorg = imagecreatefromgif($fileloc);
} else if ($fileext == 'png') {
$imgorg = imagecreatefrompng($fileloc);
}
if ($imgorg) {
if ($gd201) {
$imgnew = imagecreatetruecolor($neww, $newh);
if ($fileext == "gif" || $fileext == "png") {
$bgcolor = imagecolorallocate($imgorg, 0, 0, 0);
imagepalettecopy($imgnew, $imgorg);
imagefill($imgnew, 0, 0, $bgcolor);
imagecolortransparent($imgnew, $bgcolor);
}
imagecopyresampled($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
} else {
$imgnew = imagecreate($neww, $newh);
imagecopy($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
}
imagedestroy($imgorg);
if ($fileext == 'jpg' || $fileext =='jpeg') {
imagejpeg($imgnew, $fileloc, 80);
} else if ($gd2082 && $fileext == 'gif') {
imagegif($imgnew, $fileloc);
} else if ($fileext == 'png') {
imagepng($imgnew, $fileloc);
}
imagedestroy($imgnew);
list($neww, $newh) = getimagesize($fileloc);
array_pop($upmsg['uploads']);
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename . " - size: " . $neww . "px - " . $newh . "px";
} else {
$upmsg['uploads'][] = $filename . " as " . $newname . " - size: " . $neww . "px - " . $newh . "px";
}
}
}
}
}
}
if (file_exists($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum])) {
unlink($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum]);
}
}
} else {
$upmsg['errors'][] = "Dir error: " . $m_cnfg['upload_dir'];
}
}
return $upmsg;
}
?>
error_reporting(E_ALL);
/**********************************************************************************
* Multifile files upload at once with a form,
* optional image resize for jpeg gif and png during upload
* Author: scripts[AT ][ .]titude.nl - NLD - 2007
* For use with resize Requires GD ( 2.0.28 or up for gif support )
# example .htaccess
php_value max_execution_time = 3600
php_value max_input_time = 3600
php_value post_max_size 100Kb
php_value upload_max_filesize 100Kb
**********************************************************************************/
$m_cnfg['max_size'] = 102400; // size per file in bytes - 0 is unlimited
$m_cnfg['max_files'] = 1; // max files to upload - 0 is unlimited
$m_cnfg['ext_oke'] = array('png','jpg','gif');
$m_cnfg['upload_dir'] = $_SERVER['DOCUMENT_ROOT'] . "Steenenkamer/Uploads/uploaddir"; // upload dir
$m_cnfg['overwrite'] = true; // allow files overwrite on true
$m_cnfg['newname'] = "test"; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['imggd'] = array('jpeg', 'jpg', 'gif', 'png'); // resize with GD
$m_cnfg['imgw'] = 448; // max width for resize - 0 is no resize
$m_cnfg['imgh'] = 336; // max height for resize - 0 is no resize
$m_log = multiup($m_cnfg);
function multiup($m_cnfg) {
$upmsg = array();
if ($m_cnfg['max_size'] != 0) {
$maxdisp = ($m_cnfg['max_size'] >= 1024000) ? round($m_cnfg['max_size'] / 10240) / 100 . " Kb" : round($m_cnfg['max_size'] / 10.24) / 100 . " Kb";
// $upmsg['rules'][] = "Max file size: " . $maxdisp;
}
if (!empty($m_cnfg['ext_oke'])) {
$upmsg['rules'][] = "File types: " . implode(" ,", $m_cnfg['ext_oke']);
}
if ($m_cnfg['max_files'] != 0) {
// $upmsg['rules'][] = "Simultanious: " . $m_cnfg['max_files'];
}
if ($_SERVER["REQUEST_METHOD"] == 'POST' && !empty($_FILES[$m_cnfg['inputname']]['name'])) {
if (is_writable($m_cnfg['upload_dir'])) {
$filedir = str_replace("//", "/", $m_cnfg['upload_dir'] . "/");
$gdtrue = extension_loaded('gd') ? true : false;
$gd201 = function_exists('imagecopyresampled') ? true : false;
$gd2082 = $gd201 && function_exists('imagecreatefromgif') ? true : false;
$maxup = 1;
while(list($filenum, $filename) = each($_FILES[$m_cnfg['inputname']]['name'])) {
$uptrue = false;
if (!empty($filename)) {
$fileparts = pathinfo($filename);
$fileext = strtolower($fileparts['extension']);
// filter filename - adjust to needs - replace spaces by
// _ a-z A-Z - 0-9 _ . allowed - extension length 2-5 chars
$filename = str_replace("%20", "_", str_replace(" ", "_", $filename));
$uptrue = preg_match("/^[-a-z0-9_\.]+\.[a-z0-9]{2,5}$/i", $filename) ? true : false;
$uptrue = $uptrue && $m_cnfg['max_files'] != 0 && $m_cnfg['max_files'] < $maxup ? false : true;
$uptrue = $uptrue && (($fileext != "" && in_array($fileext, $m_cnfg['ext_oke'])) || empty($m_cnfg['ext_oke'])) ? true : false;
$uptrue = $uptrue && ($_FILES['bestanden']['size'][$filenum] < $m_cnfg['max_size'] || $m_cnfg['max_size'] == 0) ? true : false;
if (!$uptrue) {
$upmsg['errors'][] = $filename;
}
}
if ($uptrue) {
$maxup++;
$newname = "";
if ($m_cnfg['overwrite'] !== true || $m_cnfg['newname'] != "") {
$tmpname = $m_cnfg['newname'] != "" ? $m_cnfg['newname'] : str_replace ("." . $fileext, "", $filename);
if ($m_cnfg['newname'] == "timestamp") {
list($usec) = explode(" ", microtime());
$tmpname = date('YmdHis') . (string)substr($usec,2,6);
}
$fileloc = $filedir . $tmpname . "." . $fileext;
$newname = $tmpname . "." . $fileext;
$a = 1;
if ($m_cnfg['overwrite'] !== true) {
while (file_exists($fileloc)) {
$a++;
$fileloc = $filedir . $tmpname . "_" . $a . "." . $fileext;
$newname = $tmpname . "_" . $a . "." . $fileext;
}
}
} else {
$fileloc = $filedir . $filename;
}
if (move_uploaded_file($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum], $fileloc)) {
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename;
} else {
$upmsg['uploads'][] = $filename;
}
if ($gdtrue && in_array($fileext, $m_cnfg['imggd']) && $m_cnfg['imgw'] > 0 && $m_cnfg['imgh'] > 0) {
list($curw, $curh) = getimagesize($fileloc);
$p_x = $curw / $m_cnfg['imgw'];
$p_y = $curh / $m_cnfg['imgh'];
if ($p_x > 1 || $p_y > 1) {
$pfactor = ($p_x > $p_y) ? $pfactor = $p_x : $pfactor = $p_y;
$neww = round($curw / $pfactor);
$newh = round($curh / $pfactor);
$imgorg = false;
if ($fileext == "jpg" || $fileext =='jpeg') {
$imgorg = imagecreatefromjpeg($fileloc);
} else if ($gd2082 && $fileext == "gif") {
$imgorg = imagecreatefromgif($fileloc);
} else if ($fileext == 'png') {
$imgorg = imagecreatefrompng($fileloc);
}
if ($imgorg) {
if ($gd201) {
$imgnew = imagecreatetruecolor($neww, $newh);
if ($fileext == "gif" || $fileext == "png") {
$bgcolor = imagecolorallocate($imgorg, 0, 0, 0);
imagepalettecopy($imgnew, $imgorg);
imagefill($imgnew, 0, 0, $bgcolor);
imagecolortransparent($imgnew, $bgcolor);
}
imagecopyresampled($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
} else {
$imgnew = imagecreate($neww, $newh);
imagecopy($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
}
imagedestroy($imgorg);
if ($fileext == 'jpg' || $fileext =='jpeg') {
imagejpeg($imgnew, $fileloc, 80);
} else if ($gd2082 && $fileext == 'gif') {
imagegif($imgnew, $fileloc);
} else if ($fileext == 'png') {
imagepng($imgnew, $fileloc);
}
imagedestroy($imgnew);
list($neww, $newh) = getimagesize($fileloc);
array_pop($upmsg['uploads']);
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename . " - size: " . $neww . "px - " . $newh . "px";
} else {
$upmsg['uploads'][] = $filename . " as " . $newname . " - size: " . $neww . "px - " . $newh . "px";
}
}
}
}
}
}
if (file_exists($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum])) {
unlink($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum]);
}
}
} else {
$upmsg['errors'][] = "Dir error: " . $m_cnfg['upload_dir'];
}
}
return $upmsg;
}
?>
<html>
<head>
<style>
body {
font-family: arial;
font-size: 12px;
margin-left: 50px;
margin-top: 50px;
}
.mijnTekstVeld {
font-family:Arial;
background:"#e7cea5";
width:400px;
height:20px;
font-size:11pt;
border-style:solid;
border-width:1;
border-color:black;
color:#000000;
}
.mijnKnop {
background:"#e7cea5";
height:20px;
border-style:solid;
border-width:1;
border-color:black;
font-size:11pt;
font-weight:normal;
color:#000000;
}
</style>
</head>
<body>
<script type="text/javascript">
<!--
var max_files = ;
var count_files = 1;
function bestanden() {
if (max_files == 0 || max_files > count_files) {
var holder = document.getElementById('bestanden').getElementsByTagName('tr')[0].parentNode;
var taginp = document.createElement('input');
taginp.setAttribute('type','file');
taginp.setAttribute('name','bestanden[]');
var tagtr = document.createElement('tr');
var tagtd = document.createElement('td');
var tagspan = document.createElement('span');
tagspan.style.cursor = 'pointer';
tagspan.onclick = function() {
this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
count_files--;
}
tagspan.appendChild(document.createTextNode(' Delete'));
tagtd.appendChild(taginp);
tagtd.appendChild(tagspan);
tagtr.appendChild(tagtd);
holder.appendChild(tagtr);
count_files++;
}
}
//-->
</script>
<form method="POST" action="" enctype="multipart/form-data">
<table id="bestanden">
<tr>
<td><input type="file" size=50 class=mijnTekstVeld name="bestanden[]"><span onclick="bestanden()" style="cursor:pointer;"></span></td>
</tr>
</table>
<input type="submit" class=mijnKnop value="Upload">
</form>
Code (php)
</body>
</html>
Dit genereerd 3 plaatjes met de extentie png, gif of jpg en noemt deze test.
Hoe kan ik nu het laatst geuploadde plaatje tonen op mijn website (html-pagina) ?
Dirk.
Ook ik gebruik onderstaande script welke ook te vinden is via:
//bron: http://www.phpfreakz.com/library.php?sid=25136
Echter heb ik wel een vraagje want in onderstaande regels schrijft de maker set a rename prefix dit heb ik geprobeerd maar krijg ik niet voorelkaar.
Quote:
$m_cnfg['newname'] = ""; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['inputname'] = "bestanden"; // input file form array name
Wat ik wel is een bestand beginnen met de gebruikers_id dus als deze 123 is en hij zal uploaden het bestand 'muziek.jpg moet dit '123-muziek.jpg' worden.
Dan weet ik altijd wie welk bestand heeft geupload en krijg je eigenlijk ook nooit dubbele bestanden..
Kan iemand mij hiermee helpen..
thanx
Jan