PHP files not detected using the $_FILES array [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 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];
}
?>

Related

PHP store html input in a global variable [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 4 years ago.
i am building a multi step login page like that of gmail,
i want the email address the the user typed in to be stored in a global variable so i can echo it in the next step of the form i have been trying but can't figure it out, have seen many error, when i change the code i get a different one the new one i have now is (Notice: Undefined index: email in C:\xampp\htdocs\xxxxx\index.php on line 3)
this is the form
<form action="" method="post" class="form-login">
<div class="step-login step-one">
<input type="text" class="email"/>
<input type="button" class="btn next-step" value="next">
</div>
</form>
this is the php code that i am trying to use and store the input, i think this is where the problem is
<?php
$emails = $_POST['email'];
if(isset($_POST['next'])){
$GLOBALS['email'] = $_GET['email'];
}
?>
this is the code where i am trying to echo the varible
<div class="data-user-find">
<p class="user-email"><?php echo $GLOBALS['email']; ?></p>
</div>
Please guys help me
The "value" of your button is "next", but your button isn't called "next", it should be <button name="next" value="next" type="submit">
Then your error would go away. Also, you define an $email variable outside of your if-clause, please put it inside your if clause, so your code would be this:
<?php if(isset($_POST["next"]) {
$email = $_POST["email"]; (DONT FORGET TO GIVE YOUR MAIL INPUT THE 'name="mail"' TAG)
$GLOBALS["email"] = $email;
} ?>

$_FILES array is always empty [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 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).

HTML - method POST in form does not send data in phpstorm [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 6 years ago.
I have this issue that my form obviously doesn't send data with POST method but it sends it with GET method.
Here is my HTML code of the form
<form action="action.php" method="POST">
<input type="text" name="text">
<input type="submit" value="send">
</form>
and here is the php code in the action page
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST['text'];
var_dump($_POST);
}
if(isset($_POST['text'])){
echo "ok";
}else{
echo "no";
}
when I submit the form I get this error for output
Notice: Undefined index: text in F:\test\action.php on line 9
array(0) { } no
but when I send data with GET method it works correctly without any problem.
I think the problem is for phpstorm because it runs correctly in the xampp server. and the considerable thing is when I run it in mozila or IE it says page not found but xampp is okay.
Try using isset with your input like so:
You have to add name="something" for the isset to pick up that you have clicked it.
<?php
if (isset($_POST['sub']))
{
echo $_POST['text'];
}
?>
<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="sub" value="Submit">
</form
I can only assume that the output you are seeing is before you submit the form. When you submit it, you should check for POST and not for post:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
^^^^ here
echo $_POST['text'];
}

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

PHP $_POST into array and output 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 7 years ago.
I have been trying to create a php script which takes any number from a text form, and puts it into an array. Then I want to loop through the array and output each number.
So far with some searching around on Google, I've come up with this piece of code. Unfortunatly it doesn't work. The strange thing is, I actually got it working at some point, but somehow the code wouldn't function anymore without actually changing anything in the script.
Can anyone help me complete this, I have my version which worked at some point but doesn't anymore.
(I know I should filter input but since this is just an exercise it doesn't really matter.)
<html>
<head>
<title>13.13</title>
<body>
<h2> Inputting numbers into array through form</h2>
<br>
<br>
<form>
<input type="text" name="number" />
<input type="button" value="submit" name="submit" />
</form>
<?php
session_start();
if (isset($_REQUEST['submit'])) {
$number = $_REQUEST['number'];
if (!isSet($_SESSION['number'])) {
$_SESSION['number'] = array();
}
array_push($_SESSION['number'], $number);
foreach($_SESSION['number'] as $key => $val) {
echo $key . ">" . $val;
}
}
?>
</body>
</html>
Move session_start() to the top of your code before any HTML output. You can't start a session after headers have already been sent - and they have been sent because HTML output has been sent.
It's just good practice anyway.
You need to define a method in your form.
Here' the full code, also, always place session_start at the top of the page.
<?php
session_start();
if (isset($_REQUEST['submit'])) {
$number = $_REQUEST['number'];
if (!isSet($_SESSION['number'])) {
$_SESSION['number'] = array();
}
array_push($_SESSION['number'], $number);
/* foreach($_SESSION['number'] as $key => $val) {
echo $key . ">" . $val;
}
*/
var_dump($_SESSION['number']);
}
?>
<html>
<head>
<title>13.13</title>
<body>
<h2> Inputting numbers into array through form</h2>
<br>
<br>
<form method="post" action="#">
<input type="text" name="number" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
Output

Categories