duanhuan5409 2017-12-06 16:51
浏览 43
已采纳

PHP文件上传问题

I'm currently developing a website where users can upload their music. You can set various genres to display. Now, the problem is that sometimes (no idea why) the upload fails for certain files. An error doesnt get displayed. It's definitely not a permission error, since the uploading works for most files. I've looked around a lot here already and tried many things, but nothing worked for me. Here's my code:

HTML

<form method="post" action="musicupload.php" enctype="multipart/form-data">
    Choose an MP3 or WAV file<file></file>
    <br /><br />
    <input type="file" name="fileToUpload" id="fileToUpload" required>
    <br /><br />
    <fieldset>
        Genre
        <br />
        <input type="radio" id="db" name="Genre" value="Dubstep" required checked>
        <label for="db"> Dubstep</label>
        <input type="radio" id="trap" name="Genre" value="Trap" required>
        <label for="trap"> Trap</label>
        <input type="radio" id="BB" name="Genre" value="Bass Boosted" required>
        <label for="bb"> Bass Boosted</label>
        <input type="radio" id="other" name="Genre" value="Other" required>
        <label for="other"> Other</label>
    </fieldset>
    <br />
    <input type="submit" value="Upload Song" name="submit">
</form>

PHP

<?php
/**
 * Created by IntelliJ IDEA.
 * User: Marc
 * Date: 04.12.2017
 * Time: 20:07
 */
require 'db.php';

$target_dir = "uploadedmusic/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$filename=$_FILES["fileToUpload"]['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$genre = $_POST["Genre"];
$successfull = false;

if(isset($_POST["submit"])) {
    // Check extensions
    if ($ext != "mp3" && $ext != "wav") {
        echo "Sorry, only MP3 & WAV files are allowed.";
        $uploadOk = 0;
    }

    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $hash = md5_file("uploadedmusic/".$filename);
            //echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
            $sql = "INSERT INTO `music` (`id`, `filename`, `genre`, `uploaded`, `hash`) VALUES (NULL, '$filename', '$genre', CURRENT_TIMESTAMP, '$hash')";
            $conn->query($sql);
            $successfull = true;
        } else {
            $successfull = false;
        }
    }
}

?>

<html>
<head>
    <title>Upload Music</title>
</head>
<body>
<div style="text-align: center;">
     <img src="logo.png">
<h1>Please wait...</h1>
<p style="font-size: 35px;">
    <?php
    if ($successfull == true) {
        echo "Successfully uploaded ". basename($_FILES["fileToUpload"]["name"])."! Use the search to find it!";
    }
    else {
        echo "Sorry, there was an error uploading your file. Check for potential invalid characters like () or - in your filename!";
    }


    ?>
</p>
    <a style="font-size: 35px" href="main.php">Start listening!</a>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dpzo13732 2017-12-06 18:08
    关注

    You need to see file upload errors to find the exact issue.

    echo $_FILES["fileToUpload"]["error"] // this will give you the error code.
    

    Possible errors are. check this link http://php.net/manual/en/features.file-upload.errors.php

    $phpFileUploadErrors = array(
        0 => 'There is no error, the file uploaded with success',
        1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
        2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
        3 => 'The uploaded file was only partially uploaded',
        4 => 'No file was uploaded',
        6 => 'Missing a temporary folder',
        7 => 'Failed to write file to disk.',
        8 => 'A PHP extension stopped the file upload.',
    );
    

    Also in your code apart from checking extension, you need add condition to check upload errors

    if ($uploadOk == 0 || $_FILES["fileToUpload"]["error"]!== UPLOAD_ERR_OK) {
       echo "Sorry, your file was not uploaded.";
       // if everything is ok, try to upload file
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用