Jump to content

How to Move an Uploaded image to Certain directory with RADIO buttons


bizerk

Recommended Posts

lets say i have a <input type="radio" name="pictures" value="">

 

I want it so when the radio button named 'pictures' is clicked and a picture is submitted that it will upload to the already existing folder... /pictures...I already have the entire upload function coded I just need some help or an EXAMPLE of what this function would look like... like can someone write up a quick example of this? with the IF statement and everything

Link to comment
Share on other sites

The code StormTheGates would work for you once you set a value for the radio button.

 

Here is a mostly completed chunk. It will give you an idea about how to do this. I used a switch statement instead of if elseif elseif

 

<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="location" value="pictures" />
      Pictures</label>
    <br />
    <label>
      <input type="radio" name="location" value="movies" />
      Movies</label>
    <br />
<label>
      <input type="radio" name="location" value="music" />
      Music</label>
    <br />
  </p>
  <input type="submit" value="Submit" name="submit"  />
</form>
<?php
if($_POST['submit'])
{
$location=$_POST['location'];
	switch($location)
	{
		case 'pictures';
		$uploadto='/pictures';
			break;
		case 'movies';
		$uploadto='/movies';
			break;
		case 'music';
		$uploadto='/music';
			break;
		default;
		$uploadto='/other';
            	                break;
	}
echo $uploadto;
}
?>

Link to comment
Share on other sites

okay so here is what i have so far... but it's not working. im like retarded. anyone tell me how to fix this...? or what i am doing wrong:

 

<html>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="location" value="pictures" />
      Pictures</label>
    <br />
    <label>
      <input type="radio" name="location" value="school" />
      Movies</label>
    <br />
<label>
      <input type="radio" name="location" value="music" />
      Music</label>
    <br />
 <input type="file" name="imagefile" />
 <br>
  </p>
  <input type="submit" value="Submit" name="submit"  />
</form>
<?php
if($_POST['submit'])
$lol = rand(1,100);
{
$location=$_POST['location'];
	switch($location)
	{
		case 'pictures';
		$uploadto='/pictures';
			break;
		case 'school';
		$uploadto='/school';
			break;
		case 'music';
		$uploadto='/music';
			break;
		default;
		$uploadto='/other';
            	                break;
	}
copy ($_FILES['imagefile']['tmp_name'], "$uploadto/$lol".$_FILES['imagefile']['name'])
or die ("Error Occured When Uploading Image File to Directory $uploadto");
;
}
?>
</html>

Link to comment
Share on other sites

One of a couple things....

 

Permissions. I was working on an upload script last night and found I had to chmod the directory to 0777 in order for it to move the file.

 

Not sure on the copy() part.

 

I thought you had to use move_uploaded_file() to get it to work.

 

Here is a snippet of what I have for mine.

 
$name=$_FILES['file_upload']['name'];

$uploaddir = $_SERVER['DOCUMENT_ROOT']."/members/docs/"; 
$uploaddir.=$name;

                               if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $uploaddir))
			{
			  echo "Documents Updated!";
			}
			else
			{
			  echo "There was a problem when uploading the new file";
			  
			}

Link to comment
Share on other sites

Okay well it's not the actual uploading that i have problems with. it's just I don't understand how to implement that radio button instances you told me, with the upload function. here I will upload the entire script that i have that WORKS, but i don't see how i can make it so it uploads to certain directories... here is what i have that works:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Joe Upload</title>
<style type="text/css">
<!--
body {
background-image: url(images/index_09.gif);
}
body,td,th {
color: #000000;
}
.style1 {font-size: 10px}
a:link {
color: #CC9900;
}
a:visited {
color: #666600;
}
-->
</style></head>
<body>
<form name="form1" method="post" action="" enctype="multipart/form-data">
  <div align="left">
    <table width="527" border="1" align="center">
      <tr>
        <td width="8" valign="top"> </td>
        <td colspan="2" valign="top" background="../images/hardwired.gif"><p class="style1"><b><u>LOGIN INFORMATION:</u></b><br />
          Name:
              <input name="name" type="text" value="--CURRENTLY DISABLED--" />
        <br />
        <br />
          Password:
          <input name="password" type="text" value="--CURRENTLY DISABLED--" />
        </p>
          <p class="style1">Login is Currently Disabled. Account Creation will soon be avaible. To get a private upload directory click <a href="/makedirectory.php">here  </a>.</p>
          <p class="style1">Have a Private Directory? Upload to it by clicking <a href="/privateupload.php">here </a></p></td>
      </tr>
      <tr>
        <td height="120" rowspan="2" valign="top"> </td>
        <td width="333" height="59" valign="top" background="../images/bg.gif"><b><u>Upload a File:</u></b><br />
          1.
          <input type="file" name="imagefile" />
    <br />
    <br />
          2.
  <input type="file" name="imagefile2" />
  <br />
  <br />
          3.
  <input type="file" name="imagefile3" />
  <br />
  <br />
          4.
  <input type="file" name="imagefile4" />
  <br />
  <br />
          5.
  <input type="file" name="imagefile5" />
  <br />
          <br />
          6.
  <input type="file" name="imagefile6" />
  <br />
          <br />
          7.
  <input type="file" name="imagefile7" />
  <br />
          <br />
          8.
  <input type="file" name="imagefile8" />
  <br />
          <br />
          9.
  <input type="file" name="imagefile9" />
  <br />
          <br />
          10.
  <input type="file" name="imagefile10" />
  <br />
  <br />
  <br />
  <br />
  <br /></td>
        <td width="164" valign="top" background="../images/bg.gif"><p align="center">
  <object width="260" height="199"> 
    <param name="movie" value="http://www.youtube.com/v/H4tURCcrJYI"> 
    </param> 
    <embed src="http://www.youtube.com/v/H4tURCcrJYI" type="application/x-shockwave-flash" width="260" height="199"> </embed>
  </object>
   Submit to: </p>
          <p align="center">
           <input type="radio" name="location" value="pictures" />
      Pictures</label>
    <br />
    <label>
      <input type="radio" name="location" value="school" />
     School</label>
    <br />
