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 8641045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:25:35+00:00 2026-06-12T11:25:35+00:00

Possible Duplicate: multi image upload wrong quantity on file-upload Hey so I have a

  • 0

Possible Duplicate:
multi image upload wrong quantity on file-upload

Hey so I have a php script that uploads one file to the server, how can I change the code to allow for multiple files to be uploaded at the same time. before people start linking to other questions, I know how to search stackoverflow and google, however the answers I have found by searching I cannot figure out how to apply to my code. my code follows:

<?php
session_start();
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists($_SESSION['user']."/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"][$i],
  $_SESSION['user']."/" . $_FILES["file"]["name"]);
  echo "Stored in: " . $_SESSION['user']."/" . $_FILES["file"]["name"];

  }
}



else
  {
  echo "Invalid file";
  }

?> 
  • 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-12T11:25:37+00:00Added an answer on June 12, 2026 at 11:25 am

    This is an advanced file uploading system I wrote it some years ago:

    HTML:

    <form action="upload.php" method="post" enctype="multipart/form-data" >
        <input type="file" name="myfile[]" /><br>
        <input type="file" name="myfile[]" /><br>
        <input type="file" name="myfile[]" /><br>
        <!-- MORE AND MORE -->
        <input type="submit" name="submit" value="Upload" />
    </form>
    

    PHP:

    <?php // upload.php
    // Set timezone for probable usage.
    date_default_timezone_set('Asia/Tehran');
    
    // Assign valid types
    $valid_mime = array(
        'image/jpeg',
        'image/pjpeg',
        'image/jpeg',            
        'image/png',
        'image/gif'
    );
    
    function upload($files, $dir, $size_limit=1024, $prevent_duplicate=false){
        global $valid_mime;
    
        // $files must be given.
        if(!isset($files)) return false;
    
        // Look for $valid_mime array.
        isset($valid_mime) and is_array($valid_mime) or die('Error in data resources, valid_mime array not found.');
    
        // Make directory if not exists. set permission to 0777.
        is_dir($dir) and chmod($dir, 0777) or mkdir($dir, 0777, true);
    
        $count = 1;
        foreach($files as $file){
            $file['error'] === UPLOAD_ERR_OK or die('Error in uploading file(s).');
    
            // Check uploaded-file type.
            in_array($file['type'], $valid_mime) or die();
    
            // Set size_limit in KB.
            $file['size'] > $size_limit*1024 and die('The uploaded file exceeds the maximum file size.');
    
            // Prevent duplicate filenames.
            $prefix = ($prevent_duplicate == true) ? time().'_' : '';
            $suffix = ($prevent_duplicate == true) ? '_'.$count++ : '';
    
            $file_extension = strrchr($file['name'], '.');
            $filename = basename($file['name'], $file_extension);
    
            $file_path = "{$dir}/{$prefix}{$filename}{$suffix}{$file_extension}";
    
            // Move uploaded-file from php temp folder to desire one.
            move_uploaded_file($file["tmp_name"], $file_path);
    
            // Make an array of filepaths
            $output[] = $file_path;
        }
    
        // Change permission of folder according to security issues.
        chmod($dir, 0755);
    
        return $output; 
    }
    /////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////  Controller Section  ////////////////////////////////
    
    // Assign tmp_arr from $_FILES['myfile'] and do die if there is any problem.
    $tmp_arr = (isset($_POST['submit']) and isset($_FILES['myfile'])) ? $_FILES['myfile'] : die('Error in posting data.');
    
    // Create an array with desired structure.
    for($i=0; $i<count($tmp_arr['name']); $i++){
        $files[] = array(
            'name'      =>  $tmp_arr['name'][$i],
            'type'      =>  $tmp_arr['type'][$i],
            'tmp_name'  =>  $tmp_arr['tmp_name'][$i],
            'error'     =>  $tmp_arr['error'][$i],
            'size'      =>  $tmp_arr['size'][$i],
        );
    }
    
    // size_limit in KB
    $path_arr = upload($files, './public', 1024, true);
    
    // SEE WHAT HAPPENS ;)
    echo '<pre>';
    var_export($path_arr);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can one use multi threading in php applications Does PHP have
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
Possible Duplicate: How to “flatten” a multi-dimensional array to simple one in PHP? How
Possible Duplicate: How can one use multi threading in php applications Does anybody know
My PHP script have to create a multi-tabs Excel file with a report in
Possible Duplicate: Is Sinatra multi threaded? I have a web service that requires running
Possible Duplicate: Sorting a multidimensional array? I have an a multi dimesional associative array:
Possible Duplicate: nullPointerException in multi column list I have following app which fetches data

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.