简体   繁体   中英

PHP - using move_uploaded_file

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

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");
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM