Plll?Help about a multifielded upload form

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
novaks
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 9:34 am

Plll?Help about a multifielded upload form

Post by novaks »

I made a multifielded upload form, it?s working but when I want to upload only a 5.files (but not all 14.files) it?s not working.

Help help help for this...

Code: Select all

upload.htm

<html><head><title>OBUR</title></head><body> 
<form method="post" action="upload.php" enctype="multipart/form-data"> 
01. <input name="userfile&#1111;]" type="file"> <br>
02. <input name="userfile&#1111;]" type="file"> <br>
03. <input name="userfile&#1111;]" type="file"> <br>
04. <input name="userfile&#1111;]" type="file"> <br>
05. <input name="userfile&#1111;]" type="file"> <br>
06. <input name="userfile&#1111;]" type="file"> <br>
07. <input name="userfile&#1111;]" type="file"> <br>
08. <input name="userfile&#1111;]" type="file"> <br>
09. <input name="userfile&#1111;]" type="file"> <br>
10. <input name="userfile&#1111;]" type="file"> <br>
11. <input name="userfile&#1111;]" type="file"> <br>
12. <input name="userfile&#1111;]" type="file"> <br>
13. <input name="userfile&#1111;]" type="file"> <br>
14. <input name="userfile&#1111;]" type="file"> <br>
<input type="submit" name="addpicture" value="Upload" > 
</form>

upload.php
<?
if (!empty($addpicture)) &#123;
$prefix = time();
$_FILES&#1111;'userfile']&#1111;'name']&#1111;0] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;0];
$dest0 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;0];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;0], $dest0);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;1] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;1];
$dest1 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;1];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;1], $dest1);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;2] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;2];
$dest2 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;2];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;2], $dest2);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;3] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;3];
$dest3 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;3];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;3], $dest3);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;4] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;4];
$dest4 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;4];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;4], $dest4);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;5] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;5];
$dest5 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;5];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;5], $dest5);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;6] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;6];
$dest6 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;6];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;6], $dest6);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;7] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;7];
$dest7 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;7];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;7], $dest7);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;8] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;8];
$dest8 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;8];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;8], $dest8);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;9] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;9];
$dest9 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;9];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;9], $dest9);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;10] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;10];
$dest10= $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;10];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;10], $dest10);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;11] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;11];
$dest11 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;11];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;11], $dest11);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;12] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;12];
$dest12 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;12];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;12], $dest12);
$_FILES&#1111;'userfile']&#1111;'name']&#1111;13] = $prefix.$_FILES&#1111;'userfile']&#1111;'name']&#1111;13];
$dest13 = $PicturePath.$_FILES&#1111;'userfile']&#1111;'name']&#1111;13];
move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']&#1111;13], $dest13); 
$db->add_Picture($id,$fail0,$fail1,$fail2,$fail3,$fail4 ,$fail5,$fail6,$fail7,$fail8,$fail9,$fail10,$fail1 1,$fail12,$fail13);

&#125;
?>
novaks
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 9:34 am

I dont belief...

Post by novaks »

It's possible neither of you knows what next?:?:

I dont belief....this is the php forum...I'm trying over a week.

Please help me.:cry:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, i've no idea where you are getting/setting those $fail values, but you can do the folowing for moving the uploaded files.

Code: Select all

<?php
if (!empty($_FILES)) {
  $prefix = time();
  $count = 0;
  foreach($_FILES as $uploaded){
    if(!empty($uploaded['name'][$count])){
      $uploaded['name'][$count] = $prefix.$uploaded['name'][$count];
      $dest[] = $PicturePath.$uploaded['name'][$count];
      move_uploaded_file($uploaded['tmp_name'][$count], end($dest));
    }
    $count++;
  }

//no idea what the following is supposed to do
//or where those $fail values get set
/*
$db->add_Picture($id,$fail0,$fail1,$fail2,$fail3,$fail4 ,$fail5,$fail6,$fail7,$fail8,$fail9,$fail10,$fail1 1,$fail12,$fail13);
*/

}
?>
novaks
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 9:34 am

Post by novaks »

I got 2 function

1) is...

Code: Select all

<?php

function add_Picture($id,$fail0,$fail1,$fail2,$fail3,$fail4 ,$fail5,$fail6,$fail7,$fail8,$fail9,$fail10,$fail1 1,$fail12,$fail13)
   {      
      $failavards0 = $_FILES['userfile']['name'][0];
	  $fail1 = $_FILES['userfile']['name'][1];
	  $fail2 = $_FILES['userfile']['name'][2];
	  $fail3 = $_FILES['userfile']['name'][3];
	  $fail4 = $_FILES['userfile']['name'][4];
	  $fail5 = $_FILES['userfile']['name'][5];
	  $fail6 = $_FILES['userfile']['name'][6];
	  $fail7 = $_FILES['userfile']['name'][7];
	  $fail8 = $_FILES['userfile']['name'][8];
	  $fail9 = $_FILES['userfile']['name'][9];
	  $fail10 = $_FILES['userfile']['name'][10];
	  $fail11 = $_FILES['userfile']['name'][11];
	  $fail12 = $_FILES['userfile']['name'][12];
	  $fail13 = $_FILES['userfile']['name'][13];
	  $sql = "insert into picture (id,nosaukums,apraksts,serijasnumurs,bilde,tehniskieparametri,funkcijasizskaidorjumi,tehniskiedati,rasejums,pielietosana,ipreitings,stativs,papildaprikojums,spc,pdf) values ('$id','$fail0','$fail1','$fail2','$fail3','$fail4','$fail5','$fail6','$fail7','$fails8','$fail9','$fail10','$fail11','$fail12','$fail13')";
      $result = $this->insert($sql);
      return $result;
   }

?>
2) is...

Code: Select all

<?php

function insert($sql="")
   {
      if (empty($sql)) return false;
      if (empty($this->CONN)) return false;

      $conn = $this->CONN;
      $results = mysql_query($sql,$conn);
      if (!$results) return false;
      $results = mysql_insert_id();
      return $results;
   }

?>
Hmmm ..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well ... that's not too great, as the add_Picture function requires 14 pictures, it would be much better if it accepted an array of values, then it could handle any amount of images and not be restricted to 14 hard coded images ... imho :o
novaks
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 9:34 am

Post by novaks »

As you see I'm not a pro ... onli a biginer 8O ... end i don't know-how
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'd just move the sql stuff into the same code that loops around the $_FILES array, ie i wouldn't bother with the add_Picture or insert functions. Is the idea of the code just to 'upload a variable number of images, move them to a new directory and rename them, and insert the names into a database'? I ask because it looks like each picture is 'specific' as you're inserting each one into a separate database field.
novaks
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 9:34 am

Post by novaks »

Yes idea of the code is just 'upload a variable number of images, move them to a new directory and rename them, and insert the names into a database'. And yes each picture is 'specific' because I inserting each picture into a separate database field.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'd change the html form so that each file field matches the specifc image type it should contain, rather than use userfile[]. Look at the first user comment at http://www.php.net/manual/en/features.f ... ltiple.php for an example of how to name each field so it has a unique name. This way you can then match the uploaded images to the database fields they should be going into. So you want to do something like :
1. Loop over the $_FILES array
2. Do your moving and renaming
3. Do 1 insert for each field (or dynamically build up a query and do it in one)

If you are still stuck with that then let us know and someone might whip up some example code for you if you're lucky ;)
Post Reply