Multi Uploads for Files Help

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

Post Reply
5uperMario
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 3:35 am

Multi Uploads for Files Help

Post by 5uperMario »

yo

I'm back, if you've seen my first thread, I made a Upload tool for images and such.
After some help and rearranging of the code (I forget a { } for a function) I got it fully working.

Now I want a way to upload multiple files at once. I think pretty simple to do.

Well, the HTML part is as follows:

Code: Select all

 
<br>Upload a File
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="uploaded" type="file" id="ufile[]" size="50" /><br />
<input name="uploaded2" type="file" id="ufile[]" size="50" /><br />
<input name="uploaded3" type="file" id="ufile[]" size="50" /><br />
<input name="uploaded4" type="file" id="ufile[]" size="50" /><br />
<input type="submit" value="Upload the File(s)" />
</form>
 
 
And my upload code is of follows:

Code: Select all

 
<?php
require ("includesforall/header.php");
 
 
$strFile = $_FILES['uploaded']['tmp_name'];
$strFileName = $_FILES['uploaded']['name'];
$arrValidExts = array('jpg','gif','png','bmp','tif','zip','txt','rar','mp3','wav','ogg','mid','at3'); // put allowed extension here
$intMaxSize = 20000000; // put max filesize in bytes here
 
 
function findexts ($filename)
{
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts; 
}
 
$bolAllowed = true;
$ext = findexts ($strFileName);
global $filetype_img;
if(!in_array($ext,$arrValidExts)) {
   $bolAllowed = false;
}
if(filesize($strFile) > $intMaxSize) {
    $bolAllowed = false;
}
if($bolAllowed) {
    echo('Upload Successful<br>');
    $ext = findexts ($_FILES['uploaded']['name']) ; 
    $ran = rand () ;
    $ran2 = $ran.".";
    $target = "images/";
    $target = $target . $ran2.$ext; 
 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file has been uploaded as ".$ran2.$ext;
echo "<br>The file is located here: http://www.pspdd.comze.com/upload/images/".$ran2.$ext;
echo "<br>Click <a href=\"http://www.pspdd.comze.com/upload/images/$ran2$ext\">Here</a> to view/download the file.";
}
 
} else {
     echo('Upload Failed For File 1.<br>Invalid file format or size For File 1.');
   // failed upload code in here
}
 
 
What a messy code
As you can see, If user uploads so-so, and it is a allowed type and size, it uploads and a successful message is shown.
If otherwise, and error
 
I thought I could copy the block of code and rename functions and stuff to 2
so, $filename2
 
assuming I wanna kept the file size and file formats along with all four uploads, renaming it will still work?
it does...kinda
If I upload 2 or 3 files, only the first one will be a success but the rest will displays errors
 
Using Google, I'm guessing I could use a for loop or something like that, to assure all the uploads are successful.
That is, if the user decides to upload four files.
So if they only choose 2 files, the 2 upload that wasnt selected would display "file not selected"
or

Code: Select all

 
if file not selected
echo "blablabla
 
 
rather then it displaying if the file didn't upload right "upload unsuccessful"
 
idk I've been on this for about 4 hours now.
 

Code: Select all

 
<?php
 
    for($i = 0; $i < 100; $i++) {
    echo "test<br>";
    }
 
?>
 
"A for loop allows you to create loop that automatically execute desired blocks of code as many times as you like"
So then, this is what I need for the uploads?
So then the block of code I posted would be executed the number of times as there of many upload forms
?

How do I implant this into my already assembled code :?:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Multi Uploads for Files Help

Post by jackpf »

You need a foreach() loop, as the files are put into an array (because you put [] after them).
5uperMario
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 3:35 am

Re: Multi Uploads for Files Help

Post by 5uperMario »

ok...
I managed to put it in a foreach loop...I think..?
I'm following this guide http://www.tizag.com/phpT/foreach.php

I guess I don't fully understand how to use foreach loop.

well, I will keep try tho
It now uploads with the one file but if I upload using the other boxes it ignores it
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Multi Uploads for Files Help

Post by jackpf »

Try this:

Code: Select all

 
if(count($_FILES) == 0)
{
    echo '<form method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="submit" />
    </form>';
}
else
{
    foreach($_FILES['file']['name'] as $key => $value)
    {
        if(!empty($_FILES['file']['name'][$key]))
            echo $_FILES['file']['name'][$key].'<br />';
    }
}
 
That's basically it - you just need to adapt it to handle the uploads.
Post Reply