Jump to content


Photo

for each loop?


  • Please log in to reply
11 replies to this topic

#1 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 26 August 2006 - 05:19 PM

It seems like Ive done this before,.. But I cant quite remember:

I have an upload script with ten upload fields,.. I have a script to upload the first file, thumbnail it, & put both paths in a database. Now I need to do the same thing to each upload field. I could type the code out manual,.. but that would such a pain and not very efficient. here is the code I have:

//validate file 1
function filecheck($filenamea){
   $ext = strrchr($filenamea,'.');
   $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");
   if ( in_array ($ext, $FileType) )
     return TRUE;
   else
     return FALSE;
}

if (filecheck($filenamea) == FALSE )
   die ('Image 1 is not of an accepted type'); 
   
//if the temp file1 is there copy it to the server 
if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { 
copy($_FILES["file"]["tmp_name"], "/home/rack/public_html/project/jobs/$dir2/return_images/" . $_FILES["file"]["name"]); 
echo "<br>"; 
}  
 

//get image size and name
$im_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/" . $filenamea;
$image_attribs = getimagesize($im_file_name); 
$im_old = imageCreateFromJpeg($im_file_name); 


//declare thumb
$th_max_width = 100; 
$th_max_height = 100; 
$ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; 
$th_width = $image_attribs[0] * $ratio; 
$th_height = $image_attribs[1] * $ratio; 
$im_new = imagecreatetruecolor($th_width,$th_height); 
imageAntiAlias($im_new,true); 

//resize image
$th_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/thumbs/" . $filenamea;
imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); 
imageJpeg($im_new,$th_file_name,100);




$mysql = "my database";
mysql_select_db($mysql);
$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$fullpath', '$thumbnailpath', '$job', '$direc', NULL, '1');";

mysql_query($sql) or die('Error, insert query failed');

Im thinking I ned to set the upload fields as an array,. and then do some kinda foreach loop. But Im pretty lost. If this not enough of the code please tell me. And I do some wierd things in my code, so dont blast me to much. Any help would be great,.. thanks

BTW,.. about 3 weeks back I posted a question about uploading files via a Windows based smart phone. It turned out the problem was a security setting on the server. So if you have any problems with that,.. look there first....Thanks

#2 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 27 August 2006 - 02:35 PM

bump

#3 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 27 August 2006 - 05:28 PM

Im thinking I ned to set the upload fields as an array,. and then do some kinda foreach loop.

That's how you would do it. what part were you neading help with?

#4 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 27 August 2006 - 07:08 PM

well the order I suppose,.. I have started on the script,..but depending on how I arrange it,.. I can upload 1 file,.. then 2.. its wierd,.. this is what I have so far:

foreach($_POST['textfield'] as $value) { 
$check_msg .= "Checked: $value\n"; 
}
echo $check_msg;
foreach($_FILES['file']['name'] as $value2) {
$filename .= "Image Name: $value2\n";
}
echo $filename;


function filecheck($value2){
   $ext = strrchr($value2,'.');
   $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");
   if ( in_array ($ext, $FileType) )
     return TRUE;
   else
     return FALSE;
}
foreach($_FILES["file"]["tmp_name"] as $value){

//validate file 1


if (filecheck($value2) == FALSE )
   die ('Image 1 is not of an accepted type'); 
//if the temp file1 is there copy it to the server 
if (@is_uploaded_file($value)) { 
copy($value, "/home/rack/public_html/project/jobs/test_job/" .$value2); 
echo "<br>success"; 
}else {
echo "<br>Not Gonna Happen<br>";

//get image size and name
$im_file_name = "/home/rack/public_html/project/jobs/test_job/" . $value2;
$image_attribs = getimagesize($im_file_name); 
$im_old = imageCreateFromJpeg($im_file_name); 

//declare thumb
$th_max_width = 100; 
$th_max_height = 100; 
$ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; 
$th_width = $image_attribs[0] * $ratio; 
$th_height = $image_attribs[1] * $ratio; 
$im_new = imagecreatetruecolor($th_width,$th_height); 
imageAntiAlias($im_new,true); 

//resize image
$th_file_name = "/home/rack/public_html/project/jobs/test_job/thumb/" . $value2;
imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); 
imageJpeg($im_new,$th_file_name,100);
}
  }

