I'm trying to upload files to my laravel site using <input type="file"> yet it seems to be passing a string to the controller. I have this test to see if its a file yet I keep getting File not ok.
if (Input::hasFile('file')) {
dd('File ok');
}
dd('File not ok');
Any ideas?
Thanks
this may be you're not setting your form to enctype="multipart/form-data", it should be like this
<form method="POST" action="/your/url" enctype="multipart/form-data">
Or maybe, your input has no name attribute like
<input type="file" name="file">
Related
As part of a class I've been asked to create a simple file upload system that uploads files in a single button to a folder called ./files. I've been able to do this and I have been able to keep it all contained in one a3.php file with this:
<form action="a3.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submitFile">
</form>
Which opens up a file dialog and only requires the user to press two buttons
I use the following function to run the upload function.
if(isset($_POST['submitFile']))
{
uploadFile();
}
The uploadFile() function simply uses move_uploaded_file to move to file to the ./files folder. Currently without any questions asked.
My issue is that my attempts to make the buttons for the file upload a single one have been stymied. I have attempted to use the following code to resolve the issue:
<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" onchange="frm.submit();">
</form>
Which, while it refreshes the page as if an action is being performed, appears to do nothing. I have attempted to look into how exactly .submit() works but haven't gotten any closer to understanding any intricacies it may have while a part of onchange.
I've looked around and I've found code that can upload in a single button, and I've found code that can upload using an internal php function, but none that does both.
You can try using a button but not displaying it. When the file input is changed, using javascript trigger a click on the hidden button, like in the code below:
<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data">
<input type="submit" name="submitFile" style="display:none" id="submit">
<input type="file" name="uploadFile" onchange="document.getElementById('submit').click()">
</form>
<?php
if(isset($_POST['submitFile']))
{
uploadFile();
}
?>
You're basically looking for an ajax upload. You can do this using javascript (on modern browser), and the easiest thing would probably be using a library like jquery. Otherwise, you can do it using an iframe.
Here are a few similar questions with example scripts-
Using HTML5 file uploads with AJAX and jQuery
jQuery Ajax File Upload
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'];
Hello I needed code for automatically chose file and upload it to desired link. How to do that?
html code:
<html>
<head><title>Uploading</title></head>
<body>
<form method="post" enctype="multipart/form-data" action="uploadFile.php">
<input type="file" name="file" id="file">
<input type="submit" value="Submit">
</form>
</body>
</html>]
In above code at this line
<input type="file" name="file" id="file">
this path is fixed and can't be changed as user tries. so when user click on "Submit" button the file has to upload.
How to do this?
TL;DR: What you are trying to do is absolutely impossible - for a good reason.
If this was possible, you could create a hidden upload field pointing to a file containing valuable data (e.g. the browser's cookie database) and submit the form using JavaScript (or make the user submit it without knowing about that upload) and copy any file the user has access to.
I have a form for file upload like this
<form action="upload_file.php" 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>
Sometimes user upload files with 200 characters name.
Exist a way to limit filename maxlenghth?
The browser will expose the base file name to JavaScript so this is technically possible, although as #VisioN mentioned above, I can't think why you'd care; just rename the file on the server to something else. If you absolutely need to prevent it, then give your element an ID of uploader and then:
<script>
document.getElementById("uploader").onsubmit = function(){
return document.getElementById("file").value.length < 200;
};
</script>
Of course this will only work if JavaScript is enabled.
use the Code from Graham and check the filename length also with PHP
if(strlen($_POST['name']) > 200) { echo "filename to long"; exit; }
and you must added MAX_FILE_SIZE to you HTML-form otherwise PHP don't get the uploaded file
<input type="hidden" name="MAX_FILE_SIZE" value="MaximumFilesizeInBytes">
You should also validate it PHP side since someone could submit it directly to upload_file.php and bypass any HTML/JavaScript restrictions.
I have a question about the $_FILES global variable in php. When I do a print_r on my files array I get a blank array() 1 displayed (but I have uploaded one file with post from a form on another page). The post variable for upload (upload being the input type = file name) is set to the filename but nothing in Files is set and if I try and call $_FILES['upload']['name'] nothing shows up. What could be causing this? When I submit my form I have a bunch of different fields (text boxes, select boxes, checkboxes, etc.) but that shouldn't effect my file upload right?
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="test.php" method="POST" enctype="multipart/form-data">
You need to make sure your form has the enctype attribute, e.g.:
<form method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload"/>
<input type="submit"/>
</form>