I've a form for adding user to my database and it also uploads an image to my site, this all works fine. I am using http://nativedroid.godesign.ch/ as my site is for mobiles.
The problem is when I include this line on the php page with the form:
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
This causes $_FILES to be empty. Even if I simplify the form to this the $_FILES array is empty.
This is my simple form:
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="myfile"><p>
<input type="submit" value="Add Staff"></p>
</form>
When I echo this in my upload.php
$name = $_FILES["myfile"]["name"];
I get an undefined error for "name", because the array is empty.
For some reason including the jquery is emptying the array, but I can't find a solution. I've been stuck on this for days now so any help is appreciated.
I think js conflict will be there so your code will not working there and you are getting empty array.
Change form code .. please check submit tag..
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="myfile"><p>
<input type="submit" name="anyname" value="Add Staff"></p>
</form>
hope this help!
Related
I am trying to create a simple PHP Contact form using PHPMailer (because I also want to attach files through the form). And somehow something that should be really simple managed to give me headaches.
Here are some lines of code:
<form method="POST" action="" enctype= multipart/form-data">
.....
<input type="file" name="file">
</form>
.....
$file = $_POST['file']['tmp_name'];
echo $file;
My main problem is that I attach a file, complete all the fields, submit the form. I receive the email except the file attached. I tracked down and found out that, if I echo the $file var, It will display the first letter of the file.
Ex: if the file is named test.jpg, echoing $file will result in a t.
I have no idea what is this happening, judging the fact that there aren't too many lines of code and nothing that will change the filename..
Hope someone can help me out.
" missing in enctype and use $_FILES instead of $_POST
<form method="POST" action="" enctype= "multipart/form-data">
.....
<input type="file" name="file">
</form>
AND
$file = $_FILES['file']['tmp_name'];
<?php
var_dump($_POST);
var_dump($_FILES);
?>
<!doctype html>
<form id="register_form" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
<input type="file" name="file_upload" accept="image/*" /><br>
<input type="submit" value="Upload" id="submit_button">
</form>
When uploading a file, for example, tipsfedora.png, I get the file in the $_POST array, yet no files in the $_FILES array. Any help would be appreciated. Full code is posted. I would like to use the files with bulletProof image upload, yet the problem is that $_FILES is an empty array.
$_FILES contains content of uploaded files. But you actually didn't upload any files because the form doesn't have enctype attribute. It is required to enable file uploading.
But even you didn't upload any file, $_POST contains the file name.
Solution? Just add to enctype="multipart/form-data" to the <form>.
The MDN document about enctype may help you.
You forgot to add
enctype="multipart/form-data"
to the <form> tag
How can I redirect a page with a file POST data on it? Something like
Page1.php
<html>
<form action="page2.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Send"/>
</form>
</html>
Page2.php
<html>
<form action="page3.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/> ### get the value of file from page1.php and send to page 3
<input type="submit" value="Send"/>
</form>
</html>
Is it possible?
Not that way... the file is uploaded when the user submits the form. You could then base64 encode that file and embed it in the HTML as a hidden field to be submitted on the second form, but that's very inefficient.
You should handle the file upload on the first submission. Save the file somewhere or stick it in your database, whichever works better for the files you're receiving. If you save the file, I'd recommend generating a unique name for it (like a UUID). Then use the filename or the primary key from the DB as a value in a hidden field in the second form, which you can use to find the file you already received when the user submits the second form.
I keep getting an error:Notice: Undefined index: on line 35
line 35:
$handle = new Upload($_FILES['my_field']);
this is my input field
<input type="file" size="32" name="my_field" value="" />
I do not understand this error, thanks!!!
EDIT:
<form name="upload" id="upload" enctype="multipart/form-data" method="post" action="actions/upload.php" />
<p><input type="file" size="32" name="my_field" value="" /></p>
<p class="button"><input type="hidden" name="action" value="image" />
<br>
<input style="margin-left:224px;" type="submit" name="submit" value="upload" />
Update: The OP is doing an Ajax request - well that obviously can't work with a File upload.
Old answer:
I think I found it.
Look closely at this tag:
<form name="upload" id="upload" enctype="multipart/form-data" method="post"
action="actions/upload.php" />
the closing /> closes the form. Everything that comes afterwards, is not inside that form - it's inside a new one that the browser probably generates to deal with the broken markup. That new form is not enctype=multipart/form-data.
Did you use enctype="multipart/form-data" on the form element?
This seems to be the only reason that the the key my_field isn't set on $_FILES.
Edit: If your file is bigger than post_max_size, you also get an empty $_FILES array.
See also: Apache/PHP: $_FILES Array mysteriously empty
In order to upload WITH an ajax submission you can use an IFRAME with the upload part in there.
Send ajax form submission
Using javascript trigger submit() on the upload form in the IFRAME
Have the returning page in the iframe trigger a javascript complete response function in the main page
A bit complicated but would work. You would need some mechanism to tie them together, for instance if you are submiting info about the image, have step 1 return the id of the database row where that info is stored so that the IFRAME upload form can submit that id so it knows where to store the picture.
Hello everyone and thanks in advance.
I have a problem and I have 2 form into one another, the domestic form is to perform a file upload.
As I can do to make when sending in internal form not run the main form.
<form name="x" method="post" action="xxx.php">
....
<form action="" method="post" enctype="multipart/form-data" target="xxx">
<input type="file" />
<input type="submit" />
</form>
<iframe id="xxx" src="process.php">
</iframe>
....
<input type="submit" name="pro" value="Register user"/ >
</form>
Doing this does not work, as this within another form.
Any help or possible solution.
Forms cannot be nested. That's simply is not allowed in HTML and if you do so you might get undefined behavior which could vary between browsers. So try removing the inner form and put the enctype on the outer form.