Cannot Upload an Image To Database [duplicate]












0
















This question already has an answer here:




  • Why does file upload not work without the enctype property?

    2 answers



  • How to get useful error messages in PHP?

    29 answers




I have a problem where I cannot upload my image from the form to the database. The problem is that I cannot get the path of the image and the name of the image. As a result, it does not go through the if statements and does not upload to database. Any help will be appreciated.



  <form action="<?php echo htmlspecialchars('register_parse.php');?>" method='post'>
<div class="avatar"><label>Upload a picture: </label><input type="file" name="avatar" accept="image/*" required></div>
</form>


And here is the register_parse.php file:



  if(isset($_POST['email']) && isset($_POST['avatar'])){

$avatar_path = $mysqli->real_escape_string('img/'.$FILES['avatar']['name']);

// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}

else{
if(preg_match("!image!",$_FILES['avatar']['type'])){

if(copy($_FILES['avatar']['tmp_name'], $avatar_path)){
$_SESSION['avatar']=$avatar_path;


$sql = "INSERT INTO users (email, image) VALUES ('".$email."','".$avatar_path."')";

$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

if($mysqli->query($sql) === true){
$_SESSION['message'] = "Registration successful.";
header("location: success.php");
}
}
else{
$_SESSION['message'] = "User could not be added to the database.";
header("location: error.php");
}
}
else{
$_SESSION['message'] = "File upload failed";
header("location: error.php");
}
}
}

}
?>


It always goes to FILE Upload Failed. When I remove the last two if statements if(preg_match("!image!",$_FILES['avatar']['type'])) and if(copy($_FILES['avatar']['tmp_name'], $avatar_path)) , everything works, but in the database I get that the path of the image is just img/, so it cannot get the path of the image.










share|improve this question













marked as duplicate by Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 23:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You have $FILES instead of $_FILES....

    – Sloan Thrasher
    Nov 19 '18 at 23:26






  • 1





    Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

    – Diogo Santo
    Nov 19 '18 at 23:29











  • I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

    – Kaloyan
    Nov 20 '18 at 11:53
















0
















This question already has an answer here:




  • Why does file upload not work without the enctype property?

    2 answers



  • How to get useful error messages in PHP?

    29 answers




I have a problem where I cannot upload my image from the form to the database. The problem is that I cannot get the path of the image and the name of the image. As a result, it does not go through the if statements and does not upload to database. Any help will be appreciated.



  <form action="<?php echo htmlspecialchars('register_parse.php');?>" method='post'>
<div class="avatar"><label>Upload a picture: </label><input type="file" name="avatar" accept="image/*" required></div>
</form>


And here is the register_parse.php file:



  if(isset($_POST['email']) && isset($_POST['avatar'])){

$avatar_path = $mysqli->real_escape_string('img/'.$FILES['avatar']['name']);

// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}

else{
if(preg_match("!image!",$_FILES['avatar']['type'])){

if(copy($_FILES['avatar']['tmp_name'], $avatar_path)){
$_SESSION['avatar']=$avatar_path;


$sql = "INSERT INTO users (email, image) VALUES ('".$email."','".$avatar_path."')";

$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

if($mysqli->query($sql) === true){
$_SESSION['message'] = "Registration successful.";
header("location: success.php");
}
}
else{
$_SESSION['message'] = "User could not be added to the database.";
header("location: error.php");
}
}
else{
$_SESSION['message'] = "File upload failed";
header("location: error.php");
}
}
}

}
?>


It always goes to FILE Upload Failed. When I remove the last two if statements if(preg_match("!image!",$_FILES['avatar']['type'])) and if(copy($_FILES['avatar']['tmp_name'], $avatar_path)) , everything works, but in the database I get that the path of the image is just img/, so it cannot get the path of the image.










share|improve this question













marked as duplicate by Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 23:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You have $FILES instead of $_FILES....

    – Sloan Thrasher
    Nov 19 '18 at 23:26






  • 1





    Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

    – Diogo Santo
    Nov 19 '18 at 23:29











  • I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

    – Kaloyan
    Nov 20 '18 at 11:53














0












0








0









This question already has an answer here:




  • Why does file upload not work without the enctype property?

    2 answers



  • How to get useful error messages in PHP?

    29 answers




I have a problem where I cannot upload my image from the form to the database. The problem is that I cannot get the path of the image and the name of the image. As a result, it does not go through the if statements and does not upload to database. Any help will be appreciated.



  <form action="<?php echo htmlspecialchars('register_parse.php');?>" method='post'>
