Beste mensen,

Ik wil mijn blog graag uitbreiden met de mogelijkheid om meerdere foto's in een blogitem te kunnen zetten.

Waar ik begin is met het form.

Als ik alles puur met html en php wil oplossen lijkt het vrijwel onmogelijk.


Meerdere input type="file" veldjes zijn namelijk wel toegestaan maar ze moeten altijd gevuld wezen anders gaat het uploaden ook niet door.

heeft iemand een idee hoe je dit oplost?

<?php
if(isset($_POST['submit'])) {
$object = new Blog_set();
$object->page = 'nieuws';
$object->result_page = 'index';

$object->username = $_SESSION['username'];
$object->title = stripslashes($_POST['titel']);
$object->summary = stripslashes($_POST['samenvatting']);
$object->post = stripslashes($_POST['blog']);

//setlocale(LC_TIME, 'nl_NL');
//$blog->tstamp = strftime("%A %e %B %Y %H:%M", time());


$object->caption = stripslashes($_POST['caption']);


$object->attach_file($_FILES['file_upload']);

if(!empty($_FILES['file_upload1'])) {
$object->attach_file($_FILES['file_upload1']);
//$object->attach_file($_FILES['file_upload2']);
//$object->attach_file($_FILES['file_upload3']);
}

if($object->save()) {

$session->message("Blog is toegevoegd op de site");
redirect_to('lijst_blog.php'); //listphotos
} else {
// Fail
$message = join("<br/>", $object->errors);
}

}
?>



	
		<form action="upload_blog.php" enctype="multipart/form-data" method="POST">
		
			<fieldset id="blog">
				<label for="titel">Titel : </label> 
				<input class="input" name="titel" id="titel" type="text" 
					value="<?=$titel ?>" 
					tabindex="1" />
						
				<label for="samenvatting">Samenvatting : </label> 
				<textarea class="input" rows="5" cols="35" id="samenvatting" name="samenvatting" 
					value="<?=$samenvatting ?>" tabindex="2" ></textarea>

				<label for="blog">Blog : </label> 
				<textarea class="input" rows="5" cols="35" id="blog" name="blog" 
					value="<?=$blog ?>" tabindex="2" ></textarea>


			<label for="plaatje">Hoofd-foto</label>	
				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
				<input type="file" name="file_upload" />
			
				<label for="plaatje1">Optioneel extra foto 1</label>	
				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
				<input type="file" name="file_upload1" />

				<!--
<label for="plaatje2">Optioneel extra foto 2</label>	
				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
				<input type="file" name="file_upload2" />
			
				<label for="plaatje3">Optioneel extra foto 3</label>	
				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
				<input type="file" name="file_upload3" />
-->

<label for="caption">Alt:</label> 
				<input type="text" name="caption" value="" /> 
			</fieldset>

			<input type="submit" name="submit" value="Upload" />
			
			</form>


Ik wil namelijk niet voor iedere blog 2 of meerdere foto's uploaden, soms heb ik er aan 1 meer dan voldoende....
Gebruik maken van een array?

Die dan sorteren en de lege velden eruit halen?
@Mr.Ark

Als ik alleen file veld 1 vul, dan heb je al de melding dat de rest leeg is.

terwijl de rest juist optioneel zou moeten wezen.

<?php
if(!empty($_FILES['file_upload1'])) {
$object->attach_file($_FILES['file_upload1']);
//$object->attach_file($_FILES['file_upload2']);
//$object->attach_file($_FILES['file_upload3']);
} ?>

Let wel dat ik alleen 2 input type="file" veldjes aan heb staan momenteel
Snel voorbeeldje gemaakt:

<?PHP

# Errors weergeven
error_reporting(E_ALL);

# Kijken of er gepost wordt
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
# Values trimmen
$Value = trim($Value);

# Array sorteren
foreach($_POST['veld'] as $Key => $Value)
{
# Kijken of er lege values zijn (DIT KAN OOK NOG WAT BETER)
if($Value == '')
{
# Lege value(s) uit de array halen
unset($_POST['veld'][$Key]);
}
}

# Array met de nieuwe waarden
$newArray = array_values($_POST['veld']);

# Kijken of er wel values zijn ingevuld
if(count($newArray) == 0)
{
# Geen values ingevuld
echo 'Er zijn geen values ingevuld.';
}
else
{
# Array sorteren om de nieuwe values te laten zien
foreach($newArray as $Results)
{
echo $Results.'</br>';
}
}
}

?>

