Page 1 of 2

Uploading multiple files @ once..?

Posted: Sun Nov 17, 2002 4:47 pm
by atw
Hi there,

I am working on a website which has league tables. As a temporary measure (until I get the MySQL db sorted) I have set up a CSV parser script (with the kind help of topic http://www.php-forum.com/p/viewtopic.php?t=572).

However, the guy whom is supposed to be updating it is a very computer illiterate and is having trouble uploading the 8 files to the site. I have been searching for an easily customisable script which will upload the files and place them in the appropriate directory (ensuring they are all in lowercase).

Any help would be VERY appreciated, thank you... :wink:

atw

Hi I hawe a php script....

Posted: Mon Nov 18, 2002 3:20 am
by Pinex
I have a php script that does something like that...
I found it some time ago and I don't know who made it and where to download it. If anybody knows tell me.
I would like to get some expert feadback on it before you use it... Maybe the moderator (Pejone) would give us a comment?!

Do you want me to publish the code?
(I would like to get an OK from the moderator (Pejone) to)
;]-

Posted: Mon Nov 18, 2002 6:02 am
by atw
Yeah, feel free; it is after all yours : )

atw

PS. Thanks... :D

Here it is...

Posted: Mon Nov 18, 2002 10:36 am
by Pinex
It's not mine... and I dont know whos it is...
Html:
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form method=POST action=do_upload.php enctype=multipart/form-data>
<p>Files to upload:<br>
<input type=file name=img1 size=30><br>
<input type=file name=img2 size=30><br>
<input type=file name=img3 size=30><br>
<input type=file name=img4 size=30><br>
<input type=file name=img5 size=30><br>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>

I think you can just add several <input type=file name=img* size=30><br> with new number for eac up to 8... But then you have to do the same in the code...

do_upload.php:

Code: Select all

<?
//user defined variables
$abpath = "/home/site/www.domene.no/uplader/img"; //Absolute path to where images are uploaded. No trailing slash
$sizelim = "yes"; //Do you want size limit, yes or no
$size = "2500000"; //What do you want size limited to be if there is one

//all image types to upload
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type
$cert4 = "image/ief"; //Ief type
$cert5 = "image/png"; //Png type
$cert6 = "image/tiff"; //Tiff type
$cert7 = "image/bmp"; //Bmp Type
$cert8 = "image/vnd.wap.wbmp"; //Wbmp type
$cert9 = "image/x-cmu-raster"; //Ras type
$cert10 = "image/x-x-portable-anymap"; //Pnm type
$cert11 = "image/x-portable-bitmap"; //Pbm type
$cert12 = "image/x-portable-graymap"; //Pgm type
$cert13 = "image/x-portable-pixmap"; //Ppm type
$cert14 = "image/x-rgb"; //Rgb type
$cert15 = "image/x-xbitmap"; //Xbm type
$cert16 = "image/x-xpixmap"; //Xpm type
$cert17 = "image/x-xwindowdump"; //Xwd type

$log = "";


//begin upload 1

//checks if file exists
if ($img1_name == "") {
$log .= "No file selected for upload 1<br>";
}
if ($img1_name != "") {
//checks if file exists
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 already existed<br>";
} else {

//checks if files to big
if (($sizelim == "yes") && ($img1_size > $size)) {
$log .= "File 1 was too big<br>";
} else {


//Checks if file is an image
if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3) or ($img1_type == $cert4) or ($img1_type == $cert5) or ($img1_type == $cert6) or ($img1_type == $cert7) or ($img1_type == $cert8) or ($img1_type == $cert9) or ($img1_type == $cert10) or ($img1_type == $cert11) or ($img1_type == $cert12) or ($img1_type == $cert13) or ($img1_type == $cert14) or ($img1_type == $cert15) or ($img1_type == $cert16) or ($img1_type == $cert17)) {
@copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server<br>";
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 was uploaded<br>";
}
} else {
$log .= "File 1 is not an image<br>";
}
}
}
}

//Upload #2

