$_FILES array is always empty [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am trying to upload some files on server with PHP and HTML.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<body>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<input type='submit' name ='submit' value='Upload' />
</form>
</body>
</html>
PHP:
if(!empty($_FILES['submit']['name'][0])) {
$name_arr = $_Files['upload']['name'];
$tmp_arr = $_Files['upload']['tmp_name'];
$type_arr = $_Files['upload']['type'];
$error_arr = $_Files['upload']['error'];
echo count($tmp_arr);
for($i = 0; $i < count($tmp_arr); $i++) {
if(move_uploaded_file($tmp_arr[$i], "test_uploads/" . $name_arr[$i])) {
echo "Done";
} else {
echo "Nope";
}
}
} else
echo "What's going on?";
And I always get the "What's going on?" message or "the size of" e.g. $tmp_arr "is 0".

Use a var_dump($_FILES) to find out if the files are correctly uploaded.
If it was, Then you will see the array structure for all info.
This should be avoided: $_Files, always a good practice to use correct case ($_FILES).

Related

PHP files not detected using the $_FILES array [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 months ago.
The problem
The $_FILES['file'] array is set, yet it's empty whenever I try to use it.
What I tried
Googling
Setting file upload to On in php.ini (both Xampp and project file root)
Uploading one file at a time (just a wild try at fixing the problem
Debugging the entire code for a month trying to solve this problem
What I know for a fact
The path to the onSubmit is correct
The name of the input in the form and the name after $_FILES['file'] is exactly the same
The form has all it's required attributes
The input has type="file" and multiple in it
My code for the form(HTML) and the file engine(PHP)
<html>
<form method="POST" action="../php/post.php" enctype="multipart/form-data">
<h3>Title</h3>
<input type="hidden" name="case" value=1>
<input type="title" name="pname">
<h3>Message</h3>
<input type="message" name="pmsg">
<h3>Images</h3>
<input type="file" name="pimg[]" multiple>
<input class="submit" type="submit" value="Upload">
</form>
</html>
PHP
<?php
if (!empty($_FILES['file']['pimg'])){
$noFiles = 1;
echo "Files found...\n";
} else {
$noFiles = 0;
echo "Files not found...\n";
echo (!empty($_FILES['file']['pimg']));
echo $_FILES['file']['pimg'][0];
}
?>
Output
The If determines the array is empty, the last echo causes an error
You're not far off.
$_FILES['file']['pimg'] should be $_FILES['pimg']
You should also check to see if there is something selected. $_FILES['pimg']['size'] checks the filesize. If 0, nothing is selected
This way works.
<?php
if ($_FILES['pimg']['size'] != 0){
$noFiles = 1;
echo "Files found...\n";
echo $_FILES['pimg']['name'][0];
} else {
$noFiles = 0;
echo "Files not found...\n";
/* This will always be empty placed here as no files have been found */
//echo (!empty($_FILES['file']['pimg']));
//echo $_FILES['file']['pimg']['name'][0];
}
?>

Undefined variable: details in C:\xampp\htdocs\redo\consultation.php on line 21 in Php [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
<?php include 'connection.php'; ?>
<?php
if (isset($_GET['consultation']))
{
echo "Consultation Details";
$no=$_GET['consultation'];
?>
<form class="" method="get">
<textarea name="details" rows="8" cols="80" placeholder="enter consultation details"></textarea><br>
<button type="submit" name="c">submit</button>
</form>
<?php
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
//$details= $_GET['details'];
$insertQuery="INSERT INTO redo (consultation) VALUES ($details) WHERE no=$no;";
$insert = mysqli_query($conn,$insertQuery);
if ($insert) {
echo "recorded";
?>
Back to Total Patients
Back to Registration
<?php
}
else {
echo "record couldnt be inserted";
}
}
?>
No $_GET['details'], you sure you're using a GET method in the form? Change $_GET['details'] to $_REQUEST['details'].
To be exact, this one fails:
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
I mean, the if condition is not met, so $details is not defined, as you don't define it anywhere in the code outside that if.
Other solution would be adding:
$details = '';
in front of that if.

Isset is not working [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP & Case Sensitivity [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with isset. What I want is some echos after getting, checking and initialising the values from isset. it doesn't work. I don't see any reason behind this. It only refreshes the page after submitting the form.
Below is the code
<?php
if (isset($_post['contact_name'] ) && isset($_post['contact_email']) && isset($_post['contact_text']))
{
echo $contact_name = $_post["contact_name"];
echo $contact_email = $_post['contact_email'];
echo $contact_text = $_post['contact_text'];
//!empty($_post['contact_name'])
//echo 'no value submit';
if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text))
{
echo 'ok';
}
else
{
echo 'all fildes required !!!!!!!!!!!!!';
}
}
?>
<html>
<form action="index.php" method="post">
name : <br> <input type="text" name="contact_name" ><br><br>
Email address : <br> <input type="text" name= "contact_email "><br><br>
Message :<br><textarea name="contact_text" rows="6" cols="30" type="text" ></textarea><br><br>
<input type="submit" value="send">
</form>
</html>
You need to use this;
<?php
if (isset($_POST['contact_name'] ) && isset($_POST['contact_email']) && isset($_POST['contact_text']))
{
...
Use $_POST instead of $_post. Hope this will work.

<br /><b>Notice</b>: Undefined variable: user_name in <b>C:\xampp\htdocs\form2\edit.php</b> on line <b>7</b><br /> [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<!doctype html>
<html>
<head>
<title></title></head>
<body>
<form action="#" method="POST" enctype="multipart/form-data">
<input type="text" name="usname" value = "<?=$user_name;?>"/>
<input type="password" name="psd" value = "<?=$pass_word;?>" />
<input type="textarea" name="addrs" value = "<?=$address;?>">
<input type="hidden" name="id" value = "<?=$id?>"/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($_POST['submit'])){
$sql= "Update form set user_name='".$_POST['usname']."',
pass_word='".$_POST['psd']."',
address='".$_POST['addrs']."'
where id='".$_GET['id']."'
";
$result=mysql_query($sql);
if($result)
{
echo"data is succesfully data insert";
header("location:login.php");
}else
{echo"";}
}
$id='';
$user_name='';
$pass_word='';
$address='';
$host="localhost";
$username="root";
$password="";
$db="form2";
$con=mysql_connect($host,$username,$password);
if(isset($_GET['id']))
{
echo $query = "select id,user_name,pass_word,address from form where id=".$_GET['id'];
mysql_select_db($db,$con);
echo $result=mysql_query($query);
if($result){
while($row= mysql_fetch_array($result))
$id=$row['id'];
$user_name=$row['user_name'];
$pass_word=$row['pass_word'];
$address=$row['address'];
}
}
?>
</body>
</html>
Notice: Undefined variable: user_name in C:\xampp\htdocs\form2\edit.php on line 7
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Notice: Undefined variable: address in C:\xampp\htdocs\form2\edit.php on line 9
submit
i am create a edit.php and update the values
I'm running a PHP script, and maintain getting errors like:
If you are using some variable then please check if its set or not
<?=$user_name;?> you need to chnage to <?=(isset($user_name)?$user_name:""?> and same for others.

showing undefined index when try to submit the form [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
Every time I hit submit button after selecting an image will face the problem "Notice: Undefined index: profile_picture in C:\wamp\www\Test\testImage.php on line 5", and the page print the text not selected which must be display if image is not selected.
The index is defined, but why it is behave like this, please help with your suggestions
<?php
$photo = "";
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$photo = trim(stripslashes($_POST["profile_picture"]));
if($photo != '')
{
//$photo = trim(stripslashes($_POST["profile_picture"]));
header("Location: success.php");
}
else{
echo 'not selected';
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form name="signup_form" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" class="profile_picture" id="profile_picture" name="profile_picture"> </br>
<input type="submit" name="submit_btn" id="submit_btn" class="submit_btn" value="sign up">
</form>
</body>
</html>
Files are not posted over $_POST.
They are posted with $_FILES array.
Reference

Categories