tags:

views:

67

answers:

2

Hai I am having a form which contains a filetype like this

  <tr><td> <input name="ufile[]" type="file" id="ufile[]" size="50" /></td></tr>

on submit i am calling a script

function addRowToTable()
    {
        var tbl = document.getElementById('uploadTab');
        var lastrow = tbl.rows.length;
        var iteration = lastrow; 
        var row = tbl.insertRow(lastrow);

        var cell2 = row.insertCell(0);
        var e2 = document.createElement('input');
        e2.type = 'file';
        e2.name = 'ufile[]';
        e2.id = 'ufile[]';
        e2.size='50';
        cell2.appendChild(e2);
    }

This script generates The tr on a button click... In my view generatedsource tool i get the "" like this

<tr><td><input size="50" id="ufile[]" name="ufile[]" type="file"></td></tr>

when i submit the form i dont get the file name for the generated file type in my view page But i get the file name foe the one that is default What may be the problem?

I posting my form to this page,

<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
$path4= "upload/".$HTTP_POST_FILES['ufile']['name'][3];

//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
echo copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];

if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}

?>

edit This is my array

Array ( [ufile] => Array ( [name] => Array ( [0] => stripe.jpg [1] => stripe.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg ) [tmp_name] => Array ( [0] => D:\xampp\tmp\php29A9.tmp [1] => D:\xampp\tmp\php29AA.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 8717 [1] => 8717 ) ) ) 
+1  A: 

Use Firebug's right click > "Inspect Element" to make sure the new input is in fact a child of your form. Otherwise, it won't be sent along.

Pekka
@udaya if you upload a really, really big file in the dynamic field. Does it take the appropriate (long) amount of time to send the request to the server?
Pekka
@udaya no I mean, deliberately upload a huge file to see whether it gets sent by the browser at all. But anyway, I think I've got it, hang on., I will update my answer in a second.
Pekka
@udaya the files are definitely both there. can you change `$HTTP_POST_FILES` to `$_FILES` as a first measure?
Pekka
@udaya the file is there. What exactly is your error message? Because your script is designed to output "error" in any case, because the 3rd file is missing. You need to check your script.
Pekka
@udaya *are* you uploading three files?
Pekka
@udaya Please try uploading a huge file in the generated input to see whether it gets transmitted to the server at all.
Pekka
@udaya I see. Check my new comment above, try a big file to test whether it works at all.
Pekka
@pekka i tried to upload a picture of 1024*186 pixel it is uploaded
udaya
@pekka File Size :209286 is also uploaded
udaya
@udaya so it gets uploaded but does not appear in the FILES array?
Pekka
@bobince cheers, I was a bit uncertain about it but thought the other way round was buggy. Removed that part.
Pekka
@udaya did it work out, or sort itself?
Pekka
+2  A: 

Hate to state the obvious, but have you remembered to include the enctype attribute?

<form method="post" enctype="multipart/form-data" action="...php">

Without this, you'll never get a file upload; you'll only see the filename as the submitted value.

<input name="ufile[]" type="file" id="ufile[]" size="50" />

It's invalid to include square brackets in an id. Choose an id that includes only alphanumerics plus .-_, and begins with a letter. If you need to add an id to every generated element, make sure it's different each time. Although as far as I can see you're not actually using any of these IDs, so you could quite easily omit them all.

$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];

EXTREME DANGER. The user can put anything in the filename, including .. (go up a directory) and active filetypes like .php. Never trust user-submitted filenames on your server's filesystem! Either strongly sanitise them (which is harder than you think), or, better, use a completely unrelated name to store it under.

Allowing users to upload files at all carries big risks. See this question for further discussion.

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";

This and every time you put a text string into HTML, you need to call htmlspecialchars() on it. Otherwise < and & characters in the string can include markup including script, and you've got a cross-site-scripting security hole.

bobince
He must have, as the upload fields he has in plain HTML work fine.
Pekka
I dunno, he only mentions the getting the ‘filename’ in the question, so I'm not wholly sure what's happening.
bobince
There are file sizes and temp files in the `print_r` output so they seem to be transmitted. As for the rest, sharp eyes sir!
Pekka
@Pekka: Ah, didn't spot that `print_r` (sharp eh!)... it looks just fine though. The `name` is there, the `size` is there, the `tmp_name` is there. It just looks like the same file has been uploaded twice, once in each row. I'm struggling to understand what “i dont get the file name for the generated file type in my view page” means.
bobince