Jump to content


Photo

Quick Question regarding Radio Buttons with PHP


  • Please log in to reply
7 replies to this topic

#1 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 02 October 2006 - 04:35 PM

Okay so I have an upload site where users can upload an image. At first i made it simple and made it so users can upload an image and it is sent to their USER DIRECTORY and to a PUBLIC DIRECTORY. Now what i want to happen is to add radio buttons with different PORTAL NAMES on it where the file will be sent. So for example:

if the user wants to upload picture: "lol.jpg" and they check the "mainportal" radio button than i want it to use a function that i previousley made that will check it's size and do all that crap and than SEND it to their user directory/mainportal. Here is the script of the form, and where i think i need to put the radio button script, but I DONT KNOW how to manipulate the radio  buttons so it calls the script...

<input type="hidden" name="MAX_FILE_SIZE" value="' . $uploadmax . '" />
		          Overwrite existing image(s)? Yes<input type="checkbox" name="overwrite_file" value="true" /><br />
				  <br>
				  Which Portal to send Files: 
				  <br>
				  <label>
      <input type="radio" name="mainportal" value="main">
      Main Portal</label>
	  <br>
				  <input type="file" name="imgupload_one" /><br />
				  <input type="file" name="imgupload_two" /><br />
				  <input type="file" name="imgupload_three" /><br />
				  <input type="file" name="imgupload_four" /><br />
				  <input type="file" name="imgupload_five" /><br />
				  <input type="file" name="imgupload_six" /><br />
				  <input type="file" name="imgupload_seven" /><br />
				  <input type="file" name="imgupload_eight" /><br />
				  <input type="submit" name="upload_image" value="Upload Image(s)" /><br />
				  	<br />
				<align="center">Click<a href="/more.php/"> here</a> to upload MORE pictures.	
				  </form></p>';
	}


Okay so that script makes it call up the function "upload". And here is where i define that function:

