Multi Uploads for Files Help
Posted: Wed Jul 22, 2009 3:56 am
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:
And my upload code is of follows:
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
rather then it displaying if the file didn't upload right "upload unsuccessful"
idk I've been on this for about 4 hours now.
"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
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>";
}
?>
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