making loop for 6 uploads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

making loop for 6 uploads

Post by ddragas »

Can somebody help me in making loop

What I need is to make upload 6 pictures on server. I've got script (upload and resize class) for uploading, and resizing one image.

How can I make loop for uploading and resizing 6 images aon server.

Here is array of filenames witch should be uploaded

array("filename", "filename1", "filename2", "filename3", "filename4", "filename5");



if is needed I can upload uther files of resize class

Code: Select all

<?php 
include_once("ImageResizeFactory.php"); 
if($_POST["bupload"]) 
{ 

    $maxSize = "2048576"; // 2MB upload size of the file. 
    $width = "300";//$_POST["width"]; 
    $height = "300";//$_POST["height"]; 
    $allowedExtensions = array("jpg", "JPG", "JPEG", "png", "PNG"); 
     
//    $fajloviArray = array("filename", "filename1", "filename2", "filename3", "filename4", "filename5");     

    $uploadedFileName = $_FILES['filename']['name']; 
    if($_FILES['filename']['size'] > $maxSize) 
    { 
        $error = "File size cannot exceed 2MB"; 
    } 
    if(file_exists("uploadedfiles/" . $uploadedFileName)) { 
        $error = "File with " . $uploadedFileName . " name is already present<br>"; 
    } 
    $extension = pathinfo($_FILES['filename']['name']); 
    $extension = $extension["extension"]; 
    foreach($allowedExtensions as $key=>$ext) { 
        if(strcasecmp($ext, $extension) == 0) 
        { 
            $boolValidExt = true; 
            break; 
        } 
    } 
    if($boolValidExt) { 
        if(empty($error)) { 
            if(is_uploaded_file($_FILES['filename']['tmp_na
me'])) { 
                copy($_FILES['filename']['tmp_name'], "uploadedfiles/" . $uploadedFileName); 
            } 
        } 
    } 
    else 
    { 
        $error = "Files with .$extension extensions are not allowed"; 
    } 
    if(empty($error)) 
    { 
        $srcFile = "uploadedfiles/" . $uploadedFileName; 
        $destFile = "uploadedfiles/resize_" . $uploadedFileName; 

        // Instantiate the correct object depending on type of image i.e jpg or png 
        $objResize = ImageResizeFactory::getInstanceOf($srcFile, $destFile, $width, $height); 
        // Call the method to resize the image 
        $objResize->getResizedImage(); 
        unlink($srcFile); 
        unset($objResize); 
        header("Location:" . $destFile); 
        exit; 
    } 
} 
?> 


<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<FORM name="frmupload" method="POST" enctype="multipart/form-data"> 
  Slika 1 
  <input type="file" name="filename"> 
  <br> 
  Slika 2 
  <input type="file" name="filename1"> 
  <br> 
  Slika 3 
  <input type="file" name="filename2"> 
  <br> 
  Slika 4 
<input type="file" name="filename3"> 
  <br> 
  Slika 5 
<input type="file" name="filename4"> 
  <br> 
  Slika 6 
<input type="file" name="filename5"> 
  <br> 
  <br> 
  <br> 
  <input type="submit" name="bupload" value="Resize"> 
  <input name="reset" type="reset" value="Clear"> 
  <font color="red"><b><?php echo $error;?></b></font> 
  <TABLE align="center"> 
    <TR> 
         
      <TD>&nbsp; </TD> 
    </TR> 
</TABLE> 
<INPUT type="hidden" name="hdnupload" value="false"> 
</FORM> 
</BODY> 
</HTML> 
<?php

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: making loop for 6 uploads

Post by feyd »

an edit of your code:
note: not tested

Code: Select all

<?php 
include_once("ImageResizeFactory.php"); 
if($_SERVER['REQUEST_METHOD'] === "POST") 
{ 

    $maxSize = "2048576"; // 2MB upload size of the file. 
    $width = "300";//$_POST["width"]; 
    $height = "300";//$_POST["height"]; 
    $allowedExtensions = array("jpe?g", "png");

    $errors = array();

    foreach($_FILES as $data)
    {
        $ret = processUpload($data,$maxSize,$width,$height,$allowedExtensions);
        if(!is_bool($ret))
            $errors[] = $ret;
    }

    if(!empty($errors))
        $error = implode("<br />\n",$errors);
    else
        exit("completed successfully.");
}

function processUpload($data,$maxSize,$width,$height,$allowedExtensions)
{
    $uploadedFileName = $data['name']; 
    if($data['size'] > $maxSize) 
    { 
        $error = "File size cannot exceed 2MB"; 
    } 
    if(file_exists("uploadedfiles/" . $uploadedFileName)) { 
        $error = "File with " . $uploadedFileName . " name is already present<br>"; 
    } 
    $extension = pathinfo($data['name']); 
    $extension = $extension["extension"];
    $allowedExtensions = '#^'.implode("|",$allowedExtensions).'$#i';
    $boolValidExt = preg_match($allowedExtensions,$extension);
    if($boolValidExt) { 
        if(empty($error) && is_uploaded_file($data['tmp_name'])) {
            copy($data['tmp_name'], "uploadedfiles/" . $uploadedFileName); 
        } 
    } 
    else 
    { 
        $error = "Files with .$extension extensions are not allowed"; 
    } 
    if(empty($error)) 
    { 
        $srcFile = "uploadedfiles/" . $uploadedFileName; 
        $destFile = "uploadedfiles/resize_" . $uploadedFileName; 

        // Instantiate the correct object depending on type of image i.e jpg or png 
        $objResize = ImageResizeFactory::getInstanceOf($srcFile, $destFile, $width, $height); 
        // Call the method to resize the image 
        $objResize->getResizedImage(); 
        unlink($srcFile); 
        unset($objResize); 
//        header("Location:" . $destFile); 
//        exit; 
    }

    if(!empty($error))
        return $error;
    else
        return true;
}
?> 


<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<FORM name="frmupload" method="POST" enctype="multipart/form-data"> 
  Slika 1 
  <input type="file" name="filename"> 
  <br> 
  Slika 2 
  <input type="file" name="filename1"> 
  <br> 
  Slika 3 
  <input type="file" name="filename2"> 
  <br> 
  Slika 4 
<input type="file" name="filename3"> 
  <br> 
  Slika 5 
<input type="file" name="filename4"> 
  <br> 
  Slika 6 
<input type="file" name="filename5"> 
  <br> 
  <br> 
  <br> 
  <input type="submit" name="bupload" value="Resize"> 
  <input name="reset" type="reset" value="Clear"> 
  <font color="red"><b><?php if(!empty($error)) echo $error;?></b></font> 
  <TABLE align="center"> 
    <TR> 
         
      <TD>&nbsp; </TD> 
    </TR> 
</TABLE> 
<INPUT type="hidden" name="hdnupload" value="false"> 
</FORM> 
</BODY> 
</HTML>
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you for quick reply.

I've tested code you have give to me, but it only works when I upload one by one image.

If I upload all 3 or more in one time, I get error (sometimes if I upload 2 in one time)

"Fatal error: Maximum execution time of 30 seconds exceeded"

How can I make more time than 30 sec?

or suggestion: to check if filefild is empty to go to another, becouse it checks for each filefild, and it is trying to upload all filefields empty or not empty
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]set_time_limit[/php_man]() for changing the time limit.. The script itself shouldn't take too long to run, unless your imageresize script is royally slow.. I don't think time is counted while uploading.. :roll:
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Sorry, my mistake. :oops:

The script is working great!!!!
Thank you a milion.


What you have done is to resize and upload images all in same size, and that is what I've been aksing for, and thank you for that.

Can I be free to ask three other things?


1. User should be able to upload images in its own folder named by userID

lets say

$userID="2524";

$userfolder=$userID;

if folder doesnt exist than it should be created, and pictures that are uploaded, saved in that folder.

2. saved path in database of each image that was uploaded

fields in database:
Feeld1, Feeld2, Feeld3, Feeld4, Feeld5, Feeld6

3. Only first image that was uploaded should have one size, and all other to have the other sizes (smaller ones).


These is stuff I need.

If I'm asking to much feel free to refuse to help me. :cry:
Thank you again
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

why dont you try it on your own first, and then ask specific questions. We aren't going to code it for you... Sorry
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it shouldn't take much fiddling to get those features with my version of the script.. but as Illusionist said, try it first.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Well you'right guys.

If somebody is got to do it then it's me.

I've manige to resolve problem with folders.

Now it remains to resolve other two. What I do not know is how to resize only first image with size (W=500, H=500), and all others with size (W=150, H=150) when script is going loop trough all filefields.

and second question:
I know that image names should be given to variables, and later inserted in database with complete path. But again I don't know how to determine what value should be given to $image1, value should be given to $image2, etc... when script is going loop trough all filefields.

Here is code until now.

And guys... Thank you for helping me. I realy aprecheate it.

Code: Select all

<?php 
include_once("ImageResizeFactory.php"); 
if($_SERVER['REQUEST_METHOD'] === "POST") 
{ 
	ini_set("max_execution_time","3000"); 
    

	$maxSize = "2048576"; // 2MB upload size of the file. 
    $width = "300";
    $height = "300";
    $allowedExtensions = array("jpg", "JPG", "JPEG", "png", "PNG"); 

    $errors = array(); 

    foreach($_FILES as $data) 
	
    { 
        $ret = processUpload($data,$maxSize,$width,$height,$allowedExtensions); 
        if(!is_bool($ret)) 
            $errors[] = $ret; 
    } 
	
    if(!empty($errors)) 
        $error = implode("<br />\n",$errors); 
    else 
        exit("Upload slika uspješno izvršen...."); 
} 

function processUpload($data,$maxSize,$width,$height,$allowedExtensions) 
{ 
    $uploadedFileName = $data['name']; 
	$localtime = mktime();// the current datetime as a timestamp 
	$yyyymmdd = date("d_m_y__H_i_s__", $localtime);
	$Photo_Dir = "uploadedfiles/";
	$userID = "2251";
	
	$User_Dir = $userID ."/";
	
			if (!is_dir($Photo_Dir.$User_Dir))
               {
                    mkdir ($Photo_Dir.$User_Dir, 0777);
                    chmod ($Photo_Dir.$User_Dir, 0777);
               }
	
    if($data['size'] > $maxSize) 
    { 
        $error = "Slika nesmije biti ve&#263;a od 2MB"; 
    } 
    if(file_exists($Photo_Dir . $uploadedFileName)) { 
        $error = "Slika sa imenom " . $uploadedFileName . " ve&#263; postoji<br>"; 
    } 
    $extension = pathinfo($data['name']); 
    $extension = $extension["extension"]; 
    $allowedExtensions = '#^'.implode("|",$allowedExtensions).'$#i'; 
    $boolValidExt = preg_match($allowedExtensions,$extension); 
    if($boolValidExt) { 
        if(empty($error) && is_uploaded_file($data['tmp_name'])) { 
            copy($data['tmp_name'], $Photo_Dir . $uploadedFileName); 
        } 
    } 
    else 
    { 
        $error = "Datoteka sa .$extension ekstenzijom nije dopuštena !!!"; 
    } 
    if(empty($error)) 
    { 
        $srcFile = $Photo_Dir . $uploadedFileName; 
        $destFile = $Photo_Dir . $User_Dir . $yyyymmdd . $uploadedFileName; 

        // Instantiate the correct object depending on type of image i.e jpg or png 
        $objResize = ImageResizeFactory::getInstanceOf($srcFile, $destFile, $width, $height); 
        // Call the method to resize the image 
        $objResize->getResizedImage(); 
        unlink($srcFile); 
        unset($objResize); 
//        header("Location:" . $destFile); 
//        exit; 
    } 

    if(!empty($error)) 
        return $error; 
    else 
        return true; 
} 
?> 


<HTML> 
<HEAD> 
<TITLE></TITLE> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250"></HEAD> 
<BODY> 
<FORM name="frmupload" method="POST" enctype="multipart/form-data"> 
  Slika 1 
  <input type="file" name="filename"> 
  <br> 
  Slika 2 
  <input type="file" name="filename1"> 
  <br> 
  Slika 3 
  <input type="file" name="filename2"> 
  <br> 
  Slika 4 
<input type="file" name="filename3"> 
  <br> 
  Slika 5 
<input type="file" name="filename4"> 
  <br> 
  Slika 6 
<input type="file" name="filename5"> 
  <br> 
  Slika 7 
<input type="file" name="filename6"> 
  <br> 
  <br> 
  <br> 
  <input type="submit" name="bupload" value="Resize"> 
  <input name="reset" type="reset" value="Clear"> 
  <font color="red"><b><?php if(!empty($error)) echo $error;?></b></font> 
  <TABLE align="center"> 
    <TR> 
          
      <TD>  </TD> 
    </TR> 
</TABLE> 
<INPUT type="hidden" name="hdnupload" value="false"> 
</FORM> 
</BODY> 
</HTML> 

<?php

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for the first image, pass a different width & height to the processUpload function, since there are arguments for just that.

as for 2:
you already have $uploadedFilename in the function, you could call a second function to add it to the database, since $uploadedFilename will be the final resting place for it.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

should it go like this?

if($data['filename'] = "filename")
{ function processUpload

($data,$maxSize,$width1,$height1,$allowedExtensions)
{
else
{
function processUpload($data,$maxSize,$width,$height,$allowedExtensions)
{
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was thinking more along the lines of:

Code: Select all

$first = true;
foreach(whatever)
{
if($first)
  processUpload(); // first file
else
  processUpload(); // all other files
$first = false;
}
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

If I do it in this way I get error in line 45.

This is error
"Parse error: parse error, unexpected T_VARIABLE, expecting '{' in c:\program files\apache group\apache\htdocs\cro1\resize\tmp8o5x4ymjpr.php on line 45"

and line 45 is

$uploadedFileName = $data['name']; // LINE 45


Here is complete code

Code: Select all

<?php 
include_once("ImageResizeFactory.php"); 
if($_SERVER['REQUEST_METHOD'] === "POST") 
{ 
   ini_set("max_execution_time","3000"); 
    

    $maxSize = "2048576"; // 2MB upload size of the file. 
	$width1 = "800"; 
    $height1 = "800";     
	$width = "300"; 
    $height = "300"; 
	
    $allowedExtensions = array("jpg", "JPG", "JPEG", "png", "PNG"); 

    $errors = array(); 

$first = true; 

    foreach($_FILES as $data) 


{ 
if($first) 
  			processUpload($data,$maxSize,$width1,$height1,$allowedExtensions); // first file 
else 
  			processUpload($data,$maxSize,$width,$height,$allowedExtensions); // all other files 
$first = false; 
}
    
    { 
        $ret = processUpload($data,$maxSize,$width,$height,$allowedExtensions); 
        if(!is_bool($ret)) 
            $errors[] = $ret; 
    } 
    
    if(!empty($errors)) 
        $error = implode("<br />\n",$errors); 
    else 
        exit("Upload slika uspješno izvršen...."); 
} 

function processUpload($data,$maxSize,$width,$height,$allowedExtensions) 
 
   $uploadedFileName = $data['name']; // LINE 45
   $localtime = mktime();// the current datetime as a timestamp 
   $yyyymmdd = date("d_m_y__H_i_s__", $localtime); 
   $Photo_Dir = "uploadedfiles/"; 
   $userID = "2251"; 
    
   $User_Dir = $userID ."/"; 
    
         if (!is_dir($Photo_Dir.$User_Dir)) 
               { 
                    mkdir ($Photo_Dir.$User_Dir, 0777); 
                    chmod ($Photo_Dir.$User_Dir, 0777); 
               } 
    
    if($data['size'] > $maxSize) 
    { 
        $error = "Slika nesmije biti ve&#263;a od 2MB"; 
    } 
    if(file_exists($Photo_Dir . $uploadedFileName)) { 
        $error = "Slika sa imenom " . $uploadedFileName . " ve&#263; postoji<br>"; 
    } 
    $extension = pathinfo($data['name']); 
    $extension = $extension["extension"]; 
    $allowedExtensions = '#^'.implode("|",$allowedExtensions).'$#i'; 
    $boolValidExt = preg_match($allowedExtensions,$extension); 
    if($boolValidExt) { 
        if(empty($error) && is_uploaded_file($data['tmp_name'])) { 
            copy($data['tmp_name'], $Photo_Dir . $uploadedFileName); 
        } 
    } 
    else 
    { 
        $error = "Datoteka sa .$extension ekstenzijom nije dopuštena !!!"; 
    } 
    if(empty($error)) 
    { 
        $srcFile = $Photo_Dir . $uploadedFileName; 
        $destFile = $Photo_Dir . $User_Dir . $yyyymmdd . $uploadedFileName; 

        // Instantiate the correct object depending on type of image i.e jpg or png 
		$objResize = ImageResizeFactory::getInstanceOf($srcFile, $destFile, $width, $height); 
        // Call the method to resize the image 
        $objResize->getResizedImage(); 
        unlink($srcFile); 
        unset($objResize); 
//        header("Location:" . $destFile); 
//        exit; 
    } 

    if(!empty($error)) 
        return $error; 
    else 
        return true; 

} 
?> 


<HTML> 
<HEAD> 
<TITLE></TITLE> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250"></HEAD> 
<BODY> 
<FORM name="frmupload" method="POST" enctype="multipart/form-data"> 
  Slika 1 
  <input type="file" name="filename"> 
  <br> 
  Slika 2 
  <input type="file" name="filename1"> 
  <br> 
  Slika 3 
  <input type="file" name="filename2"> 
  <br> 
  Slika 4 
<input type="file" name="filename3"> 
  <br> 
  Slika 5 
<input type="file" name="filename4"> 
  <br> 
  Slika 6 
<input type="file" name="filename5"> 
  <br> 
  Slika 7 
<input type="file" name="filename6"> 
  <br> 
  <br> 
  <br> 
  <input type="submit" name="bupload" value="Resize"> 
  <input name="reset" type="reset" value="Clear"> 
  <font color="red"><b><?php if(!empty($error)) echo $error;?></b></font> 
  <TABLE align="center"> 
    <TR> 
          
      <TD>  </TD> 
    </TR> 
</TABLE> 
<INPUT type="hidden" name="hdnupload" value="false"> 
</FORM> 
</BODY> 
</HTML> 

<?php

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're missing the starting { after function processUpload($data,$maxSize,$width,$height,$allowedExtensions)
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

It's working.

Thank you a lot.


Can You help me about last problem? How will I put path in database of each uploaded and resized image? Now I see code could be something like you've told me before.

$first=true;
conect to db
sql_db=insert in database Field1 values $Photo_Dir . $User_Dir . $yyyymmdd . $uploadedFileName
disconect db
$first=false;

$second=true;
conect to db
sql_db=insert in database Field2 values $Photo_Dir . $User_Dir . $yyyymmdd . $uploadedFileName
disconect db
$second=false;


Am I going in right way, or maybe exist something more easier?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

other than the syntax of the sql statements, yes, I think it could be done like that...
Post Reply