Jump to content
Updating IPB tonight Read more... ×


This topic is now archived and is closed to further replies.


Looping the UPLOAD process

Recommended Posts

hi, these are my codes.

[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
        .box {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            border: 2px solid #000000;

<body bgcolor="#999999" text="#FFFFCC" leftmargin="0" topmargin="0">
<div align="center">
  <pre><font face="Georgia, Times New Roman, Times, serif"><strong>
Uploading image      files</strong></font>

    $uploadDir = $_SERVER['DOCUMENT_ROOT'].'/mygallery/upload/images/';
            $fileName = $_FILES['userfile1']['name'];
            $tmpName  = $_FILES['userfile1']['tmp_name'];
            $fileSize = $_FILES['userfile1']['size'];
            $fileType = $_FILES['userfile1']['type'];

    // the files will be saved in filePath
    $filePath = $uploadDir . $fileName;

    // move the files to the specified directory
    // if the upload directory is not writable or
    // something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file";

    include 'library/opendb.php';

        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);

    $query = "INSERT INTO upload2 (name, size, type, path ) ".
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

    mysql_query($query) or die('Error, query failed : ' . mysql_error());

    include 'library/closedb.php';

    echo "<br>File uploaded<br>";


<form action="" method="post" enctype="multipart/form-data" name="uploadform">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<strong>Image 1.</strong>
    <input name="userfile1" type="file" class="box" id="userfile1" size="50">
    <strong>Image 2.</strong>
        <input name="userfile2" type="file" class="box" id="userfile2" size="50">

    <strong>Image 3.</strong>
        <input name="userfile3" type="file" class="box" id="userfile3" size="50">

    <strong>Image 4.</strong>
        <input name="userfile4" type="file" class="box" id="userfile4" size="50">

    <strong>Image 5.</strong>
        <input name="userfile5" type="file" class="box" id="userfile4" size="50">

    <input name="upload" type="submit" id="upload" value="  Upload  ">


what the codes does is to just upload files, very simple really.
actually it has no error and it works very fine, as you notice only USERFILE1 is currently active and working
and not userfile2, userfile3 and soon.

what i want to incorporate here is to make USERFILE2, 3, 4 and 5 active using looping while make the codes
stay short.

now, do i need to use while loop here? or for loop here? or should i create a function to do this or array?
or else Class?

i have no nice idea right now to make this happen, so i hope you help me with this.

maybe a bright simple snippet of codes to show me on how to do this is very helpful?

Thank you very much in advance.

Share this post

Link to post
Share on other sites
first change userfile1-5 to userfile_1-5

for ($_POST as $name => $value)
if (preg_match ("/userfile_[1-5]{1}/", $name))
  $name = explode ("_", $name);
  $num = $name[1];
  $file = $value; //do stuff with these values
this what u wanted?

Share this post

Link to post
Share on other sites
ok, thanks i'll try to figure out that.
but if you still have cool logics other than this will be also appreciated.

thanks in advance.

Share this post

Link to post
Share on other sites


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.