0
I have some problem with this code below.
I have a form where the user has two options between simply writing a text and publishing or an image text. Unfortunately the $_FILES["file"]
is being identified giving an error similar to:
Notice: Undefined index: file in C: xampp htdocs zayonet start.php on line 99
Notice: Undefined index: file in C: xampp htdocs zayonet start.php on line 122
Notice: Undefined index: file in C: xampp htdocs zayonet start.php on line 123
How can I solve?
HTML:
<div class="col-md-12 col-sm-12 publish">
<form action="" method="POST" accept-charset="utf-8" enctype="multipart/form-date">
<textarea name="texto" placeholder="Write anything here"></textarea>
<label for="file-input"><img src="icons/foto1.png" alt="Insert Photo"
class="maquina-foto"></label>
<input type="submit" name="POSTER" value="Post" />
<input type="file" name="file" id="file-input" style="display:none;" />
</form>
</div>
PHP:
<?php
if (isset($_POST['POSTER'])) {
$error = $_FILES["file"]["error"];
if ($error) {
$texto = $_POST["texto"];
$date = date("Y-m-d");
if ($texto == "") {
echo "You have to write something";
}else{
$query_pubs = BD::conn()->prepare("INSERT INTO public (`user` , `texto` , `date`) VALUES (:e , :t , :d)");
$query_pubs->bindParam(":e", $_SESSION['email_user']);
$query_pubs->bindParam(":t", $texto);
$query_pubs->bindParam(":d", $date);
$query_pubs->execute();
if ($query_pubs) {
echo "<script language='javascript'>window.location='';</script>";
}else{
echo "Something did not go well ... Try later.";
}
}
}else{
$n = rand(0, 1000000);
$img = $n.$_FILES["file"]["name"];
$tmp_name=$_FILES["file"]["tmp_name"];
move_uploaded_file($tmp_name, "upload_imagens/".$img);
$texto = $_POST["texto"];
$date = date("Y-m-d");
if ($texto == "") {
echo "You have to write something";
}else{
$query_pubs_1 =BD::conn()->prepare("INSERT INTO public (`user` , `texto` , `imagem` , `date`) VALUES (:e_1, :t_1, :img, :d_1)");
$query_pubs_1->bindParam(":e_1", $_SESSION['email_user']);
$query_pubs_1->bindParam(":t_1", $texto);
$query_pubs_1->bindParam(":img", $img);
$query_pubs_1->bindParam(":d_1", $date);
$query_pubs_1->execute();
if ($query_pubs_1) {
//header("Location: ./");
echo '<script language="javascript">window.location=""</script>';
}else{
echo "Something did not go well ... try later. ";
}
}
}
}
?>
enctype="multipart/form-date"
is supposed to beenctype="multipart/form-data"
and confirm that the file is being sent by imprinting the contents of$_FILES
withprint_r($_FILES);
– Isac
Thank you very much!... I think that was the mistake that needed to be corrected
– Jose Pinto