//checks if file exists
if ($img2_name == "") {
$log .= "No file selected for upload 2<br>";
}
if ($img2_name != "") {

//checks if file exists
if (file_exists("$abpath/$img2_name")) {
$log .= "File 2 already existed<br>";
} else {

//checks if files to big
if (($sizelim == "yes") && ($img2_size > $size)) {
$log .= "File 2 was too big<br>";
} else {

//Checks if file is an image
if (($img2_type == $cert1) or ($img2_type == $cert2) or ($img2_type == $cert3) or ($img2_type == $cert4) or ($img2_type == $cert5) or ($img2_type == $cert6) or ($img2_type == $cert7) or ($img2_type == $cert8) or ($img2_type == $cert9) or ($img2_type == $cert10) or ($img2_type == $cert11) or ($img2_type == $cert12) or ($img2_type == $cert13) or ($img2_type == $cert14) or ($img2_type == $cert15) or ($img2_type == $cert16) or ($img2_type == $cert17)) {
@copy($img2, "$abpath/$img2_name") or $log .= "Couldn't copy image 2 to server<br>";
if (file_exists("$abpath/$img2_name")) {
$log .= "File 2 was uploaded<br>";
}
} else {
$log .= "File 2 is not an image<br>";
}
}
}
}

//Upload #3

if ($img3_name == "") {
$log .= "No file selected for upload 3<br>";
}
if ($img3_name != "") {

//checks if file exists
if (file_exists("$abpath/$img3_name")) {
$log .= "File 3 already existed<br>";
} else {

//checks if files to big
if (($sizelim == "yes") && ($img3_size > $size)) {
$log .= "File 3 was too big<br>";
}

//Checks if file is an image
if (($img3_type == $cert1) or ($img3_type == $cert2) or ($img3_type == $cert3) or ($img3_type == $cert4) or ($img3_type == $cert5) or ($img3_type == $cert6) or ($img3_type == $cert7) or ($img3_type == $cert8) or ($img3_type == $cert9) or ($img3_type == $cert10) or ($img3_type == $cert11) or ($img3_type == $cert12) or ($img3_type == $cert13) or ($img3_type == $cert14) or ($img3_type == $cert15) or ($img3_type == $cert16) or ($img3_type == $cert17)) {
@copy($img3, "$abpath/$img3_name") or $log .= "Couldn't copy image 3 to server<br>";
if (file_exists("$abpath/$img3_name")) {
$log .= "File 3 was uploaded<br>";
}
} else {
$log .= "File 3 is not an image<br>";
}
}
}


if ($img4_name == "") {
$log .= "No file selected for upload 4<br>";
}
if ($img4_name != "") {

//checks if file exists
if (file_exists("$abpath/$img4_name")) {
$log .= "File 4 already existed<br>";
} else {

//checks if files to big
if (($sizelim == "yes") && ($img4_size > $size)) {
$log .= "File 4 was too big<br>";
} else {


//Checks if file is an image
if (($img4_type == $cert1) or ($img4_type == $cert2) or ($img4_type == $cert3) or ($img4_type == $cert4) or ($img4_type == $cert5) or ($img4_type == $cert6) or ($img4_type == $cert7) or ($img4_type == $cert8) or ($img4_type == $cert9) or ($img4_type == $cert10) or ($img4_type == $cert11) or ($img4_type == $cert12) or ($img4_type == $cert13) or ($img4_type == $cert14) or ($img4_type == $cert15) or ($img4_type == $cert16) or ($img4_type == $cert17)) {
@copy($img4, "$abpath/$img4_name") or $log .= "Couldn't copy image 4 to server<br>";
if (file_exists("$abpath/$img4_name")) {
$log .= "File 4 was uploaded<br>";
}
} else {
$log .= "File 4 is not an image<br>";
}
}
}
}

