Jump to content


Photo

foreach but no spesific field...


  • Please log in to reply
3 replies to this topic

#1 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 19 October 2006 - 02:08 PM

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

<?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();
?>

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

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 19 October 2006 - 02:18 PM

You need to change the name and refrence the files as an array!

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();
}
?>

The your form would be...

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

me!

#3 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 26 October 2006 - 10:26 AM

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.

<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);

}} } 

?>

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

Please help.

#4 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 30 October 2006 - 09:39 AM

help please




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users