Jump to content

For Each upload


piznac

Recommended Posts

Ok got a strange one here:

[code]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());
}

}
}






?> [/code]

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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

[code]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());
}

}
}






?> [/code]

Thanks man
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.