【发布时间】:2015-02-23 17:15:22
【问题描述】:
我在网页上有一个表单,允许用户上传 mp3 文件。它工作得很好,除了超过 2MB 的文件。然后它说“对不起,上传您的文件时出错。”我已经测试了 2mb - 6mb 的 mp3,但它们不起作用,我相信我已经将代码调整到可以接受超过 2MB 的文件的位置。有人可以帮忙吗?
这是 HTML,(我认为这不是问题):
<div id="contactpara">
<form id="upform" action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Music" name="submit">
<iframe id="upload_target" name="upload_target" src="" style="width:500px;height:100px;border:0px solid #000;"></iframe>
</form>
</div>
这是 PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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["fileToUpload"]["size"] > 10000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
它说 $imageFileType 的原因是因为这是一个 PHP 代码,用于接受我调整以接受音频文件的图像。这会导致问题吗?请告诉我。
【问题讨论】:
-
鉴于您可能会收到特定错误消息的唯一地方是如果
move_uploaded_file()调用失败,您应该查看它失败的原因。 -
也许您的服务器配置只允许最大 2mb 的文件。使用
ini_get('upload_max_filesize');在 PHP 中检查。 -
@MichaelDoye 谢谢你!我只需将其添加到我的 php.ini 中: post_max_size = 50M upload_max_filesize = 50M 就解决了问题
标签: php audio upload submit mp3