Reputation: 123
I'm having some trouble with a Form. It contains an input type File for uploading an image, and a select tag to choose where to put this image. Code goes like this:
<?php
require_once("functions.php");
if(isset($_COOKIE['user']))
{
$username= checkcookie($_COOKIE['user']);
}
if (isset($_SESSION['user'])) {
if ($_POST) {
$errors = array();
$errors = SomeValidation();
if (empty($errors )) {
UpdateImages();
exit;
}else {
Header ("location: somefile.php?error=There was an error");
exit;
}
}else{
var_dump("error");
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<?php if(isset($_GET['error'])) {?>
<div class="alert alert-danger" role="alert">
<ul>
<li><?php echo $_GET['error'] ?></li>
</ul>
</div>
<?php } ?>
<div class="cbp-mc-column">
<label>Choose new Image</label>
<select id="code" name="code">
<option value="1" >First</option>
<option value="2" >Second</option>
<option value="3" >Third</option>
</select>
<br/>
<input type="file" name="newimage" value="" />
<br>
<input type="submit" name="update" value="Update">
<br>
</div>
</form>
<!-- Bootstrap Core JavaScript -->
<script src="../js/bootstrap.min.js"></script>
</body>
</html>
<?php }else
{
header('location: panel.php');
}
?>
I'm always getting False for if($_POST)
, and also when I submit the form.
Any idea why?
EDIT: I forgot to mention, I'm sending the select
value via post, so I can choose which file upload, and It is always empty.
Upvotes: 2
Views: 871
Reputation: 8618
First, you need to check if the submit button was clicked :isset($_POST['update'])
Next, check if a file has been selected: isset($_FILES['newimage']['tmp_name'])
if (isset($_POST['update'])) {
if (isset($_FILES['newimage']['tmp_name'])) {
$errors = array();
$errors = SomeValidation();
if (empty($errors )) {
UpdateImages();
exit;
} else {
Header ("location: somefile.php?error=There was an error");
exit;
}
} else {
echo "No file selected.";
}
} else {
echo "Not a POST request.";
}
Upvotes: 0
Reputation: 190
to upload image or any other file you need to check the $_FILES array not post, this question could be helpfull
How do you loop through $_FILES array?
Upvotes: 1
Reputation: 5306
It is better to check inputs instead of request method.. so use
if (!empty($_POST["update"])) {
....
}
Upvotes: 0