使用php上传文件时出错。没有显示php错误

时间:2017-10-25 16:31:16

标签: php html forms file-upload upload

PHP代码:

if(isset($_POST["btn-vd-submit"]) AND $vd_perm_actual > 0) {
$filename = $_FILES['vdfile']['name'];
$target_dir = "./voice-demo-files/";
$target_file = $target_dir . basename($_FILES['vdfile']['name']);
$uploadOk = 1;
$vdFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES['vdfile']['size'] > 50000000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($vdFileType != "mp3") {
    echo "Sorry, only mp3 files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES['vdfile']['tmp_name'], $target_file)) {
        echo "The file ". basename( $_FILES['vdfile']['name']). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

HTML表单:

<form class="custom-form" method="post" enctype="multipart/form-data">
    <div class="s-4 m-4 center">
        <center>Choose A Voice Demo File (.mp3) to upload and link with your profile:<br><br><br>
        <input type="file" name="vdfile" id="vdfile"><br><br><br>
    </div>
    <div class="s-4 m-4 center">
        <button class="submit-form center button background-primary text-white" name="btn-vd-submit" type="submit">Upload This Voice Demo!</button>
    </div>

当我选择并上传文件时,收到我设置的自定义错误消息,&#34;抱歉,上传文件时出错。&#34 ;; 我没有在页面或日志中显示任何类型的php错误,但是当我检查目录时文件没有上传。

1 个答案:

答案 0 :(得分:0)

尝试以下测试:

  1. 上传1kb文件。如果那可行,则意味着您有最大文件限制错误。 (阅读here如何解决)
  2. 如果您在1kb上传尝试期间尝试将文件上传到其他目录(可能是当前目录) - 成功意味着您可以将文件写入到目录中的权限。阅读有关修复持久性问题的here
  3. 除此之外,尝试在代码中添加以下行,它将帮助我们完成调试过程:

    error_reporting(E_ALL);