function upload($user_dat, $globalvars)
	{
		$this->display_space($user_dat, $globalvars);
		
		if(isset ($_POST['upload_image']))
		{
			if($_FILES['imgupload_one']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_one');
			} else {
				echo "You must select at least one image to upload!<br />";
			}
			
			if($_FILES['imgupload_two']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_two');
			}
			
			if($_FILES['imgupload_three']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_three');
			}
			
			if($_FILES['imgupload_four']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_four');
				
			}
			
			if($_FILES['imgupload_five']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_five');	
				}
		
			if($_FILES['imgupload_six']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_six');
				
			}
			if($_FILES['imgupload_seven']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_seven');
				
			}
			if($_FILES['imgupload_eight']['name'] != null)
			{
				$this->upload_transfer($user_dat, $globalvars, 'imgupload_eight');
				
			}
			//here is where i think i should put the radio buttons IF statements.
	}

So yeah above after all the img_upload defines is where i am thinking i should put the IF statement, but i don't know what exactly to put. PLEASE HELP!

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 02 October 2006 - 06:47 PM

Maybe I misunderstand the question. You already have the code working, so you're just adding an if statement to the end, right? wouldn't it just be
if($_POST['mainportal']=="main") ...
Of course, you'd also have to redo all of those if statements for each upload.

Better solution would be to make a loop:
do {
	if($_FILES[key($_FILES)]['name'] != null) {
		$this->upload_transfer($user_dat, $globalvars, key($_FILES));
	}
}while(next($_FILES));


Now, what upload_transfer() does is a mystery (since you didn't include it here). If it does all the validation stuff to the file AND moves it with move_upload_files to someplace, then your additional if statement might not work because the original file has moved already.

#3 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 02 October 2006 - 07:34 PM

Here is the upload_transfer function, but i was not concerned about the img_uploads. What i wanted to do was just make a new function called mainupload_function and have it executed when some highlighted the RADIO BUTTON MAIN PORTAL. here is the main upload function script:

function upload_graffiti($user_dat, $globalvars, $whichfile)
	{
		$error_occured = false;
		
		echo "<i>" . $_FILES[$whichfile]['name'] . " upload status</i>:<br />";
		
		switch($_FILES[$whichfile]['error'])
		{
			case 1:
				$error_occured = true;
				echo "The image you attempted to upload exceeds the max file size limit in the PHP configuration file. Please either reduce  your image size, or contact an administrator and ask them to expand file size<br />";
			break;
				
			case 2:
				$error_occured = true;
				echo "The image you attempted to upload exceeds the max file size limit set by the admin.<br />";
			break;
		}
				
		$split_ext = explode(',', $globalvars->settings['allowed_ext']);
		$split_img = explode('.', $_FILES[$whichfile]['name']);
		$extcount = count($split_ext);

		for($i=0;$i<$extcount;$i++)
		{
			$invalidimg = true;
			$split_values = explode(':', $split_ext[$i]);
			$i_values = $extcount - 1;
			
			if($split_values[0] == strtolower($split_img[1]) && $split_values[1] == "allow")
			{	
				$invalidimg = false;
				$i = $extcount + 1000;
			} elseif($i == $i_values && $split_values[0] != strtolower($split_img[1]))
			{
				// If the image was valid, we would have exited by now.
				$error_occured = true;
			}
		}
	
		if(is_dir($user_dat['usrdir']))
		{
			$sql_dirlimit = explode(':', $globalvars->settings['directory_limit']);
			
			switch($sql_dirlimit[1])
			{
				case MB:
					$dirsize_set = new functions();
					$dirsize = $dirsize_set->mb_bytes($sql_dirlimit[0]);
				break;
				
				case KB:
					$dirsize_set = new functions();
					$dirsize = $dirsize_set->kb_bytes($sql_dirlimit[0]);
				break;
			}
					
			if($user_dat['usedspace'] < $dirsize)
			{
				if(file_exists($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))
				{
					if($_POST['overwrite_file'] == true)
					{
						unlink($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']);
					} else {
						$error_occured = true;
								
						echo "The image you attempted to upload already exists!<br />Please select to overwrite exisiting images, or rename the image you're attempting to upload.<br /><br />";
					}
				}
			} else {
				$error_occured = true;
				echo "You have used up all your directory space.<br />";
			}
		} else {
			$error_occured = true;
				
			echo "Your directory is non-existant. Please contact an adminstrater.<br />";
		}
				
		if($error_occured != true)
		{
		

		if(move_uploaded_file ($_FILES[$whichfile]['tmp_name'], $user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))
			{
			$file = $user_dat['name'] . "-" . $split_img[0] . "." . strtolower($split_img[1]);
		$destination2 = "portals/graffiti/$file";
		
				rename($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name'], $user_dat['usrdir'] . "/" . $split_img[0] . "." . strtolower($split_img[1]));
				copy($user_dat['usrdir'] . "/" . $split_img[0] . "." . strtolower($split_img[1]), $destination2); 
				echo "Your image was uploaded successfully. ";
				echo "Click " . '<a href="users/' . $user_dat['name'] . "/" . $split_img[0] . "." . strtolower($split_img[1]) . '" target="_blank">here</a>' . " to view your image.";
			} else {
				echo "An unknown error occured.";
			}
		} else {
			if($invalidimg == true)
			{
				echo "The type of image you attempted to upload is not allowed for upload.";
			}			
		}
		
		echo "<br />----------------------------------------------------------------------------------------------<br />";
	}

All of it works and should work as long as i find a way to make the RADIO Button execute this function. This is the part that is different than the original function "upload_transfer"

if(move_uploaded_file ($_FILES[$whichfile]['tmp_name'], $user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))
{
$file = $user_dat['name'] . "-" . $split_img[0] . "." . strtolower($split_img[1]);
$destination2 = "portals/graffiti/$file";

rename($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name'], $user_dat['usrdir'] . "/" . $split_img[0] . "." . strtolower($split_img[1]));
copy($user_dat['usrdir'] . "/" . $split_img[0] . "." . strtolower($split_img[1]), $destination2);
echo "Your image was uploaded successfully. ";
echo "Click " . '<a href="users/' . $user_dat['name'] . "/" . $split_img[0] . "." . strtolower($split_img[1]) . '" target="_blank">here</a>' . " to view your image.";
} else {

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 02 October 2006 - 07:41 PM

as far as I can tell (and I didn't pour over your code), you should be doing fine. If it doesn't work, shout back with errors or problems. Try to make your question a little less nebulous, though.

#5 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 02 October 2006 - 10:10 PM

Alright so wait, i should just do what i did with my FORM image uploads but replace with my radio buttons and direct it to a different function.. cause that did not work... explain to me what i should do please

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 02 October 2006 - 10:15 PM

if checking that radio should do something totally different, just put an if statement for the radio button in your code that does one thing, then else does what it used to do... make sense?

#7 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 02 October 2006 - 10:51 PM

Yes But what if i want several radio buttons to differnet portals so would i just make a lot of IF statements all saying ELSE to the same public upload?

#8 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 02 October 2006 - 10:56 PM

well, if the only difference is where it goes, then pass the radio'ed variable to your move_uploaded_file function as the directory name to move the file to -- no if statements required.

of course, you'd need to have a default radio button already selected.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users