<div class="avatar"><label>Upload a picture: </label><input type="file" name="avatar" accept="image/*" required></div>
</form>


And here is the register_parse.php file:



  if(isset($_POST['email']) && isset($_POST['avatar'])){

$avatar_path = $mysqli->real_escape_string('img/'.$FILES['avatar']['name']);

// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}

else{
if(preg_match("!image!",$_FILES['avatar']['type'])){

if(copy($_FILES['avatar']['tmp_name'], $avatar_path)){
$_SESSION['avatar']=$avatar_path;


$sql = "INSERT INTO users (email, image) VALUES ('".$email."','".$avatar_path."')";

$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

if($mysqli->query($sql) === true){
$_SESSION['message'] = "Registration successful.";
header("location: success.php");
}
}
else{
$_SESSION['message'] = "User could not be added to the database.";
header("location: error.php");
}
}
else{
$_SESSION['message'] = "File upload failed";
header("location: error.php");
}
}
}

}
?>


It always goes to FILE Upload Failed. When I remove the last two if statements if(preg_match("!image!",$_FILES['avatar']['type'])) and if(copy($_FILES['avatar']['tmp_name'], $avatar_path)) , everything works, but in the database I get that the path of the image is just img/, so it cannot get the path of the image.










share|improve this question















This question already has an answer here:




  • Why does file upload not work without the enctype property?

    2 answers



  • How to get useful error messages in PHP?

    29 answers




I have a problem where I cannot upload my image from the form to the database. The problem is that I cannot get the path of the image and the name of the image. As a result, it does not go through the if statements and does not upload to database. Any help will be appreciated.



  <form action="<?php echo htmlspecialchars('register_parse.php');?>" method='post'>
<div class="avatar"><label>Upload a picture: </label><input type="file" name="avatar" accept="image/*" required></div>
</form>


And here is the register_parse.php file:



  if(isset($_POST['email']) && isset($_POST['avatar'])){

$avatar_path = $mysqli->real_escape_string('img/'.$FILES['avatar']['name']);

// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}

else{
if(preg_match("!image!",$_FILES['avatar']['type'])){

if(copy($_FILES['avatar']['tmp_name'], $avatar_path)){
$_SESSION['avatar']=$avatar_path;


$sql = "INSERT INTO users (email, image) VALUES ('".$email."','".$avatar_path."')";

$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

if($mysqli->query($sql) === true){
$_SESSION['message'] = "Registration successful.";
header("location: success.php");
}
}
else{
$_SESSION['message'] = "User could not be added to the database.";
header("location: error.php");
}
}
else{
$_SESSION['message'] = "File upload failed";
header("location: error.php");
}
}
}

}
?>


It always goes to FILE Upload Failed. When I remove the last two if statements if(preg_match("!image!",$_FILES['avatar']['type'])) and if(copy($_FILES['avatar']['tmp_name'], $avatar_path)) , everything works, but in the database I get that the path of the image is just img/, so it cannot get the path of the image.





This question already has an answer here:




  • Why does file upload not work without the enctype property?

    2 answers



  • How to get useful error messages in PHP?

    29 answers








php html mysql file-upload






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 23:18









KaloyanKaloyan

134




134




marked as duplicate by Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 23:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 23:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    You have $FILES instead of $_FILES....

    – Sloan Thrasher
    Nov 19 '18 at 23:26






  • 1





    Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

    – Diogo Santo
    Nov 19 '18 at 23:29











  • I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

    – Kaloyan
    Nov 20 '18 at 11:53














  • 1





    You have $FILES instead of $_FILES....

    – Sloan Thrasher
    Nov 19 '18 at 23:26






  • 1





    Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

    – Diogo Santo
    Nov 19 '18 at 23:29











  • I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

    – Kaloyan
    Nov 20 '18 at 11:53








1




1





You have $FILES instead of $_FILES....

– Sloan Thrasher
Nov 19 '18 at 23:26





You have $FILES instead of $_FILES....

– Sloan Thrasher
Nov 19 '18 at 23:26




1




1





Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

– Diogo Santo
Nov 19 '18 at 23:29





Does your image gets through? Normally you need method="post" enctype="multipart/form-data"> in your form

– Diogo Santo
Nov 19 '18 at 23:29













I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

– Kaloyan
Nov 20 '18 at 11:53





I added the enctype="multipart/form-data" and fixed $_FILES, but I am redirected to a complete blank page. Does anyone know why I am redirected to a blank page? When I remove the enctype="multipart/form-data" it goes again to File Upload failed.

– Kaloyan
Nov 20 '18 at 11:53












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?