Need Help With File Uploads

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
Ult1matek1ll
Forum Newbie
Posts: 1
Joined: Thu Feb 16, 2012 12:30 pm

Need Help With File Uploads

Post by Ult1matek1ll »

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

Code: Select all

<?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") ?>
and this is the upload script so far

Code: Select all

<?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++;
	}
	
?>
Thanks in advance for any help that you can give me.Also if you can suggest any easier ways i would certainly be obliged.
Post Reply