Ult1matek1ll Posted February 16, 2012 Share Posted February 16, 2012 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!!! } } 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. Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/ Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 Please post code in [ code][ /code] tags. Also I don't see a question anywhere. What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318105 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318106 Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 Can you post the output of : echo '<pre>' . print_r($_FILES, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318109 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318111 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 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 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318112 Share on other sites More sharing options...
Drummin Posted February 16, 2012 Share Posted February 16, 2012 Your echo "<select>"; should have a name, eg echo "<select name=\"product_id\">"; Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318113 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 Thank you drummin That worked so far will proceed with some more test and reply again Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318115 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318117 Share on other sites More sharing options...
Drummin Posted February 16, 2012 Share Posted February 16, 2012 Not sure. Would adding unset($_FILES); at the end of processing, clear things out for the next upload? Not sure if that's the problem. Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318121 Share on other sites More sharing options...
Ult1matek1ll Posted February 16, 2012 Author Share Posted February 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257128-help-with-file-uploads/#findComment-1318146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.