Image uploaden en wegschrijven naar DB
Ik heb een vraag ivm uploading van meerdere foto's in een form. Momenteel werk ik met een simple form met daarin 1
Quote:
<form name="pictureform" action="uploadSchilderij.inc.php" method="post" enctype="multipart/form-data" target="upload_target" >
<input name="myfile" id="myfile" type="file" size="30"/>
<input name="myfile" id="myfile" type="file" size="30"/>
Daarna laat ik mijn script uitvoeren die de foto upload naar mijn server. En mijn gegevens in de mysql database wegschrijft ik doe dit op deze manier:
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
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
<?php
// keep our session
session_start();
include ('db_connect.inc.php');
include ('lib/WideImage.php');
// Check the upload file type
$filetype = $_FILES['myfile']['type'];
$gd_function_suffix = array(
'image/pjpeg' => 'jpeg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/bmp' => 'wbmp',
'image/png' => 'png',
'image/x-png' => 'png'
);
$function_suffix = $gd_function_suffix[$filetype];
// Build Function name for ImageCreateFromSUFFIX
if(!$function_suffix){
$err = "Unsupported file type, must be JPG|PNG|GIF|BMP";
}
else{
$function_to_read = 'ImageCreateFrom' . $function_suffix;
// Get the image and create a thumbnail
$img = $function_to_read($_FILES['myfile']['tmp_name']);
if (!$img) {
$err = "could not create image handle";
}
$width = imageSX($img); // Get the image width
$height = imageSY($img); // Get the image height
if (!$width || !$height) {
$err = "No Width or Height";
}
// Create php Session array for image information
if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img);
$imagevariable = ob_get_contents();
ob_end_clean();
// Create a unique id or name for the image
$file_id = md5($_FILES["myfile"]["tmp_name"] + rand()*100000);
$new_name = $file_id.".$function_suffix";
$temp_name = $_FILES["myfile"]["tmp_name"];
// assign upload directory, and target path for image
$uploaddir = "../img/schilderij/"; //getcwd();
$target_path = $uploaddir.DIRECTORY_SEPARATOR.$new_name;
// Move uploaded image to uploaddir location
if(!@move_uploaded_file($temp_name, $target_path)){
$err = "Couldn't Copy File to Filesystem";
}
// insert into DB
$schilderijId = $_POST['schilderij'];
$fotoAlbum = $_POST['fotoAlbum'];
if($fotoAlbum == 1)
{
mysql_query("UPDATE foto SET fotoAlbum = '0' WHERE schilderijId='".$schilderijId."' AND fotoAlbum ='1'");
$image = WideImage::load("../img/schilderij/".$new_name);
$resized = $image->resize(200, 200, 'outside');
$resized->saveToFile("../img/cache/".$file_id."Small.png");
}
$img = WideImage::load("../img/schilderij/".$new_name);
$rs = $img->resize('560', '340', 'inside', 'down');
$rs->saveToFile("../img/schilderijResized/".$new_name);
mysql_query("INSERT INTO foto (fotoNaam, schilderijId,fotoAlbum) VALUES ('".$new_name."', '".$schilderijId."', '".$fotoAlbum."')");
}
// Pass our $err varialbe along
if(!$err){
$result = "'1','".$new_name."'";
}
else{
$result = "'.$err.','".$file_id."'";
}
?>
// keep our session
session_start();
include ('db_connect.inc.php');
include ('lib/WideImage.php');
// Check the upload file type
$filetype = $_FILES['myfile']['type'];
$gd_function_suffix = array(
'image/pjpeg' => 'jpeg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/bmp' => 'wbmp',
'image/png' => 'png',
'image/x-png' => 'png'
);
$function_suffix = $gd_function_suffix[$filetype];
// Build Function name for ImageCreateFromSUFFIX
if(!$function_suffix){
$err = "Unsupported file type, must be JPG|PNG|GIF|BMP";
}
else{
$function_to_read = 'ImageCreateFrom' . $function_suffix;
// Get the image and create a thumbnail
$img = $function_to_read($_FILES['myfile']['tmp_name']);
if (!$img) {
$err = "could not create image handle";
}
$width = imageSX($img); // Get the image width
$height = imageSY($img); // Get the image height
if (!$width || !$height) {
$err = "No Width or Height";
}
// Create php Session array for image information
if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img);
$imagevariable = ob_get_contents();
ob_end_clean();
// Create a unique id or name for the image
$file_id = md5($_FILES["myfile"]["tmp_name"] + rand()*100000);
$new_name = $file_id.".$function_suffix";
$temp_name = $_FILES["myfile"]["tmp_name"];
// assign upload directory, and target path for image
$uploaddir = "../img/schilderij/"; //getcwd();
$target_path = $uploaddir.DIRECTORY_SEPARATOR.$new_name;
// Move uploaded image to uploaddir location
if(!@move_uploaded_file($temp_name, $target_path)){
$err = "Couldn't Copy File to Filesystem";
}
// insert into DB
$schilderijId = $_POST['schilderij'];
$fotoAlbum = $_POST['fotoAlbum'];
if($fotoAlbum == 1)
{
mysql_query("UPDATE foto SET fotoAlbum = '0' WHERE schilderijId='".$schilderijId."' AND fotoAlbum ='1'");
$image = WideImage::load("../img/schilderij/".$new_name);
$resized = $image->resize(200, 200, 'outside');
$resized->saveToFile("../img/cache/".$file_id."Small.png");
}
$img = WideImage::load("../img/schilderij/".$new_name);
$rs = $img->resize('560', '340', 'inside', 'down');
$rs->saveToFile("../img/schilderijResized/".$new_name);
mysql_query("INSERT INTO foto (fotoNaam, schilderijId,fotoAlbum) VALUES ('".$new_name."', '".$schilderijId."', '".$fotoAlbum."')");
}
// Pass our $err varialbe along
if(!$err){
$result = "'1','".$new_name."'";
}
else{
$result = "'.$err.','".$file_id."'";
}
?>
Nu is mijn vraag wat als ik meerdere foto's wil gaan toevoegen vanuit 1 form?
Hoe zouden jullie dit oplossen?
Alvast bedankt
Joachim!
http://www.uploadify.com/ ?
Misschien met Gewijzigd op 18/11/2010 08:24:26 door Obelix Idefix