1

I'm using Linux Debian

trying to make Login & Registration System With php

on control page

index.php:

<?php
include("connect.php");
global $tf_handle;
$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'];
  $imageSize        = $_FILES['image']['size'];

  if($image == "")
  {
    $error = "Please Upload Image   ";
  }
  else
  {

    $insertQuery = "INSERT INTO users (firstName,lastName,email,password,image)
                    VALUES ('$firstName','$lastName','$email','$password','$image')";
    if(mysqli_query($tf_handle,$insertQuery))
    {

      if(move_uploaded_file($tmp_image,"images/$image"))
      {
        $error = "You're successfully registered";
      }
      else
      {
        $error = "Image isn't Uploaded";
      }
    }
  }
}
?>  


<!doctype html>
<html>
  <head>
    <title>Registration Page</title>
    <link rel="stylesheet" href="css/styles.css" />

  </head>
  <body>
      <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" required /><br/><br/>

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

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

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

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

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

              <input type="submit" name="submit" value="Registration" />
            </form>  

        </div>

      </div>

  </body>
</html>

While running the script

The Query Works Fine and it inserts information into Database

the problem it doesn't move the image to (images) Folder

move_uploaded_file($tmp_image,"images/$image");

do i use it in wrong way ??

Result:

Warning: move_uploaded_file(images/snapshot46.png): failed to open stream: Permission denied in /var/www/html/LoginRegistrationSystem/index.php on line 51

4
  • Have you checked if the folder has write permission ? Commented Sep 10, 2015 at 19:30
  • Did you check that images folder exists according to path from which script is called? Commented Sep 10, 2015 at 19:31
  • u_mulder more calcification please ...... Look /var/www/html/LoginRegistrationSystem/ contains 2 folder (css,images) and 2 files index.php & connect.php i.imgur.com/wngUc16.png Commented Sep 10, 2015 at 19:37
  • it needs permissions !!? Commented Sep 10, 2015 at 19:38

1 Answer 1

1

I wonder what are you getting when you print the $error:

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

From the php manual you get that:

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

So I would say:

1- Check the return based on your $error variable and you'll know if the file is a valid upload file.

2- Check the params you're using on move_uploaded_file are (string $filename , string $destination)

3- Check the permissions and path to your folder (if the problem lies in the permissions take a look at this post)


From the manual, the first "move_uploaded_file" example (check how $uploads_dir and $name are being used):

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

i think everything is ok the problem from the permissions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.