Need Help With File Uploads

I first submitted this as a guest but now im resubmitting it as a registered user and with a better title XD

My goal is to allow the user to select whether or not they want to replace the current img files. If so delete all current ones in select dir and upload into it. If not then began uploading at a specific point such that.

1.jpg
2.jpg
3.jpg

and so on

(replace yes deletes all and no deletes none)

The directories are assigned previously and are accessed via sql database

These are the two current sets i have

[php]<?php $page_title = "Central Valley LLC | Photo Addition" ?>

<?php include("header.php"); ?> <?php include("nav.html"); ?>
		<div id="content">
			<form action="upload_file.php" method="post" enctype="multipart/form-data">
				<label for="which">Choose A Product:</label>
				<?php $con = mysql_connect("localhost","phoenixi_cv","centraladmin");
					if (!$con)
					{
						die('Could not connect: ' . mysql_error());
					}
					mysql_select_db("phoenixi_cvproducts", $con);
					$result = mysql_query("SELECT * FROM Products");
					echo "<select>";
					while($row = mysql_fetch_array($result))
					{
						echo "<option ";
						echo "value=\"" . $row['num'] . "\">";
						echo $row['Name'] . "</option>";
					}
					echo "</select>";
					mysql_close($con);
				?>
				<br />
				<h3 id="center">Do You Wish To Replace Current Images?</h3>
				<br />
				<input type="radio" name="replace" value="y" />YES<br />
				<input type="radio" name="replace" value="n" />NO
				<br />
				<input name="uploads[]" type="file" multiple="multiple" />
				<br />
				<input type="submit" name="submit" value="Submit" />
			</form>		
		</div><!--#content-->
<?php include("footer.html") ?>[/php]

and this is the upload script so far

[php]<?php
$count = 1;
if($_POST[replace]==‘y’)
{
$mydir = ‘assets/images/’ . $_POST[‘which’] . ‘/’;
$d = dir($mydir);
while($entry = $d->read())
{
if($entry!="." && $entry!="…")
{
unlink($entry);
}
}
}
else
{
$loop = true;
while($loop == true)
{
$filename = ‘assets/images/’ . $_POST[‘which’] . ‘/’ . $count . ‘.jpg’;
if(file_exists($filename))
{
$count++;
}
else
{
$loop = false;
}
}
}

if(!is_dir("uploads/".$id))
{   //this checks to make sure the directory does not already exist
	mkdir("uploads/".$id, 0777, true); //if the directory doesn't exist then make it
	chmod("uploads/".$id, 0777);  //chmod to 777 lets us write to the directory
}
$uploaddir = 'assets/images/' . $_POST['which'] . '/';
foreach($_FILES["uploads"]["name"] as $bla=> $boo)
{      //we have to do a loop to get all the filenames
	$file=$uploaddir.$boo;  //we will check the filename in the upload directory, see if it exists
	if (file_exists($file)) 
	{   //if it exists then ......
		die("Filename already exists, please rename this file");   //if filename exists in the directory then we die!!! :P
	}
}
foreach ($_FILES["uploads"]["error"] as $key => $error) 
{
	if ($error == UPLOAD_ERR_OK) 
	{
		echo"$error_codes[$error]";   // let you know if there was an error on any uploads
		move_uploaded_file(      //php function to move the file
		$_FILES["uploads"]["tmp_name"][$key],     //from the temporary directory 
		$uploaddir. $_FILES["uploads"]["name"][$key]   //to the directory you chose
		) or die("Problems with upload");
	}

}
foreach($_FILES[“uploads”][“name”] as $bla=> $boo)
{
$file=$uploaddir.$boo;
$movepoint = $uploaddir . $count . ‘.jpg’;
rename($file, $movepoint);
$count++;
}

?>

[/php]

Thanks in advance for any help that you can give me.Also if you can suggest any easier ways i would certainly be obliged.

Sponsor our Newsletter | Privacy Policy | Terms of Service