Jump to content

Recommended Posts

Hi. I'm trying to make a script upload several images at once. I have a form which creates an array of files, with title and description for each file. The following script is supposed to handle the files properly, one by one, but it stops after the first image. I've tried with different images, different filetypes and so on, and I am basically stuck. What am I missing?

<?php
$imageError = "";
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) { 
	$tmp_name = $_FILES["image"]["tmp_name"][$key]; 
	$tmp = strtolower($_FILES["image"]["name"][$key]);
	$illegalChars = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/";
	$tmp = ereg_replace($illegalChars, "_", $tmp);
	$langSpecificChars = array("Æ", "Ø", "Å", "æ", "ø", "å");
	$english = array("e", "o", "a", "e", "o", "a");
	$name = str_replace($langSpecificChars, $english, $tmp);
              if (($_FILES["image"]["type"][$key] != "image/jpeg") && ($_FILES["image"]["type"][$key] != "image/pjpeg") && ($_FILES["image"]["type"][$key] != "image/gif") && ($_FILES["image"]["type"][$key] != "image/png")) { // this is for weeding out non-images and then show which files that were not accepted
		$imageError .= $name . " ";
		continue;
	}
	$imgInfo = getimagesize($tmp_name); // images larger than 800x600 should be resized to save bandwidth/diskspace, and be nice to people with small monitors
	if (($imgInfo[0] > 800) || ($imgInfo[1] > 800)) {
		$tmq = $imgInfo[0];
		if ($imgInfo[0] < $imgInfo[1]) {
			$tmq = $imgInfo[1];
		}
		$tmp = $tmq / 800;
		$cropwidth = $imgInfo[0] / $tmp;
		$cropheight = $imgInfo[1] / $tmp;
		cropImage($cropwidth, $cropheight, $tmp_name, $_FILES["image"]["type"][$key], "../$page/$name");
	}
	else {
		move_uploaded_file($tmp_name, "../$page/$name");
	}
	// image resized (if needed) - moving on to the thumbnail
	$cropwidth = 150; // max thumbnail dimensions
	$cropheight = 150;
	if ($imgInfo[0] < $imgInfo[1]) {
		$cropwidth = 100;
		$cropheight = 150;
	}
	else {
		$cropwidth = 150;
		$cropheight = 100;
	}
	$thumbnail = "../$page/thumb_$name";
	cropImage($cropwidth, $cropheight, "../$page/$name", $_FILES["image"]["type"][$key], $thumbnail);
		// image and thumbnail done, insert info into db
		$tmp = htmlspecialchars($_POST['imageTitle'][$key]);
		$imageTitle = mysql_escape_string($tmp);
		$tmp = htmlspecialchars($_POST['imgDescription'][$key]);
		$imageDescription = mysql_escape_string($tmp);
		$tmp = htmlspecialchars($_POST['approved']);
		$imageApproved = mysql_escape_string($tmp);
		$sql = "INSERT INTO ".$dbTablePrefix."_images (location, gallery, title, description, width, height, approved) VALUES ( "
			."'".$name."', "
			."'".$page."', "
			."'".$imageTitle."', "
			."'".$imageDescription."', "
			."'".$imgInfo[0]."', "
			."'".$imgInfo[1]."', "
			."1"
			.")";
		mysql_query($sql, $dbConnect);
    } // end if
} // end foreach
?>

 

I am not really sure what you mean...

 

The thing is, i did this once before, this is basically just a translation of my previous script (had all the variables and stuff in Norwegian..) So it's a translated copy of a previous script, and the previous one works! I'll post some more code:

 

Old stuff (form and script) - Norwegian...

<p><input type="file" name="bilde[]" accept="image/jpeg" /></p>
<p><input type="text" name="bildeTittel[]" maxlength="25" value="Tittel" /></p>
<p style="margin-bottom: 15px;"><textarea name="beskrivelse[]" rows="2" cols="40">Beskrivelse...</textarea></p>
<div id="myDiv"> </div>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="flereBilder();" title="Enda et bilde...">Nytt bilde...</a></p>

