product aanmaken met meerdere fotos
addproduct.php
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
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
if (isset($_POST['submit'])) {
if ($product) {
$product->product_name = $_POST['product_name'];
$product->caption = $_POST['caption'];
$product->description = $_POST['description'];
$product->prijs = $_POST['prijs'];
$product->category_id = $_POST['category_id'];
$product->EAN = $_POST['EAN'];
$product->release_date = $_POST['release_date'];
$product->publisher = $_POST['publisher'];
$product->dev = $_POST['developer'];
$product->save();
$image_array = ($_FILES['images']);
for ($i=0;$i<count($image_array);$i++){
$new_image->image_name = $_FILES['images']['name'[$i]];
$new_image->type = $_FILES['type'[$i]];
$new_image->tmp_name = $_FILES['tmp_name'[$i]];
$new_image->size = $_FILES['size'[$i]];
//create new product_image
$new_image->set_file_product($_FILES['images']);
$new_image->save_product_and_image();
}
}
}
if ($product) {
$product->product_name = $_POST['product_name'];
$product->caption = $_POST['caption'];
$product->description = $_POST['description'];
$product->prijs = $_POST['prijs'];
$product->category_id = $_POST['category_id'];
$product->EAN = $_POST['EAN'];
$product->release_date = $_POST['release_date'];
$product->publisher = $_POST['publisher'];
$product->dev = $_POST['developer'];
$product->save();
$image_array = ($_FILES['images']);
for ($i=0;$i<count($image_array);$i++){
$new_image->image_name = $_FILES['images']['name'[$i]];
$new_image->type = $_FILES['type'[$i]];
$new_image->tmp_name = $_FILES['tmp_name'[$i]];
$new_image->size = $_FILES['size'[$i]];
//create new product_image
$new_image->set_file_product($_FILES['images']);
$new_image->save_product_and_image();
}
}
}
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
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
form
<form method="POST" data-parsley-validate novalidate enctype="multipart/form-data">
<div class="form-group">
<label for="product_name">Product Name*</label>
<input type="text" name="product_name" parsley-trigger="change" required placeholder="Enter product name" class="form-control" >
</div>
<div class="form-group">
<label for="caption">Caption*</label>
<input type="text" name="caption" parsley-trigger="change" required placeholder="Enter caption" class="form-control" >
</div>
<div class="form-group">
<label for="description">description*</label>
<input type="text" name="description" parsley-trigger="change" required placeholder="description" class="form-control" >
</div>
<div class="form-group">
<label for="prijs">prijs*</label>
<input type="text" name="prijs" parsley-trigger="change" required placeholder="Enter prijs" class="form-control" >
</div>
<h5 class="m-t-30"><b>Enter category*</b></h5>
<select class="select2 select2-multiple" name="category_id" multiple="multiple" multiple data-placeholder="Choose ...">
<optgroup label="categories">
<?php $categories = Categorie::find_all();
foreach ($categories as $category) : ?>
<option value="<?= $category->category_id; ?>"><?= $category->category_name; ?></option>
<?php endforeach; ?>
</optgroup>
</select>
<div class="form-group">
<label for="EAN">EAN</label>
<input type="text" name="EAN" class="form-control">
</div>
<div class="form-group">
<label for="release_date">release_date</label>
<input type="date" name="release_date" class="form-control">
</div>
<div class="form-group">
<label for="publisher">Publisher</label>
<input type="text" name="publisher" class="form-control">
</div>
<div class="form-group">
<label for="developer">developer</label>
<input type="text" name="developer" class="form-control">
</div>
<div class="form-group">
<label for="images[]">add images</label>
<input type="file" name="images[]" multiple class="form-control">
</div>
<div class="form-group text-right m-b-0">
<input type="submit" name="submit" value="Add Product" class="btn btn-primary">
</div>
</form>
<form method="POST" data-parsley-validate novalidate enctype="multipart/form-data">
<div class="form-group">
<label for="product_name">Product Name*</label>
<input type="text" name="product_name" parsley-trigger="change" required placeholder="Enter product name" class="form-control" >
</div>
<div class="form-group">
<label for="caption">Caption*</label>
<input type="text" name="caption" parsley-trigger="change" required placeholder="Enter caption" class="form-control" >
</div>
<div class="form-group">
<label for="description">description*</label>
<input type="text" name="description" parsley-trigger="change" required placeholder="description" class="form-control" >
</div>
<div class="form-group">
<label for="prijs">prijs*</label>
<input type="text" name="prijs" parsley-trigger="change" required placeholder="Enter prijs" class="form-control" >
</div>
<h5 class="m-t-30"><b>Enter category*</b></h5>
<select class="select2 select2-multiple" name="category_id" multiple="multiple" multiple data-placeholder="Choose ...">
<optgroup label="categories">
<?php $categories = Categorie::find_all();
foreach ($categories as $category) : ?>
<option value="<?= $category->category_id; ?>"><?= $category->category_name; ?></option>
<?php endforeach; ?>
</optgroup>
</select>
<div class="form-group">
<label for="EAN">EAN</label>
<input type="text" name="EAN" class="form-control">
</div>
<div class="form-group">
<label for="release_date">release_date</label>
<input type="date" name="release_date" class="form-control">
</div>
<div class="form-group">
<label for="publisher">Publisher</label>
<input type="text" name="publisher" class="form-control">
</div>
<div class="form-group">
<label for="developer">developer</label>
<input type="text" name="developer" class="form-control">
</div>
<div class="form-group">
<label for="images[]">add images</label>
<input type="file" name="images[]" multiple class="form-control">
</div>
<div class="form-group text-right m-b-0">
<input type="submit" name="submit" value="Add Product" class="btn btn-primary">
</div>
</form>
functies
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
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
public function set_file_product($file){
if(empty($file) || !$file || !is_array($file)){
$this->errors[]= "No file uploaded";
return false;
}elseif($file['error'] !=0){
$this->errors[] = $this->upload_errors_array[$file['error']];
return false;
}else{
$this->product_image = basename($file['name']);
$this->tmp_path = $file['tmp_name'];
$this->type = $file['type'];
$this->size = $file['size'];
}
}
public function save_product_and_image(){
if($this->id){
move_uploaded_file($this->tmp_path);
$this->update();
unset($this->tmp_path);
return true;
}else {
if (!empty($this->errors)) {
return false;
}
if (empty($this->product_image) || empty($this->tmp_path)) {
$this->errors[] = "File not available";
return false;
}
$target_path = SITE_ROOT . DS . "admin" . DS . $this->upload_directory . DS . $this->product_image;
if (file_exists($target_path)) {
$this->update();
return true;
}
if (move_uploaded_file($this->tmp_path, $target_path)) {
if ($this->create()) {
unset($this->tmp_path);
return true;
}
} else {
$this->errors[] = "This folder has no write rights";
return false;
}
}
}
public function create(){
global $database;
$properties = $this->clean_properties();
$sql = " INSERT INTO " .static::$db_table . "(" .implode(",", array_keys($properties)) . ")";
$sql .= "VALUES('" . implode("','", array_values($properties)) . "')";
if($database->query($sql)){
$this->id = $database->the_insert_id();
return true;
}else{
return false ;
}
}
if(empty($file) || !$file || !is_array($file)){
$this->errors[]= "No file uploaded";
return false;
}elseif($file['error'] !=0){
$this->errors[] = $this->upload_errors_array[$file['error']];
return false;
}else{
$this->product_image = basename($file['name']);
$this->tmp_path = $file['tmp_name'];
$this->type = $file['type'];
$this->size = $file['size'];
}
}
public function save_product_and_image(){
if($this->id){
move_uploaded_file($this->tmp_path);
$this->update();
unset($this->tmp_path);
return true;
}else {
if (!empty($this->errors)) {
return false;
}
if (empty($this->product_image) || empty($this->tmp_path)) {
$this->errors[] = "File not available";
return false;
}
$target_path = SITE_ROOT . DS . "admin" . DS . $this->upload_directory . DS . $this->product_image;
if (file_exists($target_path)) {
$this->update();
return true;
}
if (move_uploaded_file($this->tmp_path, $target_path)) {
if ($this->create()) {
unset($this->tmp_path);
return true;
}
} else {
$this->errors[] = "This folder has no write rights";
return false;
}
}
}
public function create(){
global $database;
$properties = $this->clean_properties();
$sql = " INSERT INTO " .static::$db_table . "(" .implode(",", array_keys($properties)) . ")";
$sql .= "VALUES('" . implode("','", array_values($properties)) . "')";
if($database->query($sql)){
$this->id = $database->the_insert_id();
return true;
}else{
return false ;
}
}
Gewijzigd op 16/12/2019 17:40:36 door Jens waelkens
Lees ook eens: https://www.php.net/manual/en/features.file-upload.multiple.php
Let wel op de veiligheid, en zorg voor goede foutafhandeling.
Gewijzigd op 16/12/2019 17:53:56 door - Ariën -