foreach ($_FILES['file']['name'] as $value3) {
$pathname .= "<br>$path$value3<br>";
}
echo $pathname;


with this being my form:

<form action="fortest2.php" method="post" enctype="multipart/form-data" name="form1">
  <table width="247" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th width="247" scope="col"><input name="file[]" type="file" id="file[]"></th>
    </tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td>
    </tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td>
    </tr>
    <tr>
      <td><input name="textfield[]" type="text" id="textfield[]"></td>
    </tr>
    <tr>
      <td><input name="textfield[]" type="text" id="textfield[]"></td>
    </tr>
    <tr>
      <td><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td>
    </tr>
  </table>
</form>

this is just a test script for me learn,.. but what its doing now is skipping all the file uploads except the last. But reporting them all as a success. And since the first to are not uploaded its giving m an error on the thumbnails,..

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 August 2006 - 07:17 PM

<?php
foreach ($_FILES['file']['name'] as $k => $filename) {

        if ($name != '')  {

                    $tmpfile = $_FILES['file']['tmp_name][$k];

                    // process temp file
        }
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 28 August 2006 - 02:01 PM

Not sure I understand this Barand. Could you explain?..please

#7 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 28 August 2006 - 02:20 PM

Ok I tried this,.. but Im still only able to upload the last picture,.. but strangly now Im not getting any error form the thumbnail process. So does that mean the other two files were there at on point. God Im confused :-\

#8 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 28 August 2006 - 04:22 PM

Ok I think I got it,.. but now I have 10 upload fields. If only two files are uploaded then it gives me errors on the other 7 and so on. How would I now check to see if each field has a value. I would think by using isset,.. but where would I put it so it loops through all files and stops when there is no value.

//get array of filenames
foreach($_FILES['file']['name'] as $value2) {
$filename .= "Image Name: $value2\n";
}
echo $filename;
echo $value2;

//validate file 1
function filecheck($value2){
   $ext = strrchr($value2,'.');
   $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");
   if ( in_array ($ext, $FileType) )
     return TRUE;
   else
     return FALSE;
}



foreach ($_FILES['file']['name'] as $k => $filename) {

        if ($filename != '')  {

                    $tmpfile = $_FILES['file']['tmp_name'][$k];
					
					}else{
					echo "error";
					}
					
					//validate file 1


 
   
   
//if the temp file1 is there copy it to the server 
if (@is_uploaded_file($tmpfile)) { 
copy($tmpfile, "/home/rack/public_html/project/jobs/$dir2/return_images/" .$filename); 
echo "<br>success"; 
}else {
echo "<br>Not Gonna Happen<br>";
}

//get image size and name
$im_file_name =  "/home/rack/public_html/project/jobs/$dir2/return_images/" . $filename;
$image_attribs = getimagesize($im_file_name); 
$im_old = imageCreateFromJpeg($im_file_name); 

//declare thumb
$th_max_width = 100; 
$th_max_height = 100; 
$ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; 
$th_width = $image_attribs[0] * $ratio; 
$th_height = $image_attribs[1] * $ratio; 
$im_new = imagecreatetruecolor($th_width,$th_height); 
imageAntiAlias($im_new,true); 

//resize image
$th_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/thumbs/" . $filename;
imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); 
imageJpeg($im_new,$th_file_name,100);
					
}

foreach ($_FILES['file']['name'] as $value3) {


$pathname .= "$basepath$value3";
$thumbpath .= "$thumbpath$value3";
$mysql = "rack_pm";
mysql_select_db($mysql);
$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$pathname', '$thumbpath', '$job', '$direc', NULL, '1');";

mysql_query($sql) or die('Error, insert query failed');

}
echo $pathname;
echo $thumbpath;



#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 August 2006 - 07:36 PM

Why use 3 foreach loops to process the files.

Just process each uploaded file where I put "// process temp file"

    save original file
    create thumbnail file
    store both names to the db table
   
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 28 August 2006 - 07:43 PM

Ok good point. But now the insert into is getting some strang results. It appears to be combing the files names on the 2nd instance of the array..

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 August 2006 - 11:18 PM

post current code
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 29 August 2006 - 01:39 PM

got it all figured out,.. thanks man




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users