<?php
$feil = "";
$sidenavn = mysql_escape_string($_POST['sidenavn']);
// vi har et array med filer. leser dem inn via en foreach-løkke
foreach ($_FILES["bilde"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) { 
	$tmp_name = $_FILES["bilde"]["tmp_name"][$key];
	$tmp = strtolower($_FILES["bilde"]["name"][$key]);
	$ulovlig = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/";
	$tmp = ereg_replace($ulovlig, "_", $tmp);
	$norske = array("Æ", "Ø", "Å", "æ", "ø", "å");
	$engelske = array("e", "o", "a", "e", "o", "a");
	$name = str_replace($norske, $engelske, $tmp);
        if (($_FILES["bilde"]["type"][$key] != "image/jpeg") && ($_FILES["bilde"]["type"][$key] != "image/pjpeg") && ($_FILES["bilde"]["type"][$key] != "image/gif") && ($_FILES["bilde"]["type"][$key] != "image/png")) {
		$feil .= $name . " ";
		continue;
	}
	$bildeinfo = getimagesize($tmp_name); // må ha litt info om bildets dimensjoner til databasen
	if (($bildeinfo[0] > 800) || ($bildeinfo[1] > 800)) {
		$tmq = $bildeinfo[0];
		if ($bildeinfo[0] < $bildeinfo[1]) {
			$tmq = $bildeinfo[1];
		}
		$tmp = $tmq / 800;
		$cropbredde = $bildeinfo[0] / $tmp;
		$crophoyde = $bildeinfo[1] / $tmp;
		cropImage($cropbredde, $crophoyde, $tmp_name, $_FILES["bilde"]["type"][$key], "../$sidenavn/$name");
	}
	else {
		move_uploaded_file($tmp_name, "../$sidenavn/$name");
	}
	// så må vi lage et lite bilde til forhåndsvisningen
	$cropbredde = 150; // maks minibilde dimensjoner
	$crophoyde = 150;
	if ($bildeinfo[0] < $bildeinfo[1]) {
		$cropbredde = 100;
		$crophoyde = 150;
	}
	else {
		$cropbredde = 150;
		$crophoyde = 100;
	}
	$minibilde = "../$sidenavn/lite_$name";
	// så lager vi det croppede bildet
	cropImage($cropbredde, $crophoyde, "../$sidenavn/$name", $_FILES["bilde"]["type"][$key], $minibilde);
		// filen er lastet opp og thumbnail er lagd, må legge inn info i databasen
		// fjerner skumle saker
		$tmp = htmlspecialchars($_POST['bildeTittel'][$key]);
		$bildeTittel = mysql_escape_string($tmp);
		$tmp = htmlspecialchars($_POST['beskrivelse'][$key]);
		$bildeBeskrivelse = mysql_escape_string($tmp);
		$tmp = htmlspecialchars($_POST['godkjent']);
		$bildeGodkjent = mysql_escape_string($tmp);
		$sql = "INSERT INTO " . $tabellPrefiks . "_bilder (adresse, galleri, tittel, beskrivelse, bredde, hoyde, godkjent) VALUES ( "	
			. "'" . $name . "', "
			. "'" . $sidenavn . "', "
			. "'" . $bildeTittel . "', "
			. "'" . $bildeBeskrivelse . "', "
			. "'" . $bildeinfo[0] . "', "
			. "'" . $bildeinfo[1] . "', "
			. "$bildeGodkjent"
			. ")";
		mysql_query($sql, $tilkobling);
    } // slutt if 
} // slutt foreach
?>

 

This script works. The javascript stuff in the form is for adding more images to the form. I may just be too tired to see some translation error, but the funny thing is that that would probably break the whole script, and the first image always gets uploaded... Please help a poor n00b  :-*

 

Off to bed, will look at it again tomorrow...

 

Okay, so I decided to have another go at this...

 

The problem seems to be that when $_POST is passed, for some reason the image array only gets passed with the first value. My code follows (exerpt).

The form:

<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
(...)
<input type="hidden" name="approved" value="1" id="approved" />
<p><input type="file" name="image[]" accept="image/jpeg" /></p>
<p><input type="text" name="imageTitle[]" maxlength="25" value="Title" /></p>
<textarea name="imgDescription[]" rows="3" cols="40">Description...</textarea>
<p><input type="button" name="more_images" value="Add another image" id="" onclick="moreImages()" /></p>
(...)
<p><input type="submit" name="new_page" value="Save" /></p>
</form>

 

I made the receiving script not do anything except a print_r($_POST); and print_r($_FILES);

This gives me the following output (the whole form):

<?php
// $_POST
Array ( 
[new_p_title] => Title 
[pri_cont] => Some content... 
[sec_cont] => 0
[tert_cont] => 0
[glist] => on
[navigation] => on
[img] => on
[approved] => 1 
[imageTitle] => Array ( 
	[0] => Title 
)
[imgDescription] => Array ( 
	[0] => Description... 
) 
[new_page] => Save 
) 

// $_FILES
Array ( 
[image] => Array ( 
	[name] => Array ( 
		[0] => image_2.png 
	) 
	[type] => Array ( 
		[0] => image/png 
	) 
	[tmp_name] => Array ( 
		[0] => /Applications/MAMP/tmp/php/phpuLMpOu 
	) 
	[error] => Array (
		[0] => 0 
	) 
	[size] => Array (
		[0] => 352906 
	) 
) 
)
?>

As you can see, only the firs value gets passed, even though i have added more images in the array (the form is simply shortened).

 

However, in the "old" form and script gives me "correct" arrays when i print_r($_POST); and print_r($_FILES);

(the above is basically just a translation from the old, which has Norwegian variable names and all that stuff...)

 

Output from old script (exerpt):

<?php
// $_POST
[bildeTittel] => Array ( 
[0] => Tittel 
[1] => Tittel 
) 
[beskrivelse] => Array ( 
[0] => Beskrivelse... 
[1] => Beskrivelse... 
) 
) 

//$_FILES
Array ( 
[bilde] => Array ( 
	[name] => Array ( 
		[0] => Bilde 2.png 
		[1] => Bilde 3.png 
	) 
	[type] => Array ( 
		[0] => image/png 
		[1] => image/png 
	) 
	[tmp_name] => Array ( 
		[0] => /Applications/MAMP/tmp/php/phpJRzevK 
		[1] => /Applications/MAMP/tmp/php/phpIQOhTz 
	) 
	[error] => Array ( 
		[0] => 0 
		[1] => 0 
	) 
	[size] => Array ( 
		[0] => 352906 
		[1] => 208167 
	) 
) 
)
?>

 

Any thoughts?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.