Jump to content

Recommended Posts

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 $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

 

}

}

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++;

}

 

?>

' target='_blank'><?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.

Link to comment
https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/
Share on other sites

<?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
$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!!! 
	}
}
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++;
}

?>

 

 

It currently fails to upload any files and i was curious if i was approaching this at all in the right manner

when yes radial button pressed for empty dir nothing echoed...

when no radial button pressed for empty dir nothing yet again...

 

when yes radial button pressed for populated dir nothing echoed...

when no radial button pressed for populated dir nothing yet again...

 

sadly nothing is being echoed what-so-ever

 

if you would like me to add an echo into the code and test it i will thank you for your help

oh sorry that is precisely what you wanted

 

here you go

 

Array
(
    [uploads] => Array
        (
            [name] => Array
                (
                    [0] => SD card with Aiden 001.JPG
                    [1] => SD card with Aiden 002.JPG
                    [2] => SD card with Aiden 003.JPG
                    [3] => SD card with Aiden 004.JPG
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/php5CvGcY
                    [1] => /tmp/phpSzBVMP
                    [2] => /tmp/phpyymYpH
                    [3] => /tmp/phpGoJc6y
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [size] => Array
                (
                    [0] => 2619687
                    [1] => 2429027
                    [2] => 2720262
                    [3] => 3640722
                )

        )

)

ok works for the most part the only issue i am having is if there are more images than i am uploading to replace it holds on to them still such that

 

1.jpg

2.jpg

3.jpg

4.jpg

5.jpg

 

and i upload only 3 imgs per say:    one.jpg,two.jpg,three.jpg(names used as examples the php upload script renames them  as listed in the first part)

 

my output would be

 

one.jpg

two.jpg

three.jpg

4.jpg

5.jpg

found the problem it was at the beginning with the unlink item.

 

former code

while($entry = $d->read())
	{
		if($entry!="." && $entry!="..")
		{
			unlink($entry);
		}
	}

 

new code

while($entry = $d->read())
	{
		if($entry!="." && $entry!="..")
		{
			unlink('assets/images/' . $_POST['which'] . '/' . $entry);
		}
	}

 

Thank you so much for all of your help guys

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.