Broken file upload with bootstrap - php

With Bootstrap 4.0.0
<div class="container">
<div class="jumbotron mt-3" style="padding: 0.6em 1.6em;">
<h4>Uploader</h4>
<form enctype="multipart/form-data" action="/myurl/?action=upload" method="POST">
<input type="file" class="form-control-file">
<input type="submit" class="btn btn-primary" name="Upload">
</form>
</div>
</div>
In combination with this PHP code on the other side:
<?php var_dump($_FILES); ?>
returns:
array(0) { }
In other words: I simply select a file, /myurl/?action=upload is called, but empty($_FILES['myfile']) is TRUE / no files seems to be passed.
What is the issue?
Thanks

The issue is that you haven't provided a proper name attribute in your input field.
<input type="file" class="form-control-file" name="uploaded_file">
Would likely help this issue, but without seeing your PHP, it could be other issues.

Related

PHP 8.1 $_FILES missing type property

I am upgrading my website from PHP 7.4 to 8.1 which is causing an unusual issue with $_FILES. I'm trying to upload a new file but nothing happens. This issue cannot be regarding the file size as I'm trying to upload .txt files containing a single word.
Example of my code:
<form action="client.php" method="POST" enctype="multipart/form-data" onSubmit="javascript: return validateFile(this);">
<div class="card-header">
Upload Section
</div>
<div class="card-body">
<div class="position-relative form-group">
<label >Select file from local disk</label>
<input type="file" name="test" size="90" maxlength="500" class="form-control-file">
</div>
<input class="mt-1 btn btn-primary" name='Submit' type='submit' value='Upload File'>
</div>
</form>
When the from is posted the data within $_FILES is like so
test={ name="test.txt, full_path=test.txt, type="", tmp_name="", error={int} 6, size={int} 0}
The PHP website did not have access to the "C:\WINDOWS\Temp" directory. Which was easily fixed by changing the security permissions.

search form action url not display php variable

hi can anyone tell me whats problem in this form . its not show varible in url
<form class="navbar-form navbar-left" method="post" action="test.php?q=<?php echo $searchb;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
php code here
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
when something input in form and action url not show any value
test.php?q=
but we echo variable its show value .
First time that form loaded $_POST['searchb'] is empty so action is equal test.php?q= after load form when you submit form then $_POST['searchb'] to be filled
The Part: action="test.php?q=<?php echo $searchb;?>" is first illogical and most importantly unnecessary since you are POSTing your form. It would have been valid if $searchb was pre-defined. However, since it is a part of the Form; it will always be NULL since it was never declared but expected to be dynamically added on Form-Submit, which wouldn't happen. You do it in one of the 2 ways:
OPTION #1 - PASSING q VIA HIDDEN INPUT:
<!-- YOU DON'T NEED THE echo $searchb PART IN YOUR FORM'S ACTION BECAUSE -->
<!-- THAT VALUE IS NOT PART OF THE ACTION AS IT IS NOT EVEN SET AT ALL -->
<form class="navbar-form navbar-left" method="post" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
<!-- ADD THE q AS HIDDEN INPUT ELEMENT WITH A VALUE -->
<input type="HIDDEN" name="q" value="Some value" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
OPTION #2: USING GET & SETTING Q TO A PRE-DEFINED VALUE
<?php $param = "some-predefined-value"; ?>
<form class="navbar-form navbar-left" method="GET" action="test.php?<?php echo $param;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['searchb'])) {
$searchb = $_GET['searchb'];
}
BETTER OPTION FOR YOUR USE-CASE: USING GET & SETTING Q FROM THE INPUT
<!-- STILL NO NEED FOR SETTING QUERY PARAMETERS MANUALLY-->
<!-- THE GET METHOD WOULD TAKE CARE OF THAT FOR YOU ONCE THE FORM IS SUBMITTED -->
<form class="navbar-form navbar-left" method="GET" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<!-- NOTICE THAT THE NAME OF THE INPUT FIELD CHANGED TO; q HERE -->
<input type="text" name="q" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['q'])) {
$searchb = $_GET['q'];
}

I have a post form that submits but "$_POST" variable is empty even though my web inspector indicates otherwise

Here is my html:
<form id="quant" method="POST" action="/portfolio" name="trans">
<div class="modal-body">
Shares : <input autocomplete="off" type="text" name="quantity">
<input type="hidden" value="<?= htmlspecialchars($_GET['q']); ?>" name="stock">
<input type="submit" name="val" value="Confirm Transaction" class="btn btn-primary">
<div id="slider-range-max"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
Here is an image of my web inspector showing that in fact that data was requested:
I have been trying to figure out why this has not been working for a long time, any suggestions would be appreciated.
The Code:
if(isset($_POST["quantity"])) {
var_dump($_POST);
which returns nothing
Then you need a slash after it to let it know it is a folder and not a file: action="/portfolio/"
If you're using something like AngularJS to submit your form, it doesn't send POST data the typical way. Check out this post.

Cannot upload file through HTML form

I am planning to make a form which has a form through which users can upload a file on the website.
I used a CSS template where a class called pop-up-wrapper and pop up is defined. However, when I use the following code :
<div>
<div class="form-pop-up">
<div class="form-pop-up-wrapper">
<ul class="contact-form">
<form id="form-submit" method="post" enctype="multipart/form-data">
<li>
<p class="iam-white center-al">Upload file</p>
<input type="file" name="resume" id="user-resume">
</li>
<li>
<p> </p>
<input type="submit" name="submit" value="Submit" id="form-submit" class="mar">
</li>
</form>
</ul>
</div>
</div>
</div>
I have a PHP code which validates if any file has been entered by checking with
isset($_FILES['file']
but this always returns 0.
Is there anything in the CSS stylesheet which can lead to this situation where $_FILES is unable to catch the uploaded file?
TIA
You have to use the name in $_FILES, not the type.
isset($_FILES['resume'])
Just a shot here, but I don't think it's your CSS that's causing the problem. Try:
isset($_FILES['resume'])
instead.

Twitter Bootstrap FileUpload issue

I would like to use Twitter Boostrap FileUpload because I like the preview function it offers. This and some text I would like to post to a php script that does the uploading and adds info to a db. I dont necessarily need php to do the uploading of the file if javascript can do it I just need the link too it.
This question has been asked before but I can't find any working answer there. And no I dont want to use bootstrap file-upload that is suggested in the first answer.
In my php code I try to get the imgData with _FILE but its empty. How can I get this?
this is my form:
<form action="php/service.php" method="post">
<div class="control-group">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-file">
<span class="fileupload-new">Select image</span>
<span class="fileupload-exists">Change</span>
<input type="file" name="imgData" id="imgData" />
</span>
Remove
</div>
</div>
</div>
<div class="control-group">
<textarea rows="3" id="text" class="" name="text" placeholder="Text..."></textarea>
</div>
<div class="control-group">
<button type="submit" id="upload" class="btn" onclick="$('.fileupload').fileupload();">Upload</button>
</div>
</form>
in service.php:
if ($_FILES["imgData"]["error"] > 0) {
echo "Error: " . $_FILES["imgData"]["error"] . "<br />";
} else {
$fileExt = $_FILES["imgData"]["type"];
}
You need to set the encoding type for the form.
<form action="php/service.php" method="post" enctype="multipart/form-data">
I also had difficulties with this bootstrap way of uploading files in IE. There might be some security issues in that one that prevents uploading.

Categories