php empty post array with file upload - php

<form method="post" enctype="multipart/form-data">
<h3>Add video</h3>
<div class="field_wrap">
<p>Name</p>
<input type="text" name="title"/>
</div>
<div class="field_wrap">
<p>Video</p>
<input name="video" type="file" />
</div>
<p><button>Add</button></p>
</form>
This is the form I use to upload a video file and a title, so when I DON'T upload a file the $_POST variable is filled with form data, but If I upload a file the $_POST variable is empty, why is that?

You are looking in the wrong spot, try looking in $_FILES not $_POST

<p><button>Add</button></p>
should be
<p><input type="submit" name="submit" value="Add"></p>
Using this all the post data is accessible as
$_POST and file data with $_FILES

Related

jquery mobile my action php page is not getting input file

hellow my this is my form in jquery mobile i also turbed off the data-ajax and define the enctype but still i am not getting my file in my action page
<form id="form_id" action="action/cat-manag-add.php" enctype="multipart/form-data" data-ajax="false" method="post">
<label class="TSG-font" for="category_image">Category image:</label>
<input class="required" type="file" name="file" id="file" value="" >
<form>
this my action page getting all $_POST values but not getting the $_FILES
$title=$_POST['title'];
$description=$_POST['description'];
$file=$_FILES['file'];
kindly help me
Remove value="" in input type file.
And try var_dump($_FILES); check what is print in you action page

fetching file directory from input type="file" in HTML form

I have such an upload form:
<form id="ajax-contact-form" action="" method="post" enctype="multipart/form-data" name="form1">
<INPUT type="text" name="name" value="Material Name:" onBlur="if(this.value=='') this.value='Material Name:'"
onFocus="if(this.value =='Material Name:' ) this.value=''">
<div class="clear"></div>
Choose a file to upload<br />
<!--APC hidden field-->
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/>
<div class="clear"></div>
<!---->
<input name="file" type="file" id="file" />
<div class="clear"></div>
<!--Include the iframe-->
<br />
<iframe id="upload_frame" name="upload_frame" frameborder="0" border="0" src="" scrolling="no" scrollbar="no" > </iframe>
<br />
<!---->
<INPUT class="submit" type="submit" name="submit" value="submit">
<div class="clear"></div>
</form>
I'm using php, and I have to fetch directory name of the uploaded file to use in azure.
How can I manage this?
Thanks.
If you are not using a framework, you could use the global $_FILES when processing the form.
That will be an array of each of the files you form submitted, using the "name" of the input as key (which in your sample form is missing).
Inside that key, you'll find "name", "type", "tmp_name", "error" and "size".
tmp_name will have the absolute path to the temporary file stored on your server. The rest is self explanatory.
Now, if you are using a framework to process the form, most frameworks have an easier and error-proof way of fetching files from the request.
Update
$_FILES['file']['tmp_name'] will have the absolute path on your server to the uploaded file.
With that, you can do file_get_contents() or whatever you like.
$_FILES['file']['name'] wil have the name of the file, in case you need to persist it somewhere.
If you are using a debugger such as xdebug, you could set a breakpoint, and look at the global yourself to get a better idea.
If not, you can always do a var_dump($_FILES); in order to view the structure and get a sense of what is happening on the server.

Smarty Uploading a File with normal Input Files

In my index.php file , I got a code is trace the $_FILE, but seems like when uploaded a pic/jpeg/images file, the return result is 'array(0) { }'. Did I need use smarty's method to assign a input file's method?
var_dump($_FILES);
my photoupload.tpl
<form action="index.php?view=photoupload" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input class="btn_name" type="submit" name="submit" value="Submit">
</form>
No, you dont have to, heres a part of my current project where i work with smarty:
<form action="upload_contract.php" enctype="multipart/form-data" method="post">
...
<div class="row">
<div class="large-5 columns">
<label for="upload">Dateiupload:</label><span><input type="file" id="upload" name="upload"></span>
</div>
</div>
This one works fine without additional methods.

POST array is empty when containing a file

I have a simple HTML form containing a file input. When the form is submitted without a file, printing the $_POST array shows me all of the data submitted. When a file is submitted, however, $_POST doesn't print out any of the submitted data.
Can somebody tell me why? This is my code:
<?php
print_r($_POST);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="myfile">Video File:</label>
<input type="file" name="myfile" />
<br /><br />
<label for="mytitle">Title:</label><br />
<input type="text" name="mytitle" size="55" maxlength="60" />
<br /><br />
<input type="submit" name="mysubmit" value="Submit Video for Approval" />
</form>
Your script seems fine. Please check your server configuration. Perhaps you exceed POST limits (set with post_max_size in php.ini)
You have to use $_FILES to access the uploaded files.
var_dump($_FILES); // Your uploaded files
var_dump($_POST); // Your entered data

JQuery AJAX equivalent of form's submit file

I have a form, like this
<form id="picuploadform" enctype="multipart/form-data" method="post" action="">
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload" />
<input type="hidden" id = "picssn" name="picssn" value="qweweqwq">
</div>
<div class="row" style="width:150px;">
<input type="submit" value="Upload" />
</div>
</form>
Which when I click the submit button I have PHP on this page to process it just fine. $_FILES for receiving file and $_POST for reading picssn that came with the hidden input. But due to problem with Jquery mobile I can't use submit or .submit() now. So I want to use
$.post('myphpfile.php', {picssn:$('#picssn').value(), file: $('#fileToUpload').xxxxxxxxxxxxxxxxx });
instead. But I have no idea how to grab the file out of that form and send it this way. Is there some method that can replace xxxxxxxxxxxxxx ? Thanks!
On your form element, you need to disable the ajax submit, and then fix your other code.
Try this:
<form id="picuploadform" enctype="multipart/form-data" method="post" action="#" data-ajax="false">
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload" />
<input type="hidden" id = "picssn" name="picssn" value="qweweqwq">
</div>
<div class="row" style="width:150px;">
<input type="submit" value="Upload" />
</div>
</form>
You cant upload directly via ajax. Check this perhaps:
http://blueimp.github.com/jQuery-File-Upload/ or uploadify.com

Categories