<label>
      <input type="radio" name="location" value="music" />
      Music</label>
    <br />
 <br>
          <p align="center">
            <input type="submit" name="Submit" value=" Submit" />
          </p>
       </p></td>
      </tr>
      <tr>
        <td height="59" colspan="2" valign="top" background="images/index_04.gif"><?

$lol = rand(1,100);
$browser = $REMOTE_ADDR;
$name = $_POST['name'];
$password = $_POST['password'];
$Submit = $_POST['Submit'];
function uploadFiles($key)
{
$lol = rand(1,100);


copy ($_FILES[$key]['tmp_name'], "pictures/$lol".$_FILES[$key]['name'])
or die ("Error Occured When Uploading Image File to Directory");
echo "<br><u><b><center>File<br />
: {$_FILES[$key]['name']} Status</u></b></center><br><br>
File Size: {$_FILES[$key]['size']}<br>
File Type: {$_FILES[$key]['type']}<br>
<br><br>
File {$_FILES[$key]['name']} was uploaded Succesfully.<br>";
echo"<br> Click <a href='view/'>here</a> to see your files<br>";
}
if($_POST['Submit']) {

foreach ($_FILES as $key=>$file)
		{
		if (!empty($file['name']))
			{
			uploadFiles($key);
			}
}
		}else{
		echo "Something Went Wrong with the upload process... try our back up uploader...<a href='/uploader.php'>Go To Simple Uploader</a>";
		}

?></td>
      </tr>
    </table>
    <div align="center"></div>
    <p><br />
      <br />
      <br />
      <br />
    </p>
  </div>
</form>
</body>
</html>

Link to comment
Share on other sites

A few things I can see right off the bat.

 

1)  You have static upload fields, so put in the work and address each one independently instead of trying to use a foreach statement to get their contents.

 

You  are trying to use the $key variable before it is actually set. This won't work.

 

2) You have no reference to your location fields. You can set your location value as /images or /music. Just make the value of that field the actual path for where you want it to go.

 

I don't think that your trying to move the files correctly, so I am going to show you how I would implement your location fields using the code I posted earlier.

 

 

$location=$_POST['location']; // set a variable for the location form item

$name=$_FILES['file_upload']['name'];

 

$uploaddir = $_SERVER['DOCUMENT_ROOT'].$location;

$uploaddir.=$name;

 

                              if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $uploaddir))

{

  echo "Documents Updated!";

}

else

{

  echo "There was a problem when uploading the new file";

 

}

 

This would result in the path /var/www/test/pictures/cpuz.txt when the radio pictures was checked and uploading a file called cpuz.txt (I changed the value to /pictures)

 

Take a look at this and post back if it don't clear anything up, and I will try to help later on.

 

Right now I gotta take my kids to a Park to play for a while. :)

 

Here is my working upload form, it uploads and moves the file correctly. Look over it and see how I am dong things and see where our code differs.

 

if($_POST['submit'])

{

if($_FILES['file_upload']['tmp_name'])

{

$file=$_FILES['file_upload']['tmp_name'];

if(is_uploaded_file($file))

{

$name=$_FILES['file_upload']['name'];

$size=fsize_convert($_FILES['file_upload']['size']);

$uploaddir = $_SERVER['DOCUMENT_ROOT']."/members/docs/";

$uploaddir.=$name;

echo $uploaddir;

echo '<br><br><br>';

if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $uploaddir))

{

  echo "Documents Updated!";

}

else

{

  echo "There was a problem when uploading the new file";

 

}

 

}

}

}

else

{

?>

<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">

<table width="50%" border="0" align="center" cellpadding="5" cellspacing="0">

  <tr>

    <td>Title</td>

    <td>

      <input name="title" type="text" id="title" />

      </td>

  </tr>

  <tr>

    <td>Description</td>

    <td>

      <textarea name="description" cols="35" rows="3" id="description"></textarea>

      </td>

  </tr>

  <tr>

    <td>File</td>

    <td><input type="file" name="file_upload" id="file_upload" /></td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td colspan="2" align="center">

      <input name="submit" type="submit" id="submit" value="Submit" />

   

    </td>

  </tr>

</table>

</form>

 

 

 

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.