繁体   English   中英

PHP-使用move_uploaded_file

[英]PHP - using move_uploaded_file

我正在使用Linux Debian

试图用php登录和注册系统

在控制页面上

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>

运行脚本时

查询工作正常,并将信息插入数据库

问题是它不会将图像移动到(images)文件夹

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

我会以错误的方式使用它吗?

结果:

警告:move_uploaded_file(images / snapshot46.png):无法打开流:在第51行的/var/www/html/LoginRegistrationSystem/index.php中,权限被拒绝

我想知道打印$ error会得到什么:

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

php手册中可以得到:

如果filename不是有效的上传文件,则不会执行任何操作,并且move_uploaded_file()将返回FALSE。

如果filename是有效的上载文件,但由于某种原因无法移动,则不会发生任何操作,并且move_uploaded_file()将返回FALSE。 此外,将发出警告。

所以我会说:

1-根据您的$ error变量检查返回值,您将知道该文件是否为有效的上传文件。

2-检查在move_uploaded_file上使用的参数是(string $filename , string $destination)

3-检查权限和文件夹的路径(如果问题出在权限上,请查看此帖子


在手册中,第一个“ move_uploaded_file”示例(检查$uploads_dir$name的使用方式):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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