Jump to content

Archived

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

GremlinP1R

foreach but no spesific field...

Recommended Posts

Hi there.

I'm trying to upload a few files, now every thing is working fine for one file....
but how do I loop it for the rest??

[code]<?php
// When Submitting
  if (isset($_POST['Submit'])) {
  include('uploadclass.php');
 
  // Set upload info
      $upload_class = new Upload_Files;
  $upload_class -> temp_file_name = trim($_FILES['pic1']['tmp_name']);
  $filename = $name;
  $filename .= $nickname;
  $filename .= trim(strtolower($_FILES['pic1']['name']));
  $var = explode('.',$filename);
      $extension = $var[count($var)-1];
      if ($extension != jpg){ echo" file type";}
  else {
  $upload_class -> file_name = $filename;
  $upload_class -> upload_dir = "images/cars/";
  $upload_class -> upload_log_dir = "images/uploadlog/";
  $upload_class -> max_file_size = 50000000;
  $upload_class -> banned_array = array(" ");
  $upload_class -> ext_array = array("%.jpg");
 
 
  // Calling class Functions
//      $valid_ext = $upload_class -> validate_extension();
  $valid_size = $upload_class -> validate_size();
  $valid_user = $upload_class -> validate_user();
  $max_size = $upload_class -> get_max_size();
  $file_size = $upload_class -> get_file_size();
  $upload_directory = $upload_class -> get_upload_directory();
  $upload_log_directory = $upload_class -> get_upload_log_directory();
  $upload_file = $upload_class -> upload_file_no_validation();
?>[/code]

Thats the code that sould get looped, my field names are pic1, pic2, pic3 and pic4....

No to use foreach() I deed one field name not?

Help please...

Share this post


Link to post
Share on other sites
You need to change the name and refrence the files as an array!

[code]for ( $i = 0; $i < sizeof ( $_FILES['pic'] ); $i++ )
{
          $upload_class = new Upload_Files;
  $upload_class -> temp_file_name = trim($_FILES['pic']['tmp_name'][$i]);
  $filename = $name;
  $filename .= $nickname;
  $filename .= trim(strtolower($_FILES['pic']['name'][$i]));
  $var = explode('.',$filename);
      $extension = $var[count($var)-1];
      if ($extension != jpg){ echo" file type";}
  else {
  $upload_class -> file_name = $filename;
  $upload_class -> upload_dir = "images/cars/";
  $upload_class -> upload_log_dir = "images/uploadlog/";
  $upload_class -> max_file_size = 50000000;
  $upload_class -> banned_array = array(" ");
  $upload_class -> ext_array = array("%.jpg");
 
 
  // Calling class Functions
//      $valid_ext = $upload_class -> validate_extension();
  $valid_size = $upload_class -> validate_size();
  $valid_user = $upload_class -> validate_user();
  $max_size = $upload_class -> get_max_size();
  $file_size = $upload_class -> get_file_size();
  $upload_directory = $upload_class -> get_upload_directory();
  $upload_log_directory = $upload_class -> get_upload_log_directory();
  $upload_file = $upload_class -> upload_file_no_validation();
}
?>[/code]

The your form would be...

[code]<input type='file' name='pic[]' size='32' />
<input type='file' name='pic[]' size='32' />
<input type='file' name='pic[]' size='32' />
<input type='file' name='pic[]' size='32' />[/code]

me!

Share this post


Link to post
Share on other sites
Uhmmm Help again Please....

That is working 100% just have one small problem now with it.
Its saving the files where it sould and it names them all as it sould but...
In my data base it also have to save the name and currently its only saveing the first  ones name in all the fields.

[code]<php

// When Submitting
  if (isset($_POST['Submit'])) {
  include('uploadclass.php');
 
 
  // Set upload info
      for ( $i = 0; $i < sizeof ( $_FILES['pic'] ); $i++ )
{
      $upload_class = new Upload_Files;
  $upload_class -> temp_file_name = trim($_FILES['pic']['tmp_name'][$i]);
  $filename = $name;
  $filename .= $nickname;
  $filename .= trim(strtolower($_FILES['pic']['name'][$i]));
  $var = explode('.',$filename);
      $extension = $var[count($var)-1];
      if (($extension != jpg) and ($extension != gif) and ($extension != bmp) and ($extension != png)){ echo" file type";}
  else {
  $upload_class -> file_name = $filename;
  $upload_class -> upload_dir = "images/cars/";
  $upload_class -> upload_log_dir = "images/uploadlog/";
  $upload_class -> max_file_size = 50000000;
  $upload_class -> banned_array = array(" ");
  $upload_class -> ext_array = array("%.jpg");
 
 
  // Calling class Functions
//      $valid_ext = $upload_class -> validate_extension();
  $valid_size = $upload_class -> validate_size();
  $valid_user = $upload_class -> validate_user();
  $max_size = $upload_class -> get_max_size();
  $file_size = $upload_class -> get_file_size();
  $upload_directory = $upload_class -> get_upload_directory();
  $upload_log_directory = $upload_class -> get_upload_log_directory();
  $upload_file = $upload_class -> upload_file_no_validation();

  // Conecting to database
      $result = mysql_query("SELECT * FROM database")
                or die(mysql_error());

      $sql = "INSERT INTO table (`ID` , `Date` , `Name` , `Surname` , `NickName` , `Bdate` , `Residence` , `Occupation` , `Cell` , `CarMake` , `CarModel` , `Registration` , `Motor` ,                                    `Body` , `Interior` , `Sound` , `Pic1` , `Pic2` , `Pic3` , `Pic4` , `Active` )

              VALUES ( NULL , NOW( ) , '$name', '$surname', '$nickname', '$bdate', '$residence', '$occupation', '$cell', '$carmake', '$carmodel', '$registration', '$motor', '$body',
              '$interior', '$sound', '$filename', '$filename', '$filename', '$filename', '0')";

  // Submit data to database 
  $result = mysql_query($sql) or die (mysql_error());
  $count = ($result);

}} }

?>[/code]

The $filename works fine for saving the file but I'm doing something wrong to save the file name in the database.....

Please help.

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.