PHP - 文件上传不会将文件移动到目录

时间:2013-11-03 18:54:50

标签: php file-upload

文件上传脚本如下...在DB中存储文件名工作正常,但文件没有移动到正确的目录。 “Avatars”文件位于“/ httpdocs /”目录中。我已将权限设置为“777”。

<?php
require('dbconfig.php');

//generate a random string
function generateRandomString($length = 40) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

$customname = generateRandomString();

$newimagename = "$customname." . pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    header ('Location: /dashboard.php?filetype=invalid');
    exit();
    }
  else
    {
    //echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    //echo "Type: " . $_FILES["file"]["type"] . "<br>";
    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      //echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "avatars/" . $newimagename);
      //echo "Stored in: " . "profile_videos/" . $newimagename;

        $storedtoken = $_COOKIE['login_token'];
        $mysqlicon = mysqli_connect($db_host, $db_username, $db_password, $db_name);
        //identify the user by comparing tokens
        $find_user_id = mysqli_query($mysqlicon, "SELECT * FROM logins WHERE token='$storedtoken'");

        //grab the user's UUID
        while ($row = mysqli_fetch_array($find_user_id)) {
            $uuid = $row['userID'];
            mysqli_query($mysqlicon, "UPDATE families SET avatarURL='avatars/$newimagename' where husbandID='$uuid' OR wifeID='$uuid'");
            mysqli_close($mysqlicon);
        }

      header ('Location: /dashboard.php');

      }
    }
  }
else
  {
  header ('Location: /dashboard.php?profile-video-upload=invalid');
  mysqli_close($mysqlicon);
  exit();
  }
?>

0 个答案:

没有答案