上传

时间:2018-04-05 20:00:26

标签: php file-upload file-extension

我正在尝试在上传时验证文件,我使用以下代码来实现它,但我没有得到所需的输出。 即使我上传了允许的扩展类型,我仍然收到回复消息"您无法上传此类型的文件!"。这意味着If条件if (in_array($fileActualExt, $allowed, true))没有成真, 在此先感谢帮助我,

<?php
if(isset($_POST['submit'])) {
    if (!empty ($_FILES['file'])){
        $fileName = $_FILES['file']['name'];
        $fileTempName = $_FILES['file']['tmp_name'];
        $fileSize  = $_FILES['file']['size'];
        $fileError = $_FILES['file']['error'];
        $fileType  = $_FILES['file']['type'];

        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));
        $allowed = array('jpg', 'jpeg', 'png');

        if (in_array($fileActualExt, $allowed, true)) {
            if ($fileError === 0) {
                if ($fileSize <8000) {
                    $fileNameNew = uniqid('', true).".".$fileActualExt;
                    $fileDestination = 'uploads/'.$fileNameNew;
                    move_uploaded_file($fileTempName, $fileDestination);
                    header("location: player_home.php?uploadSucess");
                    echo "success";
                }else{
                    echo "your file is too big";
                }
            }else{
                echo "There was an Error uploading your file!";
            }
        }else{
            echo "You cannot upload files of this type!";
        }
    }
}
?>

HTML CODE

<form method="post">
   <div class="form-group">
        <input type="file" name="file">
    </div>
    <span class="sml">Maximum size of <span>8.0MB</span> for a file.‏
    <br>
    Allowed Types : jpg, jpeg, png
    </span>
    <br>
    <button class="btn btn-primary" type="submit" name="submit" style="margin-top:15px;">Save</button>
 </form>

1 个答案:

答案 0 :(得分:0)

您可能会使用true方法中的in_array()选项绊倒。删除它,条件应该工作。