i am trying to do multiple photo upload but its not working,i can just upload just one image at a time
here is my code.I am looking for uploading multiple image using multiple file input type and a single submit button.

<html>
<body>
<form action="imgupsuc.php" method="post" enctype="multipart/form-data">
<?php
    @$nm=$_POST['n1'];
?>
<input type=hidden name=n1  value=<?php echo $nm ?>>
Select a picture<input type="file" name="file" size="50">
Select a picture<input type="file" name="file" size="50"><input type="submit" value="upload">

</form>
</body>
</html>

imgupsuc.php

<?php
    session_start();
    include "connect.php";
?>
<?php
    $name=$_POST['n1'];
    move_uploaded_file($_FILES['file']['tmp_name'],"./pic/".$_SESSION['userid']."".$_FILES['file']['name']);
    $q="insert into image values('".$name."','".$_SESSION['userid']."".$_FILES['file']['name']."','".$_SESSION['userid']."')";
    $s=mysql_query($q);
    if($s>0)
        echo "<h1> pic insert successfully";
    mysql_close();
?><li>Go Back to Choose another photo to upload</li><b>or</b>
<a href="view.php">View Album</a>Or <a href="fileup.php"> Upload Another Image</a>

Dani AI

Generated

Short diagnosis: ' form used plain file inputs named file and the handler treated $_FILES['file'] as a single upload, so only one image gets processed. The usual fix is to give the inputs an array name (or a single input with the multiple attribute) and loop the $_FILES arrays on the server. Also avoid the old mysql_* functions — use PDO or mysqli with prepared statements.

Example form (single multi-select input):

<form action="imgupsuc.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="n1" value="<?php echo htmlspecialchars($nm ?? ''); ?>">
  Select pictures: <input type="file" name="images[]" multiple accept="image/*">
  <input type="submit" value="Upload">
</form>

Server-side outline (validate, rename, move, insert via PDO):

<?php
session_start();
// PDO connection...
$allowed = ['image/jpeg','image/png','image/gif'];
$uploadDir = __DIR__ . '/pic/';
if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);

foreach ($_FILES['images']['error'] as $i => $err) {
    if ($err !== UPLOAD_ERR_OK) continue;
    $tmp = $_FILES['images']['tmp_name'][$i];
    $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $tmp);
    if (!in_array($mime, $allowed)) continue;
    $ext = pathinfo($_FILES['images']['name'][$i], PATHINFO_EXTENSION);
    $name = $_SESSION['userid'] . '_' . uniqid() . '.' . $ext;
    if (move_uploaded_file($tmp, $uploadDir . $name)) {
        $stmt = $pdo->prepare("INSERT INTO image (name, path, userid) VALUES (?, ?, ?)");
        $stmt->execute([$_POST['n1'], 'pic/'.$name, $_SESSION['userid']]);
    }
}

Practical notes and troubleshooting:

  • Check enctype="multipart/form-data", post_max_size, upload_max_filesize and max_file_uploads in php.ini.
  • Validate MIME/type (finfo or getimagesize), limit file size, and generate unique filenames to avoid collisions.
  • Make the upload directory non-executable (disable PHP execution there) and ensure webserver user can write to it.
  • For large or many files, consider an HTML5/AJAX approach with progress feedback (as suggested).
  • While debugging use var_dump($_FILES) to inspect the structure and errors.

For similar implementations and edge cases see other threads (as suggested) — the change to name="images[]" and looping on the server is the key fix.

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.