//Upload #5
if ($img5_name == "") {
$log .= "No file selected for upload 5<br>";
}
if ($img5_name != "") {
//checks if file exists
if (file_exists("$abpath/$img5_name")) {
$log .= "File 5 already existed<br>";
} else {

//checks if files to big
if (($sizelim == "yes") && ($img5_size > $size)) {
$log .= "File 5 was too big<br>";
} else {


//Checks if file is an image
if (($img5_type == $cert1) or ($img5_type == $cert2) or ($img5_type == $cert3) or ($img5_type == $cert4) or ($img5_type == $cert5) or ($img5_type == $cert6) or ($img5_type == $cert7) or ($img5_type == $cert8) or ($img5_type == $cert9) or ($img5_type == $cert10) or ($img5_type == $cert11) or ($img5_type == $cert12) or ($img5_type == $cert13) or ($img5_type == $cert14) or ($img5_type == $cert15) or ($img5_type == $cert16) or ($img5_type == $cert17)) {
@copy($img5, "$abpath/$img5_name") or $log .= "Couldn't copy image 5 to server<br>";
if (file_exists("$abpath/$img5_name")) {
$log .= "File 5 was uploaded<br>";
}

} else {
$log .= "File 5 is not an image<br>";
}
}
}
}
?>
<html>
<head>
<title>Image Report</title>
</head>
<body>
<p>Log:<br>
<?

echo "$log";

?>
</p>
<body>
<html>


I repeat! It's not mine! If anybody knows hos it is.. I would like 2 know.

I would allso like to get a critical eye at the code.. somethings doesent sem totally right here.... I think it could be done easier.

Try it.. I think i worcks just the way it is. ;]-

Posted: Mon Nov 18, 2002 3:17 pm
by atw
Thanks but I was looking for something a little more simple, that would deal with (text based) CSV files...

...I am too new to this PHP thing to be able to customise that huge script.

Thank you very, very much for the help...

Posted: Mon Nov 18, 2002 7:29 pm
by Oleg Butuzov
weit to the evening I will give you awnswer =).

I am to busy almost all day! (work =))

Posted: Tue Nov 19, 2002 8:59 am
by atw
Okay...

Posted: Wed Nov 20, 2002 10:20 am
by Oleg Butuzov
exemple script

Code: Select all

<?php
$files=5;// how mach file you wants to upload
if (!isset($form)){
   echo "<form method=POST action=$PHP_SELF enctype=multipart/form-data>";
   for ($i=1; $i <= $files; $i++)
       {
        echo "File <b>$i</b> <input type=file name=file$i><br>";
          }
    echo "<input type=hidden name=form value=0>
         <input type=submit value=upload>";
}

if (isset($form))
   {
   for ($i=1; $i <= $files; $i++)
       {
            $filename="file$i";
          copy ($HTTP_POST_FILES[$filename]['tmp_name'], $i);

        }
    }
?>

Posted: Wed Nov 20, 2002 12:47 pm
by atw
Brilliant, but how do I change the upload destination?

How would I go about ensuring that they upload in the correct case (lowercase)..?

Thanks very much :lol:

Hello Pejone

Posted: Wed Nov 20, 2002 12:50 pm
by Jacques
I have a question re your code. Instead of uploading multiple files, what if I wanted to upload multiple values into the same table.

ie: red, (next record:) blue, (next record:) green

Is there something to replace
copy ($HTTP_POST_FILES[$filename]['tmp_name'], $i);
by an insert?

I have been struggling for this one for ages.

Posted: Wed Nov 20, 2002 7:31 pm
by Oleg Butuzov
yes.

for ($i=0; $1 < "10"; $i++)
{
${"value".$i}; // value
}

Posted: Thu Nov 21, 2002 8:09 am
by atw
How do I change the upload destination???

:?

Posted: Thu Nov 21, 2002 8:21 am
by WiZARD
atw wrote:How do I change the upload destination???

:?

see http://www.php.net/manual/en/function.copy.php and i think that you understand....

Posted: Sat Nov 30, 2002 11:47 am
by atw
<?php
$files=1;// how many file(s) you want to upload...
//$location="http://www.atwright.com/averages/"
if (!isset($form)){
echo "<form method=POST action=$PHP_SELF enctype=multipart/form-data>";
for ($i=1; $i <= $files; $i++)
{
echo "File <b>$i</b> <input type=file name=file$i><br>";
}
echo "<input type=hidden name=form value=0>
<input type=submit value=upload>";
}

