Jump to content


Photo

Looping the UPLOAD process


  • Please log in to reply
2 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 12 June 2006 - 02:25 PM

hi, these are my codes.

<!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>

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.






#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 12 June 2006 - 02:32 PM

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?
z..z..z..z..z..z..z..z..

#3 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 12 June 2006 - 02:35 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users