Jump to content

Looping the UPLOAD process


bilis_money

Recommended Posts

hi, these are my codes.

[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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;
        }
        -->
    </style>
</head>

<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>
    </pre>

<?php
    $uploadDir = $_SERVER['DOCUMENT_ROOT'].'/mygallery/upload/images/';
    if(isset($_POST['upload']))
    {
            $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";
        exit;
    }

    include 'library/opendb.php';

    if(!get_magic_quotes_gpc())
    {
        $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">
<table>
<pre>
<tr><td>
<strong>Image 1.</strong>
    <input name="userfile1" type="file" class="box" id="userfile1" size="50">
</td></tr>
<tr><td>
    <strong>Image 2.</strong>
        <input name="userfile2" type="file" class="box" id="userfile2" size="50">

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

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

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

</pre>
<pre>
<tr><td><center>
    <input name="upload" type="submit" id="upload" value="  Upload  ">
</center></td></tr>
</pre>
</table>
</form>


</div>
</body>
</html>
[/code]

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.




Link to comment
Share on other sites

first change userfile1-5 to userfile_1-5

[code]
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
}
}
[/code]
this what u wanted?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.