foto upload script (aanpassing)
ik heb een scriptje geschreven op 4 bestanden te uploaden en die adh van een id een nieuwe map aanmaakt
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
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
<?
define ('MAX_FILE_SIZE', 512000); // we gegeven een max grootte
if (array_key_exists('upload', $_POST)) { // dit controleert of er een bestand is geupload als men op de submit knop heeft gedrukt
//(upload = name van submit button)
define('UPLOAD_DIR', '/Users/reyntjensw/Desktop/php oefn/uploads/upload_test/'); //zeg naar waar de file moet
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; //omzetten naar kb
$permitted = array('image/jpeg','image/pjpeg','image/png'); // maak een array van de toegestane formaten
foreach ($_FILES['image']['name'] as $number => $file) {
$file = str_replace(' ','',$file); //verwijder de spaties
$sizeOK = false; // beginnen met een false
$typeOK = false;
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
$username = $_GET['id']; //geef een naam op om een map aan te maken
if(!is_dir(UPLOAD_DIR.$username)) {
mkdir(UPLOAD_DIR.$username);
}
if(!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //verplaats en hernoem het bestand
} else {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //zorg er zeker voor dat je geen bestanden overschijft
}
if ($success) {
$result[] = "$file uploaded successfully";
}
else {
$result[] = "Error uploading $file. Please try again.";
}
break;
case 3:
$result[] = "Error uploading $file. Please try again.";
default:
$result[] = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'][$number] == 4) {
$result[] = 'No file selected';
}
else {
$result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo '<ol>';
foreach ($result as $item) {
echo "<strong><li>$item</li></strong>";
}
echo '</ol>';
}
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
<p>
<label for="image1">Bestand 1:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image[]" id="image1" />
</p>
<p>
<label for="image2">Bestand 2:</label>
<input type="file" name="image[]" id="image2" />
</p>
<p>
<label for="image3">Bestand 3:</label>
<input type="file" name="image[]" id="image3" />
</p>
<p>
<label for="image4">Bestand 4:</label>
<input type="file" name="image[]" id="image4" />
</p>
<p>
<input name="upload" type="submit" id="upload" value="Upload files" />
</p>
</form>
</body>
</html>
define ('MAX_FILE_SIZE', 512000); // we gegeven een max grootte
if (array_key_exists('upload', $_POST)) { // dit controleert of er een bestand is geupload als men op de submit knop heeft gedrukt
//(upload = name van submit button)
define('UPLOAD_DIR', '/Users/reyntjensw/Desktop/php oefn/uploads/upload_test/'); //zeg naar waar de file moet
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; //omzetten naar kb
$permitted = array('image/jpeg','image/pjpeg','image/png'); // maak een array van de toegestane formaten
foreach ($_FILES['image']['name'] as $number => $file) {
$file = str_replace(' ','',$file); //verwijder de spaties
$sizeOK = false; // beginnen met een false
$typeOK = false;
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
$username = $_GET['id']; //geef een naam op om een map aan te maken
if(!is_dir(UPLOAD_DIR.$username)) {
mkdir(UPLOAD_DIR.$username);
}
if(!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //verplaats en hernoem het bestand
} else {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //zorg er zeker voor dat je geen bestanden overschijft
}
if ($success) {
$result[] = "$file uploaded successfully";
}
else {
$result[] = "Error uploading $file. Please try again.";
}
break;
case 3:
$result[] = "Error uploading $file. Please try again.";
default:
$result[] = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'][$number] == 4) {
$result[] = 'No file selected';
}
else {
$result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo '<ol>';
foreach ($result as $item) {
echo "<strong><li>$item</li></strong>";
}
echo '</ol>';
}
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
<p>
<label for="image1">Bestand 1:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image[]" id="image1" />
</p>
<p>
<label for="image2">Bestand 2:</label>
<input type="file" name="image[]" id="image2" />
</p>
<p>
<label for="image3">Bestand 3:</label>
<input type="file" name="image[]" id="image3" />
</p>
<p>
<label for="image4">Bestand 4:</label>
<input type="file" name="image[]" id="image4" />
</p>
<p>
<input name="upload" type="submit" id="upload" value="Upload files" />
</p>
</form>
</body>
</html>
Maar ik zou graag de filenames veranderen, dus:
bestand 1 => 1.jpg
bestand 2 => 2.jpg
bestand 3 => 3.jpg
bestand 4 => 4.jpg
Hoe doe ik dit het best met dit script?
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
if(!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //verplaats en hernoem het bestand
} else {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //zorg er zeker voor dat je geen bestanden overschijft
}
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //verplaats en hernoem het bestand
} else {
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number],UPLOAD_DIR.$username.'/'.$now.$file); //zorg er zeker voor dat je geen bestanden overschijft
}