首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php上传文件时出错。没有显示php错误

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

Stack Overflow用户
提问于 2017-10-25 16:31:16
回答 1查看 382关注 0票数 0

PHP代码:

代码语言:javascript
复制
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表单:

代码语言:javascript
复制
<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>

当我选择并上传一个文件时,我会得到我设置的自定义错误消息,“对不起,上传您的文件时出错了。”;我不会在页面或日志中显示任何php错误,但是当我检查目录时,文件不会被上传。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-25 16:37:41

尝试以下测试:

  1. 上传一个1kb的文件。如果这是工作,这意味着您有最大的文件限制错误。(阅读here如何修复它)
  2. 如果在1kb的上传过程中,尝试将文件上传到其他目录(可能是当前目录)--成功意味着您可以将文件写入到适当权限的目录中。阅读here关于解决问题的说明

此外,尝试将以下行添加到代码中,这将有助于调试过程:

代码语言:javascript
复制
error_reporting(E_ALL);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46937659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档