Page 1 of 1

foreach file and text

Posted: Mon Jun 23, 2003 2:08 pm
by stc7outlaw
I making an upload script so a user can upload more than 1 file to the database. I was able to make it upload 1 file and now I am using a loop sequence to use an infinite amount of file uploads if needed. I can get it to do more than 1 file upload but each file upload includes a description. This is where I run into a problem.

I need to find a way to include the file and the description in the same row of the table. I am inches from getting there but I keeping getting the parse error:

Code: Select all

Parse error: parse error, expecting `')'' in /home/oprods/www.oprods.com/html/php/db_binary_up/store.php on line 25
I must be missing something but I'm not sure. Check the code here, its pretty straight forward:

Code: Select all

<?php

// this is used
// to upload to mysql database
// more than 1 file at a time with a description and other assortments
?>

<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>

<?php
// code that will be executed if the form has been submitted:

$user = 'blahuser';
$pass = 'blahpass';
$db  = 'binary_data';


if ($submit) {

    // connect to the database
    // (you may have to adjust the hostname,username or password)

  foreach($_FILES[form_data] as $file_name => $file_array  &&  $_POST[description] as $more_descriptions => 

$description_array) { //create loop for many file uploads


    MYSQL_CONNECT("localhost",$user,$pass);
    mysql_select_db($db);

    $data = $file_array[name];
    $form_description = $description_array;

    $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
        "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

    $id= mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";

    MYSQL_CLOSE();

  }
} else {

    // else show the form to submit new data:
?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="description[0]"  size="40">
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="2000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data[0]"  size="40">
    File Description:<br>
    <input type="text" name="description[1]"  size="40">
    <input type="file" name="form_data[1]"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php

}

?>

</BODY>
</HTML>

Posted: Mon Jun 23, 2003 2:53 pm
by twigletmac
Basically you can't use a foreach loop like that, take a look at:
http://php.net/manual/en/control-structures.foreach.php

You're going to have to think a bit more about how you get the information from the arrays.

Plus you need to read this:
Array do's and don'ts - Why is $foo[bar] wrong?

Mac

Posted: Mon Jun 23, 2003 3:39 pm
by stc7outlaw
Do i need to find a whole new way of using a loop or do i need to just move the if ($submit) statement inside my foreach statement?