Let wel op, kan allemaal wat beter (Ben zelf geen Pro in Array's).

Maak dan van je velden file[]

[Edit]

Even ietsje verbeterd, kijk voor demo hier: http://phphulp.vindme.nl/demo/ArrayUnset.php
Er komt idd wel wat bij kijken om mijn huidige 1 foto per blog systeem om te schakelen naar multiple.

Wat jij aangeeft begrijp ik gedeeltelijk wel.

<?php
# Kijken of er gepost wordt
if($_SERVER['REQUEST_METHOD'] == 'POST')
{

$object = new Blog_set();
$object->page = 'nieuws';
$object->result_page = 'index';

$object->username = $_SESSION['username'];
$object->title = stripslashes($_POST['titel']);
$object->summary = stripslashes($_POST['samenvatting']);
$object->post = stripslashes($_POST['blog']);

$object->caption = stripslashes($_POST['caption']);

$object->attach_file($_FILES['file_upload']);


# Values trimmen
$Value = trim($Value);

# Array sorteren
foreach($_POST['file_upload'] as $Key => $Value)
{


# Kijken of er lege values zijn (DIT KAN OOK NOG WAT BETER)
if($Value == '')
{
# Lege value(s) uit de array halen
unset($_POST['file_upload'][$Key]);
}
}

# Array met de nieuwe waarden
$newArray = array_values($_POST['file_upload']);

# Kijken of er wel values zijn ingevuld
if(count($newArray) == 0)
{
# Geen values ingevuld
echo 'Er zijn geen values ingevuld.';
}
else
{
# Array sorteren om de nieuwe values te laten zien
foreach($newArray as $Results)
{
echo $Results.'</br>';
}
}

if($object->save()) {

$session->message("Blog is toegevoegd op de site");
redirect_to('lijst_blog.php'); //listphotos
} else {
// Fail
$message = join("<br/>", $object->errors);
}

}

?>

Alleen zorgt $object->save() ervoor dat alles wordt verdeeld als enkel bestand.

Hier begint het voor mij vanwege weinig ervaring nog iets te duizelen met omschakkelen.

<?php
// Pass in $_FILE(['uploaded_file']) as an argument
public function attach_file($file) {
// Perform error checking on the form parameters.
if(!$file || empty($file) || !is_array($file)) {
// Error: nothing uploaded or wrong argument usage
$this->error[] = "No file was uploaded";
return false;

} elseif($file['error'] != 0) {
// Error: report what PHP says went wrong
$this->errors[] = $this->upload_errors[$file['error']];
return false;
} else {



// Set object attributes to the form parameters.
$this->id = $file['id'];
$this->temp_path = $file['tmp_name'];
$this->filename = basename($file['name']);
$this->type = $file['type'];
$this->size = $file['size'];
// Dont worry about saving anything to the database yet
return true;
}
}

public function save() {
// A new record wont have an id yet
if(isset($this->id))
{
// Really just to update the caption
$this->update();
}
else
{
/////// Make sure there are no errors

// Cant save if there pre-existing errors
if(!empty($this->errors))
{
return false;
}

// Make sure the caption is not to long for DB
if(strlen($this->caption) > 50)
{
$this->errors[] = "De ondertitel van de foto is langer dan 50 tekens.";
return false;
}
// Cant save without filename and temp location
if(empty($this->filename) || empty($this->temp_path))
{
$this->errors[] = "Locatie bestand is niet beschikbaar.";
return false;
}

// Make sure a file doesn't already exist in the target location
if(file_exists($this->filename))
{
$this->errors[] = "Het bestand {$this->filename} bestaat al.";
return false;
}

######################################
# Maak een thumbnail #
######################################

$width = 150;
$height = 100;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($this->temp_path);//$target_path gaf rare tekens



$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig)
{
$width = $height*$ratio_orig;
}
else
{
$height = $width/$ratio_orig;
}

// Resample
$thumb = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($this->temp_path); //$target_path gaf rare tekens
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//Determine the target_path_thumb
$target_thumb = SITE_ROOT.DS. $this->upload_dir_thumb .DS. $this->caption.'-'.$this->filename;

// Output
imagejpeg($thumb, $target_thumb, 35);

// Free up memory
imagedestroy($thumb);

######################################
# Maak een blogitem(groot) #
######################################
$save_for_blog = SITE_ROOT .DS. $this->upload_dir_blog .DS. $this->caption.'-'.$this->filename;

list($width, $height) = getimagesize($this->temp_path);



$new_height = 150;
$new_width = 483;


$modwidth = 483;

$tn = imagecreatetruecolor($new_width, $new_height) ;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($this->temp_path);


$ratio_orig = $width_orig/$height_orig;

if ($modwidth/$modheight > $ratio_orig) {
$modwidth = $modheight*$ratio_orig;
} else {
$modheight = $modwidth/$ratio_orig;
}

// Resample
$image_resized = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($this->temp_path); //$target_path gaf rare tekens
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $modwidth, $modheight, $width_orig, $height_orig);


//bool imagecopyresampled

imagecopy(
//resource $dst_image
$tn,
//resource $src_image
$image_resized,
//int $dst_x
0,
//int $dst_y
0,
//int $src_x
0,
//int $src_y
120,
//int $src_w
$width,
//int $src_h
$height
) ;




//!!!!! VERANDEREN in ander logo
$watermerk = imagecreatefrompng('watermerk_orgineel.png');

$marge_right = 0;
$marge_bottom = 0;

$sx = imagesx($watermerk);
$sy = imagesy($watermerk);
imagecreatetruecolor($sx, $sy);

imagealphablending($watermerk, false);
imagesavealpha($watermerk, true);


// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.

//bool imagecopy

imagecopy(
//resource $dst_im
$tn,
//resource $src_im
$watermerk,
//int $dst_x
0,
//int $dst_y
0,
//int $src_x
0,
//int $src_y
0,
//int $src_w
imagesx($watermerk),
//int $src_h
imagesy($watermerk)
);

// Send the image
header('Content-type: image/png');
//--- upload image

// als je deze uitzet wordt het zwart en 5.28kb ipv 26kb
imagejpeg($tn, $save_for_blog, 92); // max. kwaliteit

imagedestroy($tn);




// Save a corresponding entry to the database
if($this->create())
{
// We are done with temp_path, the file isnt there anymore
unset ($this->temp_path);
return true;
}

}
}
?>

Sorry voor de lap.

Maar misschien kun je me een goede richting op duwen.

Je moet binnen de laatste foreach resizen, je wilt namelijk dat elke foto wordt geresized.

Zelf ben ik niet bekend met werken van functions dus hier houdt mijn kennis helaas op.

Reageren