Reputation: 37
I wanted to upload a audio file using html and php .It always returns the error message(Invalid file).please kindly help me. I am follow this url- https://forums.digitalpoint.com/threads/mp3-file-upload-in-php.1174500/;
http://p2p.wrox.com/php-how/53040-how-upload-mp3-file-using-php.html
This is the code I am using-
<form enctype="multipart/form-data" action="sound_action.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE"/>
Choose a file to upload: <input name="file" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<?php
if ((($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/mp4")
|| ($_FILES["file"]["type"] == "audio/wav"))
&& ($_FILES["file"]["size"] < 1000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Upvotes: 2
Views: 25448
Reputation: 21
You are missing an if clause :
if(isset($_POST['name_submit_button']))
{
//Whole Logic of program
}
Upvotes: 0
Reputation: 9782
There is an alternate way to validate a file, But it is a basic way to check the file validation not sure if file holds the correct type of not.
$valid_extension = array('.mp3', '.mp4', '.wav');
$file_extension = strtolower( strrchr( $_FILES["file"]["name"], "." ) );
if( in_array( $file_extension, $valid_extension ) &&
$_FILES["file"]["size"] < 1000000 ){
// Rest Logic Here
}
else
{
print_r( $_FILES );
}
Upvotes: 0
Reputation: 15629
The mime type of mp3 is audio/mpeg
for mp4 you have to use video/mp4
and wave is audio/x-wav
or audio/wav
.
Also you should increase the file size, because this argument is given in bytes and 1000000 is less than 1mb. Maybe you have also to increase the upload file size in php.ini
Upvotes: 2