Re: php batch uploading multiple files



It is working so far.. Please find the script I am using below, the
batch process also makes the filenames unique and writes their names to
a database. Thanks for your help!

<?php

require 'inc/db_connect.php';


$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"tn_".$p,150,150);
}
}

/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}

/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if
(preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if
(preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to
$filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file =
readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}

$pics=directory('big','jpg,JPG,JPEG,jpeg,png,PNG');
$pics=ditchtn($pics,'tn_');
if ($pics[0]!='')
{
foreach ($pics as $p)
{
$photo_prefix = date("iBdLswzsg");
if (!copy('big/'.$p, 'small/'.$photo_prefix.$p)) {
echo "failed to copy $file...\n";
}
$thumb_prefix = substr($photo_prefix,8);
createthumb('big/'.$p,'small/'.$thumb_prefix.$p,100,100);

//$insert_photo = "INSERT INTO tbl_photo (
// photo_name,
// photo_feature_id)
// VALUES (
// '" . $photo_prefix.$p . "',
// " . $_GET["feature_id"] . ")";

$insert_photo = "INSERT INTO tbl_photo (
photo_name,
photo_feature_id)
VALUES (
'" . $photo_prefix.$p . "',
" . 3 . ")";
}
}

?>

.



Relevant Pages

  • Re: Is there a way to query Security Event Log with Filter in C#?
    ... ManagementObjectSearcher mos = new ManagementObjectSearcher; ... foreach ) ... Example that filters event log by LogFile and TimeGenerated. ... foreach (PropertyData pd in mo.Properties) { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performace problem with DataView.RowFilter
    ... Joe - Sahil is dead right on this - I had reset the filters a ... > I need to loop through the smaller table and filter a DataView based on ... > foreach ... > foreach (DataRowView drv in dv) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performace problem with DataView.RowFilter
    ... Joe - Sahil is dead right on this - I had reset the filters a ... > I need to loop through the smaller table and filter a DataView based on ... > foreach ... > foreach (DataRowView drv in dv) ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Inline Pipeline Filter/lambda
    ... how about a different 1 character for in-line ... You can try this out using aliases. ... I also find the "foreach" a bit confusing because I thought the ...
    (microsoft.public.windows.server.scripting)