PHP上传图片什么都不做,没有给出错误

时间:2017-03-09 22:21:31

标签: php html mysql sql file-upload

我正在建立一个人们可以上传个人资料图片的网站。该页面重定向,好像没有错误,并且SQL更新正常,但图像不会出现在其目录中。 php.ini表示它有权上传。此外,当我在localhost上尝试同样的事情时,它完美地工作了

HTML:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP:

<?php
session_start();
include_once 'dbh.php';
$id = $_SESSION['userID'];

if (isset($_POST['submit'])) {
$file = $_FILES['file'];

//about the file
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];

$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

  //checks if it's in the allowed type
      if (in_array($fileActualExt, $allowed)) {
          //checks for errors
          if ($fileError === 0) {
                //checks for appropriate file size
              if ($fileSize < 1000000) {
                $fileNameNew = "profile".$id.".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                $sql = "UPDATE users SET profilePicture='$fileNameNew' WHERE userID='$id';";
                $result = mysqli_query($conn, $sql);
                header("Location: index.php?uploadsuccess");
              } else {
                  echo "Your file is too big!";
              }
          } else {
              echo "There was an error uploading your file!";
          }
      } else {
          echo "You must choose a valid file type!";
      }

}

1 个答案:

答案 0 :(得分:0)

检查上传目录的路径。

$path = getcwd();
echo $path;

$fileDestination = 'uploads/'.$fileNameNew;

可能最终看起来像

$fileDestination = '/home/user/public_html/uploads/'.$fileNameNew;