我试图使用PHP将mp3格式文件上传到文件夹中。但是这个以下编码不起作用。请帮助我一个人吗?
define("UPLOAD_DIR", "mp3_upload_folder/");
if (!empty($_FILES["myFile"]))
{
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK)
{
echo "<p>An error occurred.</p>";
exit;
}
// verify the file is a GIF, JPEG, or PNG
$fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$parts = pathinfo($name);
$sanjiv=$_POST['user_id'];
$name= $sanjiv.".".$parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
答案 0 :(得分:0)
这样可行,并确保它具有写入mp3_upload_folder的权限。我认为它还缺乏一个完整的解决方案,但我对这个问题缺乏全面的了解。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
define("UPLOAD_DIR", "./mp3_upload_folder/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = $myFile["name"]; //preg_replace("/[^A-Z0-9\.\-]/i", "_", $myFile["name"]); //this is pointless if you're not using it
// don't overwrite an existing file
$parts = pathinfo($name);
$name= $_POST['user_id'].".".$parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR .$name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="text" value="testUserId" name ="user_id"/>
<input type="file" name="myFile"/>
<input type="submit"/>
</form>
</body>
</html>
还要确保编辑服务器设置以及用于上传文件大小的php设置(在php.ini中):
的post_max_size
Upload_max_size(可能是你的罪犯默认为2M)
我不确定您使用的服务器,但通过Google搜索服务器类型和“最大上传大小”可以轻松找到信息