Echo $_FILES giving no results - php

I'm trying to make a simple file upload while learning how to do so.
code looks as following:
<form action="upload.php" method="post">
<input type="file" multiple="multiple" name="files[]"
enctype="multipart/form-data"><br><br>
<input type="submit" value="upload">
</form>
<?php
echo $_FILES["files"]['name']
?php
For some reason i get 0 results, I cant figure out why. I know that when I upload more files it'd be wise to run through them with a foreach loop, but for the sake of simplicity I'd rather have the results of 1 file first.

You need to correct your html like this
<form action="upload.php" enctype="multipart/form-data" method="post">
Then in upload.php:
print_r($_FILES);

Instead of echo try with
print_r($_FILES)
Also try to add to form tag code below
enctype="multipart/form-data"

I'm assuming both the HTML form and PHP upload code is written within this file upload.php. Please correct me if I'm wrong.
Change your form HTML to:
<form action="" enctype="multipart/form-data" method="post"> <!-- Form action can be omitted. enctype attribute is needed -->
<input type="file" multiple="multiple" name="files[]"/>
<br/><br/>
<input type="submit" value="upload" name ="submit" /> <!-- Add name here -->
</form>
Modify your PHP code:
<?php
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_FILES);
}
?>
Hope this helps.
Peace! xD

Correct html for you will be:
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="file" multiple="multiple" name="files[]"/>
<br/><br/>
<input type="submit" value="upload"/>
</form>
it must work
You may also try:
var_dump($_FILES);
print_r($_FILES);

Related

php undefined index coming on uploading file with ['name']

<form action='' method='post'>
<input type="file" class="form-control" name="kk">
<input type="submit" class="form-control" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES["kk"]["name"];
}
?>
hi,i'm new to php,In this code undefined index kk coming on uploading file,i just want the file name...please help me out
add enctype="multipart/form-data" to form tag
You need to pass enctype="multipart/form-data" when you have to upload file
<form action='' method='post' enctype="multipart/form-data">
<input type="file" class="form-control" name="kk">
<input type="submit" class="form-control" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES["kk"]["name"];
}
?>

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.

multiple image uploading using php

i'm trying to upload multiple files at one time but not getting proper result. my code is as follow:
<?php
if (isset($_FILES['uploadfiles'])) {
print_r($_FILES);
}
?>
<form action="index.php" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="uploadfiles" multiple="multiple" min="1" max="999"/>
<input type="submit" name="Upload"/>
</p>
</form>
when i use print_r($_FILES) it only dispays the first selected file details. kindly help me.
Try uploadfiles named as an array:
<input type="file" name="uploadfiles[]" multiple="multiple" min="1" max="999"/>

how to pass php variable value to action attribute of html form

I want to pass php variable value as a action to html form. I am trying as follows, but it is not working.
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
All this code are in one php file.
Have you tried <?php echo $url ?>
If it works, then short_open_tag in the php.ini is turned off. That means you will need to either turn it on or use the long open tag <?php throughout your code.
Sounds like you need to enable short_open_tag if your example doesn't work.
<?php
ini_set('short_open_tag', 'on');
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Alternately, write it like this:
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?php echo $url ?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Try this
<form name="upload" action="<? echo $url ?>" method="post" >
Remove your single quotes:
<form name="upload" action="<?=$url?>" method="post">

Why wont this simple code work all of a sudden?

Why won't this print "success" when I submit the form? I'm pretty sure it should.
<?php
if (count($_POST) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile" />
<input type="submit" value="upload" />
</form>
At a guess, the submit field has no name, so it won't be included in $_POST. Your file upload will be placed in $_FILES, see Handling file uploads.
It is also good practice to NOT ommit the action attribute.
If you want the form to submit to itself, try
<form method="post" action="?" enctype="multipart/form-data">
or
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
Further reading on second method Disclaimer: Link to my own blog
<?php
if (count($_POST['submit']) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="userfile" />
<input type="submit" value="upload" name="submit"/>
</form>

Categories