Undefined Index error when uploading files - php

So, I have a form with two input elements: one to upload a file, and the other, a hidden input. Here it is:
<form action="upload.php" method="POST">
upload: <input enctype="multipart/form-data" name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
on my "upload.php" page (which is a different page), I get the undefined index error. Here's the code for that:
<?php
session_start();
if(isset($_POST["eup"])){
$fERR=false;
if(isset($_FILES["pp"])){ // undefined index error comes if this IF is removed...
$aExts = array("png");
$temp = explode(".", $_FILES["pp"]["name"]);
$tEXT = end($temp);
if ((($_FILES["pp"]["type"]=="image/x-png")
|| ($_FILES["pp"]["type"]=="image/png"))
&& ($_FILES["pp"]["size"]<300000)
&& in_array($tEXT, $aExts)){
if ($_FILES["pp"]["error"]>0){
$ppERR=true;
}
else{
// handle file upload here
}
}
else{
$fERR=true;
}
}
else{
$fERR=true;
}
if($fERR==true){
echo "ERROR";
}else{
echo "GOOD TO GO";
}
}
echo "<br />".ini_get("file_uploads");
?>
I've looked at a bunch of other posts and websites discussing this topic, but none of the solutions worked for me.
by the way, the output of that php is:
ERROR
1

<form action="upload.php" method="POST" enctype="multipart/form-data">
upload: <input name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>

Related

$_FILES[""][""] is empty in uploading files

I really Dont know What should I do the problem is that $_Files is always empty when I try to upload a file.
<form method="get" action="pic.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
<?php
$name=$_FILES["file"]["name"];
$tmp_name=$_FILES["file"]["tmp_name"];
echo "php is talking";
echo $name;
if(isset($name)&&!empty($name))
{
echo "OK";
$location="uploads/";
if(move_uploaded_file($name,$location.$name))
{
echo "the file has been Uploaded";
}
}
?>
You have to change the HTTP method in the form from GET to POST
<form method="POST" action="pic.php" enctype="multipart/form-data">

PHP uploaded file name doesnt show up in browser

All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>

check if file has been uploaded by user in a form

I have a form where multiple images can be selected...
HTML
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="name" class="form-field" placeholder="First Name">
<input type="file" name="files[]" multiple accept="image/*" class="form-field">
<input type="submit" value="UPLOAD" class="button" name="submit4">
</form>
Then, in my PHP I want to detect if at least 1 file has been uploaded.... I tried this, but the code still triggers as if a file was uploaded...
if(isset($_POST['submit4']) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "post sucessful<br>";
if(is_uploaded_file($_FILES['files']['temp_name']))
{ echo "file exist";}
else{ echo "no file";}
}
THIS always triggers the "no file" even if there is a file..
and if I try:
if(!empty($_FILES['files']['temp_name']))
it always triggers the "no file" ... I'm really confused...
You can use following code
if(!file_exists($_FILES['files']['tmp_name']) || !is_uploaded_file($_FILES['files']['tmp_name'])) {
echo "No File";
}
else{
echo "Successfully uploaded";
}
Found my answer...
if(!empty($_FILES['files']['name'][0]))
works quit well... thanks everyone.

PHP: 2 Forms, 1 Being File Upload

I have a single profile page that I want to upload a photo on as a separate action. I have the first form submitting to the page successfully, it is when I submit the photo form that the page returns blank with an empty message.
HTML
<form method="post" action="profile.php" id="main">
<input name="txtFirstName" type="text" value="<?php echo $sFirstName; ?>">
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<form method="post" action="profile.php" id="photo_upload" enctype="multipart/form-data">
<img src="<?php echo($sPath); ?>" height='100' width='100' id="imgProfile" />
<br>
<input type="file" name="fUpload" id="fUpload">
<br>
<input type="submit" name="btnUploadPhoto" value="Upload" class="cancel"/>
</form>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['btnSubmit']) {
$sFirstName = $_POST['txtFirstName'];
}
else if ($_POST['btnUploadPhoto']) {
// DO MY MOVE LOGIC
}
}
When the "btnSubmit" is called the page loads and the textbox gets the name that was entered. When the "btnUploadPhoto" is called I get this on the screen and nothing else:
{error: '', msg: '' }
How do I get the page to reload with the original form?
As you are uploading a file, you will want to check if a file has been submitted in your PHP code. Here is the code to do this:
isset($_FILES['fUpload'])

dynamic number of file upload - PHP

I'm creating a page when user can enter how many files he want to upload then that page is rendered again with a form, after submission to another page the files are uploaded.
But i'm receiving an error [if i upload 1 file, else the problem is userfile[limit-1]]
Notice: Undefined index: userfile0 in E:\wamp\www\uploader\uploader.php on line 10
I'm pasting here my code
index.php
<html>
<?php
if (isset($_POST['num']) && !empty($_POST['num']))
{
?>
<form name="uploader" action="uploader.php" method="post">
<table>
<tr><td>Title</td><td>Select File</td><td>Description</td></tr>
<input type="text" name="number" value="<?php echo $_POST['num']; ?>"/>
<?php
for ($i=0;$i<$_POST['num'];$i++)
echo '<tr><td><input type="text" name="title'.$i.'"/></td>
<td><input type="file" id="userfile'.$i.'" name="userfile'.$i.'" size="30"></td>
<td><textarea name="desc'.$i.'" rows="4"></textarea></td></tr>';
?>
</table>
<input type="submit" name="b" value="Submit"/>
</form>
<?php
}
else
{
?>
<form name="form" method="post">
How many files to upload ? <input type="text" name="num" value="1"/>
<input type="submit" value="Submit"/>
</form>
<?php
}
?>
</html>
uploader.php
<?php
if (isset($_POST['number']))
{
for ($slot = 0; $slot < $_POST['number']; $slot++)
{
$title = $_POST["title$slot"];
$desc = $_POST["desc$slot"];
if (move_uploaded_file($_FILES["userfile$slot"]['tmp_name'],$_FILES["userfile$slot"]['name']))
echo $name.' file Uploaded !';
else
echo ' file not Uploaded ! ';
}
}
?>
Edit
SQL code removed
You need an enctype="multipart/form-data" on your <form> tag, otherwise it cannot upload files/PHP cannot read it.
You should also check the existance of a file before trying to upload it.
if (isset($_FILES['userfile'.$slot])) ...

Categories