if (isset($form))
{
for ($i=1; $i <= $files; $i++)
{
$filename="file$i.csv";
copy ($_FILES[$filename]['tmp_name'], "/home/atwright/public_html/averages/$filename");
}
}
?>


How would I go about changing this code so that the uploading files keep their original names AND have lowercase names?

Also, is it possible to make it choose a folder and upload all the files in the folder instead of IDing them all individually?

Thanks for any help :D

Posted: Sun Dec 01, 2002 12:46 am
by Oleg Butuzov
must read
http://www.php.net/manual/en/function.strtolower.php

$_FILES[$filename]['name'] - origginal name of the file

yes it posible to changr a folder =)

Posted: Sun Dec 01, 2002 9:38 am
by atw
Okay, just to rephrase, how would I change the script to make it upload the contents of folder? You must forgive me; though I am intellegent enough to fill in a lot of gaps and/or adjust simple code to my needs I am actually more at home with Macromedia's Lingo than PHP.

copy ($_FILES[$filename]['tmp_name'], "/home/atwright/public_html/averages/$filename");


What do I then change about the path? Would: "/home/atwright/public_html/averages/$name"); work? :?

Sorry for being such an info leech...

...when I understand PHP a little more I'll try to start giving back :lol:

Posted: Mon Dec 02, 2002 3:43 am
by Oleg Butuzov

Code: Select all

copy ($_FILES[$filename]['tmp_name'], "/home/atwright/public_html/averages/".$filename);

Posted: Mon Dec 02, 2002 4:45 am
by WiZARD
And once more if you try to upload file more then 2Mb your server not let you to do this operation - quotes.....

Posted: Mon Dec 02, 2002 1:03 pm
by atw
Pejone: I do not understand, that is essentially the same piece of code is it not? :?

Wizard: Not a problem, the files are all about 2.8Kb...


atw

Posted: Tue Dec 03, 2002 5:47 am
by Oleg Butuzov
I do not understand, that is essentially the same piece of code is it not?


it is more practise pice of code =)

Posted: Wed Dec 04, 2002 2:35 pm
by atw
it is more practise pice of code =)

I'm sorry I don't understand, Practise???

Will this allow me to upload the contents of folder or just a file?

If it is just for singular files, do I need to remove the line:

$filename="file$i.csv";

:?:

Posted: Thu Dec 05, 2002 2:09 am
by Oleg Butuzov
ok,

as my English so bad i will tell in this way.

Yes, your script is good.

and you do not need remove that line

Posted: Thu Dec 05, 2002 11:31 am
by atw
I would say your English is very good. Let me put it this way, your English is better than my (non-existant) Ukranian.

:lol:

Hi again!

Posted: Tue Dec 10, 2002 6:02 am
by Pinex
I like some of the functions in the script i suggested in the beginning of this topic.
But there is a code I want to add, but I dont know how to do it!

Code: Select all

//checks if file exists
if ($img1_name == "") {
$log .= "No file selected for upload 1<br>";
}
if ($img1_name != "") {
//checks if file exists
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 already existed<br>";
} else {

And I want it to do something like this:

Code: Select all

//checks if file exists
if ($img1_name == "") {
$log .= "No file selected for upload 1<br>";
}
if ($img1_name != "") {
//checks if file exists
if (file_exists("$abpath/$img1_name")) {
$log .= " <form method=POST action=??> File 1 already existed: <br>Owerwrite ".$img1_name."?  <input name=Overwrite type="checkbox" value=??><b> Yes!</b><br><input type="Submit" name="submit"></form>";
} else {

It's okay to know if the file already exists, and if you have a new one and want to overwrite you can.

Any suggestons?

Posted: Tue Dec 10, 2002 6:09 am
by Oleg Butuzov
if (if_file_exist($file))
{

}

:D