Jump to content

Wrong file type for second file upload


Recommended Posts

I use the following code to move two files to permanent location while inserting their name in db table. IT works fine as long as you do not try to upload the wrong file type for second file.

// check for img1	if (!isset($_FILES['img1']['name']) OR empty($_FILES['img1']['name'])) {				$errors['img1'] = '\'Image 1\' is a required field';		$img1 = FALSE;		} else {		$ext = explode('.',$_FILES['img1']['name']);		$ext = $ext[count($ext)-1];		if (!in_array($ext, $allowed)) {		$errors['img1'] = '\'Image 1\' accepts only jpg and gif';		$img1 = FALSE;		} else {		$img1 = $user_id . '-1.' . $ext;		}		}	// check for img2	if (!isset($_FILES['img2']['name']) OR empty($_FILES['img2']['name'])) {				$errors['img2'] = '\'Image 2\' is a required field';		$img2 = FALSE;		} else {		$ext2 = explode('.',$_FILES['img2']['name']);		$ext2 = $ext2[count($ext2)-1];		if (!in_array($ext2, $allowed)) {		$errors['img2'] = '\'Image 2\' accepts only jpg and gif';		$img2 = FALSE;		} else {		$img2 = $user_id . '-2.' . $ext2;		}		}	if (isset($_FILES['img1'])) { 	// validate the input		global $ext;		if (in_array($ext, $allowed)) {			// move the file over			if(move_uploaded_file($_FILES['img1']['tmp_name'], "{$images}{$img1}")) {			echo '<p>file 1 has been uploaded ok</p>';			} else {			echo '<p>file 1 upload did not work</p>';			}			if(move_uploaded_file($_FILES['img2']['tmp_name'], "{$images}{$img2}")) {			echo '<p>file 2 has been uploaded ok</p>';			} else {			echo '<p>file 2 upload did not work</p>';			}			// add the data

While only $img1 is required I still need to check for valid file types before moving and inserting in db (therefore line if (in_array($ext, $allowed)) ). With this way it does only work if the file type for $img2 is correct. Otherwise, there will be error messages as $img2 is FALSE (from my validation routine before). How can I amend line:if (in_array($ext, $allowed)) {to also check for $ext2? I tried:if (in_array($ext, $allowed)) && (in_array($ext2, $allowed)) {but the page won't even display...Any ideas?Kurt

Link to post
Share on other sites
No, that wont work. Why? You got the '(' and ')' wrong. It should be:
if(in_array($ext, $allowed) && in_array($ext2, $allowed)) {

Corrected and page displays. But now I realise another conceptual error I made. For the upload to work fine both files have to be uploaded. THis is not what I want. Only one is mandatory ($img1). Can I somehow amend line:if(in_array($ext, $allowed) && in_array($ext2, $allowed)) { so it only tests for $ext2 if $img2 is not FALSE (which is the complete file name including extension if it passed validation)?Kurt
Link to post
Share on other sites
You're trying to do all the files at once, have a loop that only does one at a time. You can substitute the number into the variable name to check.
I thought about a loop before, but then the problem comes in tha the db query should only be run once. I really do not have a clue in how to amend that it loops through my images, but it only does once db insert query. The code as it is:
	if (isset($_FILES['img1'])) { 	// validate the input		global $ext;		global $ext2;		if(in_array($ext, $allowed) && in_array($ext2, $allowed)) {					// move the file over			if(move_uploaded_file($_FILES['img1']['tmp_name'], "{$images}{$img1}")) {			echo '<p>file 1 has been uploaded ok</p>';			} else {			echo '<p>file 1 upload did not work</p>';			}			if(move_uploaded_file($_FILES['img2']['tmp_name'], "{$images}{$img2}")) {			echo '<p>file 2 has been uploaded ok</p>';			} else {			echo '<p>file 2 upload did not work</p>';			}			// add the data			$query = "INSERT INTO imgTest (user_id, img1, img2) VALUES ('$user_id', '$img1', '$img2')";					$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());			if (mysqli_affected_rows($dbc) == 1) { // If it ran ok       			echo "db insertion went ok";			} else { // if it did not run ok				echo '<p>Your data could not be uploaded due to a system error. We apologise for any inconvenience.</p>'; 			}				}  else { // if one of the data tests failed		echo '<p>You forgot to upload required data or there is a problem with the given input. Please amend the highlighted fields.</p>';

Can you see what I mean?Kurt

Link to post
Share on other sites

Build the query during the loop and run it after the loop finishes.

$sql = 'UPDATE artists SET ';$values = '';for (each image){  if (valid)  {	$values .= "img{$x}='{$value}',";  }}if ($values != ''){  $sql .= substr($values, 0, -1);  $sql .= " WHERE id={$id}";  mysql_query($sql);}

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...