Jump to content


Photo

For Each upload


  • Please log in to reply
4 replies to this topic

#1 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 26 October 2006 - 05:31 PM

Ok got a strange one here:

require_once('/home/rack/public_html/project/Connections/pms.php'); 
//get job number
$job = $_POST['num'];

//get directory
$direc = $_POST['nam'];



//change spaces to _ for dir
$dir1=trim($direc);
$dir2=str_replace(' ', '_', $dir1);

//set base path for database
$basepath="http://project.rackattackzone.com/jobs/$dir2/return_images/";
$thumbpath="http://project.rackattackzone.com/jobs/$dir2/return_images/thumbs/";


//get array of filenames
foreach($_FILES['file']['name'] as $value1) {
$filename .= "$value1";
//change spaces to %20
$filenames=trim($filename);
$filenamea=str_replace(' ', '%20', $filename);

}

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

        if ($filenamea != '')  {

                    $tmpfile = $_FILES['file']['tmp_name'][$k];
						}
   
   
//if the temp file1 is there copy it to the server 
if (@is_uploaded_file($tmpfile) && $filenamea != '') { 
copy($tmpfile, "/home/rack/public_html/project/jobs/$dir2/return_images/" .$filename); 
}

//get image size and name
if ($filename != ''){
$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);
					
//get array of filenames


$pathname = $basepath . $filenamea;
$thumbpath2 = $thumbpath . $filenamea;

if ($filenamea != ''){
$mysql = "rack_pm";
mysql_select_db($mysql);
$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$pathname', '$thumbpath2', '$job', '$direc', NULL, '1');";

mysql_query($sql) or die(mysql_error());
}

}
	}






?>


If one file is uploaded this works great. But if I upload multiple files,.. well its wierd it names 1 file with all the names of the other files. It only thumbnails one and the insert into database seems to work fine. So where did I go wrong? Also is thier a faster way of doing this? This is on a windows mobile enabled smart phone so speed is very important....thanks!

#2 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 26 October 2006 - 07:06 PM

bump :)

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 26 October 2006 - 07:29 PM

On a quick glance, you are running two separate foreach loops, the first one will complete before the next one starts, leaving you with only the value of the last file uploaded (and yes - i notice the $filename .= but i dont understand your logic....)

For a start, remove your first loop and include your trim and str_replace etc in the main loop. See what that brings.

#4 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 26 October 2006 - 07:35 PM

ok I'll give that a shot

#5 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 26 October 2006 - 07:59 PM

Wow,.. that seems to have fixed the problem,...

require_once('/home/rack/public_html/project/Connections/pms.php'); 
//get job number
$job = $_POST['num'];

//get directory
$direc = $_POST['nam'];



//change spaces to _ for dir
$dir1=trim($direc);
$dir2=str_replace(' ', '_', $dir1);

//set base path for database
$basepath="http://project.rackattackzone.com/jobs/$dir2/return_images/";
$thumbpath="http://project.rackattackzone.com/jobs/$dir2/return_images/thumbs/";




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

        if ($filename != '')  {

                    $tmpfile = $_FILES['file']['tmp_name'][$k];
						}
   
//change spaces to %20
$filenames=trim($filename);
$filenamea=str_replace(' ', '%20', $filenames);   
   
//if the temp file1 is there copy it to the server 
if (@is_uploaded_file($tmpfile) && $filenamea != '') { 
copy($tmpfile, "/home/rack/public_html/project/jobs/$dir2/return_images/" .$filename); 
}

//get image size and name
if ($filename != ''){
$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);
					
//get array of filenames


$pathname = $basepath . $filenamea;
$thumbpath2 = $thumbpath . $filenamea;

if ($filenamea != ''){
$mysql = "rack_pm";
mysql_select_db($mysql);
$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$pathname', '$thumbpath2', '$job', '$direc', NULL, '1');";

mysql_query($sql) or die(mysql_error());
}

}
	}






?>


Thanks man




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users