Jump to content

php functions


j05hr

Recommended Posts

Hi,

 

I've got 3 different upload forms in one form.  I'm making a function so it just one piece of code.

 

I'm having a bit of a problem changing some of the code so it works for all 3.

 

Here is the function

 

 

 

function loadImages ($action, $position)
{
	 $link = mysql_connect("","","");
	if (!$link)
	{
		die('Error: could not connect: ' . mysql_error());
	}

	$db_select = mysql_select_db("",$link);
	if (!$db_select)
	{
		die ("Error: database selection failed: " . mysql_error());
	}
	$page = ($_POST["pageName"]);
	if ((($_FILES["bfile"]["type"] == "image/gif")
	|| ($_FILES["bfile"]["type"] == "image/jpeg")
	|| ($_FILES["bfile"]["type"] == "image/pjpeg"))
	&& ($_FILES["bfile"]["size"] < 9000000))
	{
		if ($_FILES["bfile"]["error"] > 0)
		{
			echo "Return Code: " . $_FILES["bfile"]["error"] . "<br />";
		}
		else
		{
			 $success = '<p style="font-weight: bold; color: #C00">Upload successful<br />';
				// move uploaded temporary file to final destination
				move_uploaded_file($_FILES["bfile"]["tmp_name"],
									"uploads/" . $_FILES["bfile"]["name"]);

			// update database with new filename
			 $filename = $_FILES["bfile"]["name"];
			 $sql = "UPDATE images 
			 SET filename= '$filename'
			 where page = '$page'
			 and position = '$position'";

				if (!mysql_query($sql,$link))
				{
					die('Error: ' . mysql_error());
				}

		}
	}
	else
	{
		 $fail = '<p style="font-weight: bold; color: #C00">Invalid file';
	}
	mysql_close($link);
}

 

Now the $action is the bit that will determine whether it's updateTop updateMiddle or updateBottom.  $position finds out if it's top middle or bottom

 

if (isset($_POST['updateTop'])) 
    {
	loadImages ($_POST['updateTop'], "top");
}

 

Here is a sample of the html, it's in a switch

case "Products":
                              echo "<p>Image Top</p>
                                    <input type=\"file\" name=\"file\" id=\"file\" />  <input type=\"submit\" name=\"updateTop\" value=\"Update $page Top\" />
                                    <p>Image Middle</p>
                                    <input type=\"file\" name=\"mfile\" id=\"mfile\" />  <input type=\"submit\" name=\"updateMiddle\" value=\"Update $page Middle\" />
                                    <p>Image Bottom</p>
                                    <input type=\"file\" name=\"bfile\" id=\"bfile\" />  <input type=\"submit\" name=\"updateBottom\" value=\"Update $page Bottom\" />
                                    ";
                              break;

 

What I need to do is get

$_FILES["bfile"

into a variable to see whether it's "bfile" "mfile" or "file" but not sure how to write it.

 

Thanks for the help.

Link to comment
Share on other sites

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.