user3385997 user3385997 - 1 year ago 65
PHP Question

Why is my PHP keep saying "Please upload an image"?

I'm trying to have the registration come out to "You are successfully registered" by having the image transferred into the images folder. However, it keeps passing it, and goes to the else statement: "Please upload an image."

I've looked over the logic and syntax, and it looks correct. But, can't register correctly.




$error = "";

if(isset($_POST['submit'])) {

$firstName = $_POST['fname'];
$lastName = $_POST['lname'];
$email = $_POST['email'];
$password = $_POST['password'];
$passwordConfirm = $_POST['passwordConfirm'];

$image = $_FILES['image']['name'];
$tmp_image = $_FILES['image']['tmp_name'];
$tmp_image = $_FILES['image']['size'];

if(strlen($firstName) < 3) {
$error = "First name is too short.";
else if(strlen($lastName) < 1) {
$error = "Last name is too short.";
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Please enter valid email address.";
else if(strlen($password) < 8) {
$error = "Password must be greater than 8 characters.";
else if($password !== $passwordConfirm) {
$error = "Password does not match.";
else if($image == "") {
$error = "Please upload an image.";
else {
$insertQuery = "INSERT INTO users(firstName, lastName, email, password, image) VALUES('$firstName','$lastName','$email','$password','$image')";

if(mysqli_query($conn,$insertQuery)) {
if(move_uploaded_file($tmp_image,"images/$image")) {
$error = "You are successfully registered!";
else {
$error = "Image is not uploaded.";


<!DOCTYPE html>
<title>Registration Page</title>
<link rel="stylesheet" type="text/css" href="css/style.css">

<div id="error"><?php echo $error; ?></div>

<div id="wrapper">
<div id="formDiv">
<form method="POST" action="index.php" enctype="multipart/form-data">

<label>First name:</label><br />
<input type="text" name="fname" /><br /><br />

<label>Last name:</label><br />
<input type="text" name="lname" /><br /><br />

<label>Email:</label><br />
<input type="text" name="email" /><br /><br />

<label>Password:</label><br />
<input type="password" name="password" /><br /><br />

<label>Re-enter Password:</label><br />
<input type="password" name="passwordConfirm" /><br /><br />

<label>Image:</label><br /><br />
<input type="file" name="image" /><br /><br />

<input type="submit" name="submit" />


Answer Source

This is an excerpt from your code:

$tmp_image = $_FILES['image']['tmp_name'];
$tmp_image = $_FILES['image']['size'];

Because you use the same variable name on the 2nd line, you're overwriting the image tmp_name, so your script loses access to the file.

You can just delete the 2nd line, since you don't use image size in the rest of the script. Or rename it $img_size if you plan to use it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download