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>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;
}
?>