Page 1 of 1

need help if you dont mind

Posted: Sun Jun 22, 2008 12:52 am
by theebrettroberts2
Okay, I am pretty new to php. But hey everyone starts off somewhere. I am creating a multiple file upload and have 2 php files. 1) testfield.php and 2)testfield2.php. testfield.php logs into the sql database sends a query to it and grabs how many images is needed. see here.

Code: Select all

 
<?php
// Database connection information
include ("connect.php");
$sql="SELECT offername,imgnum from Offers WHERE offername='hoobaskan2'"; //gettting no of images
$result= mysql_query($sql);//getting result
while($row = mysql_fetch_array( $result )) {
$max_no_img=$row['imgnum'];  // Maximum number of images value to be set here
}
$uploadNeed=$max_no_img;
//Lists mysql fields
$fields = mysql_list_fields("TDCD", "hoobaskan2");
//counts the number of fields
$columns = mysql_num_fields($fields);
// creates the table
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
//creats the form
echo "<form method=post action=testfield2.php enctype='multipart/form-data'>";
//displays the fields
for ($i = 5; $i < $columns; $i++) {
echo "<tr><td>". mysql_field_name($fields, $i)."</td><td><input name=".$i. "' type='text'><br>";
}
//dispalys the image creation loop
for($x=; $x<=$max_no_img; $x++){
echo "<tr><td>Image $x</td><td>";
?>
 
<input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>"> </td></tr>
  </p>
  <?
  // end of for loop
  }
  ?>
  <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
      <input name="OffName" type="hidden" value ="hoobaskan2">;
  <tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>
  </p>
</form>
</table>
</body>
</html>
 
then testfield2.php saves the image files. as shown. the problem is testfield2.php only saves 2 images even if 7 are set.

Code: Select all

 
<?
$dir ="offers/";
$dir = $dir.$_POST['OffName'];
$dir2=$dir."/uploads/";
$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=1;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$dir2 .$file_name);
 // check if successfully copied
 if($copy){
 echo "$file_name | uploaded sucessfully!<br>";
 }else{
 echo "$file_name | could not be uploaded!<br>";
 }
} // end of loop
?>
 

Re: need help if you dont mind

Posted: Sun Jun 22, 2008 3:07 am
by Kieran Huggins
A neat trick: You could specify the file input field names as an array like so:

Code: Select all

<input type="file" name="files[]"/>
<input type="file" name="files[]"/>
<input type="file" name="files[]"/>
Then iterate over them thusly:

Code: Select all

while(count($_POST['files'])>0){
  $file = array_shift($_POST['files']);
  // process the file upload
}

Re: need help if you dont mind

Posted: Sun Jun 22, 2008 12:13 pm
by califdon
Please do not use vague, general Subjects when you post in a forum. Many of the people who might be able to help you will deliberately avoid reading messages that do not specify what sort of help they are seeking. Please return to your original post and edit the Subject to something that indicates what you're having trouble with.

Thank you.