Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:06:11+00:00 2026-06-15T22:06:11+00:00

I have a script that I’m trying to use to upload zip files of

  • 0

I have a script that I’m trying to use to upload zip files of photos.
The script should do these things, in this order:

  1. Take an input of 4 variables for location, date, subject, and name of directory to use
  2. Open a mysql connection
  3. Handle a zip file
    1) Upload the zip file
    2) Unzip the file
    3) Save the photos in the file to the specified directory
    4) Save the url of each photo, as well some other info about the photo, to a mysql database.
  4. Close the mysql connection
  5. Confirm that everything functioned correctly.
  6. Present the user the opportunity to upload another separate zip file with different variables.

As of right now, I’m able to get most of this done correctly. However, something is off about the structure of my code, and I’m not sure how to modify it. When I upload the first set, it works fine. However, if I try to upload a second set, a couple of things happen. The photos that have been uploaded so far end up getting entered into the mysql database again for each time that I try to upload another set.

Can anyone tell me how to correct this so that each individual image is inserted into the database once and only once?

<?php  // actual code for upload
$dirname = $_REQUEST['dirname'];
$taken = $_REQUEST['taken'];
$location = $_REQUEST['location'];
$subject = $_REQUEST['subject'];
$urldirectory = $_REQUEST['urldirectory'];

if(!(file_exists($dirname) && is_dir($dirname))) { // confirm that the directory exists, and that it is a directory
mkdir($dirname, 0777);
echo "the directory will be called ".$dirname;
} else {
echo "directory: " . $dirname;
} 

if($_FILES["zip_file"]["name"]) { // pull the nmae of the zip file from the upload
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];

    $name = explode(".", $filename); //format the filename for a variable
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
            if($mime_type == $type) {
                    $okay = true;
                    break;
            } 
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false; // let user know if the zip file has not been uploaded
    if(!$continue) {
                $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }
        $target_path = $dirname."/".$name; // get the $target_path variable to for the move_uploaded_file() function.

        if(move_uploaded_file($source, $target_path)) { // this block extracts the zip files and moves them to the $dirname directory

            $zip = new ZipArchive();
            $x = $zip->open($target_path);
            if ($x === true) {
                    $zip->extractTo($dirname."/"); 
                    $zip->close();
                    unlink($target_path);
            }

                    $message = "Your .zip file was uploaded and unpacked.";
                    //use glob to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
                    require_once 'connect.php';
                    echo '<html>'; 


                    echo '<html>'; 

                    $images = array(); // this clears the array by initializing between each reload of the page. Without this, each separate folder being uploaded would accumulate in the array and be uploaded multiple times. 
                    $images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database

                    foreach ($images as $value) {
                        if ($value!='.' && $value!='..' && $subjecttest=$subject) {
/*                                $url = trim($urldirectory)."/".trim($value);*/
                            echo '<img src="http://www.example.com/temp/' . $dirname . '/' . $value . '"< /img>';
                            $url = trim('http://www.example.com/temp/') . trim($dirname) . '/' . trim($value);
                            $insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('$taken', '$location', '$subject' , '$url');";

                                    if (mysql_query($insert_sql)) { 
                                                    echo "$value"." inserted successfully!";
                                            } else {        
                                                    echo "$value"." not inserted";
                                                    echo $insert_sql . '<BR>' . mysql_error();
                                            }

                                            } else {
                                                echo 'Please use unique info for each upload set'
                                            }

                                    unset($images); // destroys the $images variable, so it doesn't accumulate the next time you upload another folder. 
                                        }
                                        }
                            echo '</html>';


                                    } else {        
                    $message = "There was a problem with the upload. Please try again.";
            }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</p>";
if($subject) echo "<p>subject: " . $subject  . "</p>";
if($location) echo "<p>location: " . $location . "</p>";
?>
<form enctype="multipart/form-data" method="post" action="upload2.php"> 
    <label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<input type=hidden name="urldirectory" value="<?php echo "http://www.example.com/temp/".'$dirname;' ?>" />
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:06:12+00:00Added an answer on June 15, 2026 at 10:06 pm

    I believe the source of your problem is here (line 53 and 54 by my count):

    $images = array(); // this clears the array by initializing between each reload of the page. Without this, each separate folder being uploaded would accumulate in the array and be uploaded multiple times. 
    $images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
    

    By using scandir, you retrieve not only the images you’ve just uploaded, but all previously uploaded images as well. Instead, you can read the filenames directly from the zip archive, in a manner similar to this:

    $new_names = array();
    for ($i=0; $i<$zip->numFiles; $i++) {
        $new_names[] = $zip->getNameIndex($i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP upload script and am trying to make it so that
I currently have a script that I am trying to use to access an
I have a script that I'm trying to get working. Basically, what I'm trying
I have a python script that is trying to create a directory tree dynamically
I have a custom accordion script that I'm trying to adapt for another site.
I have a simple jQuery script that I'm trying to build upon but I
I have a batch script that outputs a file, and I'm trying to ensure
I have this script that run to fix my menu bar to the browser
I am trying to use regular expressions to make my inventory upload script a
I am trying to use R within a script that will act as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.