PHP上传图片错误扩展错误

时间:2018-03-24 22:25:32

标签: php image file upload

我正在使用此代码尝试将图像上传到服务器。我正在选择JPG图像。出于某种原因,我收到错误显示扩展名无效或错误。我做错了什么?

以下是代码:

<?php
    $currentDir = getcwd();
    $uploadDirectory = "/tutorials/";

    $errors = []; // Store all foreseen and unforseen errors here

    $fileExtensions = ['JPG','jpeg','jpg','png']; // Get all the file 
    extensions

    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));

    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 

    if (isset($_POST['submit'])) {

        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = "This file extension is not allowed. Please upload a 
      JPEG or PNG file";
        }

        if ($fileSize > 5000000) {
            $errors[] = "This file is more than 2MB. Sorry, it has to be 
         less than or equal to 2MB";
        }

        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

            if ($didUpload) {
                echo "The file " . basename($fileName) . " has been 
          uploaded";
            } else {
                echo "An error occurred somewhere. Try again or contact the 
        admin";
            }
        } else {
            foreach ($errors as $error) {
                echo $error . "These are the errors" . "\n";
            }
        }
    }

?>

0 个答案:

没有答案