Jump to content

Need help with array and loop


tech0925

Recommended Posts

Hello everyone! I am new here and this is my first post. I have been a member at another forum which they tend to make fun of newbies instead of helping them learn. That is why I am here  :D

 

Below is my script that allows me to upload 6 images at once and this works nicely. However, I am trying to change the variable $newname to increment in each pass so that I can build some if statements for my sql query. I would like the variable to start from $newname1 and should end up with 6 of them with the last being $newname6. That way I can have 6 unique picture variables that I can either update or insert into my table. Any help here would be great!

 

 

$upload_image_limit = 6; // How many images you want to upload at once?
define('MAX_SIZE', '100');
define('UPLOAD_DIRECTORY', '../memberYard');

// Define a list of extensions we want to allow.
$allowable = array('.gif', '.jpg', '.jpeg', '.png');

$errors = array();
if (isset($_POST['Submit']))
{
    if (!empty($_FILES))
    {
        foreach ($_FILES as $file)
        {
            // No need for a separate function to get a file's extension
            $extension = substr($file['name'], strpos($file['name'], "."));
            $size      = filesize($file['tmp_name']);

            // Check if the file's extension is in the permissable list
            if (!in_array(strtolower($extension), $allowable))
            {
                $errors[] = "File {$file['name']} is of an unknown type.";
                continue;
            }

            // Check that the file size is OK
            if ($size > MAX_SIZE * 1024)
            {
                $errors[] = "File {$file['name']} is too big.";
                continue;
            }

            // Generate a random name for the file and move it
            $newname = substr(md5(microtime()), 0, 12) . $extension;
            $copied = move_uploaded_file($file['tmp_name'], UPLOAD_DIRECTORY . DIRECTORY_SEPARATOR . $newname);

            if ($copied === FALSE)
            {
                $errors[] = "File {$file['name']} could not be uploaded.";
            }
        }
    }
}

       
        
?>

<div id="mainWrapper">
<div id="main">
    <div class="top_bg"></div>
    
<div class="description_pane">

<h2>Upload More Photos</h2>


<?php

        if (!empty($errors))
        {
            echo "<ul>";
            foreach ($errors as $error)
            {
                echo "<li>{$error}</li>";
            }
            echo "</ul>";
        }



############################### HTML FORM
while($i++ < $upload_image_limit){
	$form_img .= '<label>Image '.$i.': </label> <input type="file" name="image'.$i.'"><br /><br />';
}

$htmo .= '

	<form method="post" action="" enctype="multipart/form-data">
		'.$form_img.' <br />
		<input type="submit" value="Upload Images" name="Submit" style="margin-left: 50px;" />

	</form>
	';	

echo $htmo;
?>


<br />

</div>
    


    
</div><!--end main -->
<div class="main_bottom"></div>
</div><!--end mainWrapper -->


<?
include '../templates/footer.php';

?>

Link to comment
Share on other sites

Hi

 

You can use a simple For loop for this.

 


// Generate a random name for the file and move it
for($i=1, $i>=6, $i++){
$newname = substr(md5(microtime()), 0, 12) . $extension;            
$copied = move_uploaded_file($file['tmp_name'], UPLOAD_DIRECTORY . DIRECTORY_SEPARATOR . $newname.$i);
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.