Filename maxlength - php

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.

Related

Do modern browsers support onchange="this.form.submit()" for file upload?

I would like to have an upload form that automatically submits as soon as the user has selected a file.
This question has been asked many times before. If I understood the other threads correctly, this should work:
<form method="post" enctype="multipart/form-data">
<input id="fileToUpload" onchange="form.submit()" type="file"/>
</form>
<?php
if(isset($_FILES["fileToUpload"])){
echo "You successfully entered a file for the upload!";
// move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/my_image.png");
}
?>
When I select a file, I see the file name briefling flashing instead of the "no file chosen". But $_FILES is not set.
Replacing onchange="form.submit()" with onchange="alert('test');" leads to the expected alert box after file selection. (idea from here: https://stackoverflow.com/a/1904189/11826257)
I also tried it with an extra JavaScript function as described here: https://stackoverflow.com/a/12275917/11826257. That didn't work neither.
Do modern browsers block onchange="form.submit()" for type="file"?
I tried it with Firefox 68 and Microsoft Edge 84.
To allow $_FILES to detect your uploaded file, you need name attribute in your input tag. So change
<input id="fileToUpload" onchange="form.submit()" type="file"/>
to
<input id="fileToUpload" name="fileToUpload" onchange="form.submit()" type="file"/>
Form id values are not submitted by default. Only name values are. Try changing id to name in the input tag, e.g.:
<input name="fileToUpload" onchange="form.submit()" type="file"/>

File Input Passing String To Controller

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">

how to upload SWF format in php?

My source code is to print upload SWF file tmp_name ?
<?php
echo json_encode($_FILES);
?>
<form method="POST" action="#" enctype="multipart/form-data" novalidate>
<input type="file" id="image" name="image" />
<input id="send" type="submit" name="data" value="Submit" />
</form>
But result i'm getting
{"image":{"name":"file.swf","type":"","tmp_name":"","error":1,"size":0}}
I have tried image uploading method no use on that !
{"image":{"name":"file.swf","type":"","tmp_name":"","error":1,"size":0}}
^^^^^^^^^
This error isn't specific to Flash at all. You'd run into the same issue with other formats as well.
Error 1 is UPLOAD_ERR_INI_SIZE. It means that the file you tried to upload was larger than your server's maximum upload size (upload_max_filesize), so the server didn't accept it.
Change this configuration value, or upload a smaller file.

Automatically choose file and upload to desired server. How to do this?

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.

Uploading files in PHP

I am trying to make a site where users can upload files. I have some basic parameters (like making sure they're logged in, etc) above this code. I have narrowed down where my error is. Whenever I try to upload a file, it loads for a long time (even when it's just a small JPG), and displays nothing. I've looked through a bunch of previous questions, but they don't really help.
HTML
<form name="contact" action="action.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple><br><div id="adder"></div>
<input type="submit" name="submit" value="Submit">
</form>
PHP
for($i=0; $i<count($_FILES['file']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "uploads/".$_FILES['file']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)){
set_time_limit(0);
echo "Thanks for your submission! Your file was uploaded.;
}
change your this line:
<input type="file" name="file" id="file" multiple><br><div id="adder"></div>
to
<input type="file" name="file[]" id="file" multiple><br><div id="adder"></div>
and please write the proper code cause incomplete information will take people away for actual reason for the code to not work.( you did not close the for loop there)
also I am assuming you have checked the POST request using $_POST[] before saving anything to make sure someone actually submitted the form.

Categories