I have this form:
<label for="pdffile">Upload</label>
<form class="form-horizontal" role="form" method="POST" name="upload" id="upload" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" id="pdffile" style="display:none" form="upload"
</form>
It is supposed to be submitted with this jquery:
$('#pdffile').change(function() {
$('#upload').attr("action", "/upload").submit();
});
However when I check in PHP, nothing is uploaded:
dd($request)
Always gives me back null instead of the requested item.
Any help?
Your input does not have name
<input type="file" id="pdffile" style="display:none" form="upload"
change to
<input type="file" id="pdffile" style="display:none" name="upload"/>
and in your action you can use $_FILES to get upload file
Related
I'm using Laravel form request to validate a array of input group. I need to check files using a loop for a custom validation. But it shows this error.
Error
Call to undefined method Symfony\Component\HttpFoundation\ParameterBag::hasFile()
My HTML inputs (createUser.blade.php)
<input type="text" name="users[0][name]">
<input type="number" name="users[0][age]">
<input type="file" name="users[0][profile_pic]">
<input type="text" name="users[1][name]">
<input type="number" name="users[1][age]">
<input type="file" name="users[1][profile_pic]">
My Form Request (CreateUserFormRequest.php)
dd($this->request->hasFile('users');
Inside FormRequest class if you want to check whether request has value or not then you can do like below
$this->has('users')
If you want to check request has file or not then
$this->hasFile('logo')
also make sure input type is file
To check all data in request then
$this->all()
For files make sure input type is file
<input type="file" name="users[1][profile_pic]">
Also make sure enctype="multipart/form-data" in form tag
<form method="POST" action="" enctype="multipart/form-data">
Updated
<form method="POST" action="{{route("testing")}}" enctype="multipart/form-data">
#csrf
<input type="number" name="users[0][name]">
<input type="number" name="users[0][age]">
<input type="file" name="users[0][profile_pic]">
<input type="number" name="users[1][name]">
<input type="number" name="users[1][age]">
<input type="file" name="users[1][profile_pic]">
<button type="submit">Submit</button>
</form>
and in Form request
foreach ($this->users as $key=>$value){
if($value['profile_pic']!=null){
dump($value['profile_pic']);
}
}
If you are looking for file validation then you can do the following
public function rules()
{
return [
'users.*.profile_pic'=>'required|file'
];
}
and in your view
<input type="file" name="users[0][profile_pic]">
{{ $errors->first('users.0.profile_pic') }}
<input type="file" name="users[1][profile_pic]">
{{ $errors->first('users.1.profile_pic') }}
<label class="labelFile">Bestand kiezen...</label>
<input type="file" name="images_1" class="fileChoose" accept="image/*"></br></br></br>
<label class="labelFile">Bestand kiezen...</label>
<input type="file" name="images_2" class="fileChoose" accept="image/*"></br></br></br>
It only fills the first image and the second one it gives an errors: "4", how to fix it,
I want multiple input fields not 1 multiple :D
if you put the form with
<form method="POST" enctype="multipart/form-data" >
two items are returned to you
try like this
<?php
var_dump($_FILES['images_1']);
var_dump($_FILES['images_2']);
?>
<form method="POST" enctype="multipart/form-data" >
<label class="labelFile">Bestand kiezen...</label>
<input type="file" name="images_1" class="fileChoose" accept="image/*"></br></br></br>
<label class="labelFile">Bestand kiezen...</label>
<input type="file" name="images_2" class="fileChoose" accept="image/*"></br></br></br>
<button value="ppp" type="submit">send </button>
</form>
I'm trying to create a simple fileupload form in my custom Wordpress template:
<form action="<?php echo $current_url = home_url(add_query_arg(array(),$wp->request)); ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
On submit, the $_FILES array is empty.
I think the problem is in the action, as the url that is generated is the nice URL, instead of http://domain.com/somepost.php
What should I put into the action, so I get the $_FILES?
Try this
<?php
if(isset($_POST['submit'])){
if($_FILES["file"]["error"] > 0){
echo $_FILES["file"]["error"];
}
else{
echo $_FILES['file']["name"]; // your file name
}
}
?>
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="submit"/>
</form>
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.
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