Page 1 of 1

uploading into different folders.

Posted: Thu Apr 26, 2012 4:36 pm
by identity_crisis
Im trying to create an uploader that has an option to select different folders to upload to.

Ive managed to get it to upload to a folder using an if else statement but it only uploads to the else statement. Ive managed to get it to upload to an if section using !isset but it still doesnt seem to be changing the folder path depending on what I select in my form. As its set up now it uploads to the lower folder.

The Form

Code: Select all

<?php 
// Start a session for displaying any form errors
session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>uploader</title>

		<style type="text/css">
			label
			{
				float: left;
				text-align: right;
				margin-right: 10px;
				width: 100px;
				color: black;
			}

			#submit
			{
				float: left;
				margin-top: 5px;
				position: relative;
				left: 110px;
			}

			#error
			{
				color: red;
				font-weight: bold;
				font-size: 16pt;
			}
		</style>
	</head>

	<body>
	
		<div>
				<?php
				if (isset($_SESSION['error']))
				{
					echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
					unset($_SESSION['error']);
				}
				?>
                                
				<form action="upload.php" method="post" enctype="multipart/form-data">
				<p>
					<label>Category</label>
					<input type="text" name="category" /><br />

                                        <label>Folder</label>
                                        <select>
                                          <option value="upper">Upper</option>
                                          <option value="lower">Lower</option>
                                          <option value="footwear">Footwear</option>
                                        </select> <br />

					<label>Upload Image</label>
					<input type="file" name="image" /><br />
					<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
					<input type="submit" id="submit" value="Upload" />
				</p>
				</form>
		</div>
	</body>
</html>
The upload form

Code: Select all

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/wp-config.php");

$upper = $_POST['upper'];
$lower = $_POST['lower'];
$foot = $_POST['footwear'];
$random_digit=rand();


// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/gif");

	if (in_array($file['type'], $valid_types))
		return 1;
	return 0;
}

function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 

if (!isset($upper))
{
$TARGET_PATH = "upper/";
}

if (!isset($lower))
{
$TARGET_PATH = "lower/";
}

else 
{  
$TARGET_PATH = "foot/";
}          
// Get our POSTed variables
$cat = $_POST['category'];
$image = $_FILES['image'];
$exts = findexts ($image['name']);
$ran2 = $random_digit.".";
$new_file_name=$ran2.$exts;
// Sanitize our inputs
$cat = mysql_real_escape_string($cat);
$image['name'] = mysql_real_escape_string($image['name']);


// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $new_file_name;



// Make sure all the fields from the form have inputs
if ( $cat == "" || $image['name'] == "" )
{
	$_SESSION['error'] = "All fields are required";
	header("Location: index.php");
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
	$_SESSION['error'] = "You must upload a jpeg, or gif";
	header("Location: index.php");
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	$_SESSION['error'] = "A file with that name already exists";
	header("Location: index.php");
	exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	
	$sql = "insert into category (category, filename) values ('$cat', '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
        header("Location: index.php");
	exit; 
    
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
	header("Location: index.php");
	exit;
}

        
?>
Any help appreciated.

Re: uploading into different folders.

Posted: Thu Apr 26, 2012 5:27 pm
by requinix

Code: Select all

if (!isset($lower))
{
$TARGET_PATH = "lower/";
}

else 
{  
$TARGET_PATH = "foot/";
}
According to that code, what will happen if $lower is not set? Is that in any way affected by whether $upper is set?

Re: uploading into different folders.

Posted: Thu Apr 26, 2012 5:32 pm
by identity_crisis
It doesnt matter which option I select on the pull down menu for the upload form (upper,lower or footwear) as it is now it always uploads to the lower folder. If i remove the ! from the isset function on the

if (!isset($lower))
{
$TARGET_PATH = "lower/";

Then it uploads to the foot folder which is the else option.

Re: uploading into different folders.

Posted: Thu Apr 26, 2012 7:05 pm
by Celauran
Your select doesn't have a name.

Code: Select all

...
<select name="folder">
    <option value="upper">Upper</option>
    <option value="lower">Lower</option>
    <option value="footwear">Footwear</option>
</select>
...

Code: Select all

$TARGET_PATH = $_POST['folder'];

Re: uploading into different folders.

Posted: Mon Apr 30, 2012 9:56 am
by identity_crisis
Thanks to all the replys.

Just thought I would update this thread and let people know that its sorted.

I had to put a forward slash onto the values to get it to work but its finally working.