bfsog
August 20 '05, 07:56 AM
Hey folks. When you come out of this tutorial you should be able to write your own mp3 uploader using PHP and X/HTML.
What you need to know
- basic html
- basic php syntax
- In php the default value for the MAX_FILE_SIZE is 2 mb. Your host may have changed this.
What you need to have
- php enabled host
Lets get coding.
First, lets design the form that will take the user's selected file.
<body>
<p style="font-family: verdana;">Please select a file to upload: </p>
<br />
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="40" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
<br /><br />
<span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>
</form>
</body>
Notice the <form> tag. If your a newbie to forms, the enctype="multipart/form-data" may well be new to you. It is needed when uploading files.
Moving on, we now know that our user bob wants to upload a file, but, it would be bad for us to just take his word for it and believe its a .mp3.
So lets check that out.
<?php
if($_POST['submit']=="Upload") { // if user clicked upload
if ($_FILES['file']['name'] != "") { // if the file input is not empty
if (($_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) { // if the file is a .mp3 file
if ($_FILES["file"]["size"] < 2097152) { // if the file is under 2 megabytes
?>
Wow. thats a lot of code.
The first if statement checks to see if the form has been submitted. We need this so that when the page is accessed, the script does not try and upload a file.
The second if statement is to make sure that the user entered a filename into the input field. We need this because we need to know what file bob wants to upload.
The third if statement checks to see if the file bob wants to upload is a .mp3 file
I had trouble with this as I was testing it out in Firefox and Firefox's MIME type for an mp3 file is "application/force-download" and in IE it is "audio/mpeg" so that is why there is a OR in the code.
The final if statement checks that the size of the given file is small enough to be uploaded.
Lets say bob chose a file which is called balloon.mp3 and with a size of 1.5 mb, we would want to upload it. Heres how
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
echo "File has been stored in your uploads directory.";
Very well. That is assuming all of our conditions are met.
Now we have to reverse the order and write code to deal with the chance of one (or indeed more than one) not being met.
}
else { echo "Please upload a file that is under 2 mb!";}
} else {
echo "Please upload a mp3 file!";
exit;}
} else { echo "Please enter a file.";}
}
?>
To save you copying and pasting like a maniac, here is the file in all it's glory
<html>
<head>
<title>Upload MP3</title>
<?php
if($_POST['submit']=="Upload") {
if ($_FILES['file']['name'] != "") {
if (($_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) {
if ($_FILES["file"]["size"] < 2097152) {
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
echo "File has been stored in your uploads directory.";}
else { echo "Please upload a file that is under 2 mb!";}
} else {
echo "Please upload a mp3 file!";
exit;}
} else { echo "Please enter a file.";}
}
?>
</head>
<body>
<p style="font-family: verdana;">Please select a file to upload: </p>
<br />
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="40" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
<br /><br />
<span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>
</form>
</body>
</html>
Hope you find it useful.
What you need to know
- basic html
- basic php syntax
- In php the default value for the MAX_FILE_SIZE is 2 mb. Your host may have changed this.
What you need to have
- php enabled host
Lets get coding.
First, lets design the form that will take the user's selected file.
<body>
<p style="font-family: verdana;">Please select a file to upload: </p>
<br />
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="40" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
<br /><br />
<span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>
</form>
</body>
Notice the <form> tag. If your a newbie to forms, the enctype="multipart/form-data" may well be new to you. It is needed when uploading files.
Moving on, we now know that our user bob wants to upload a file, but, it would be bad for us to just take his word for it and believe its a .mp3.
So lets check that out.
<?php
if($_POST['submit']=="Upload") { // if user clicked upload
if ($_FILES['file']['name'] != "") { // if the file input is not empty
if (($_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) { // if the file is a .mp3 file
if ($_FILES["file"]["size"] < 2097152) { // if the file is under 2 megabytes
?>
Wow. thats a lot of code.
The first if statement checks to see if the form has been submitted. We need this so that when the page is accessed, the script does not try and upload a file.
The second if statement is to make sure that the user entered a filename into the input field. We need this because we need to know what file bob wants to upload.
The third if statement checks to see if the file bob wants to upload is a .mp3 file
I had trouble with this as I was testing it out in Firefox and Firefox's MIME type for an mp3 file is "application/force-download" and in IE it is "audio/mpeg" so that is why there is a OR in the code.
The final if statement checks that the size of the given file is small enough to be uploaded.
Lets say bob chose a file which is called balloon.mp3 and with a size of 1.5 mb, we would want to upload it. Heres how
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
echo "File has been stored in your uploads directory.";
Very well. That is assuming all of our conditions are met.
Now we have to reverse the order and write code to deal with the chance of one (or indeed more than one) not being met.
}
else { echo "Please upload a file that is under 2 mb!";}
} else {
echo "Please upload a mp3 file!";
exit;}
} else { echo "Please enter a file.";}
}
?>
To save you copying and pasting like a maniac, here is the file in all it's glory
<html>
<head>
<title>Upload MP3</title>
<?php
if($_POST['submit']=="Upload") {
if ($_FILES['file']['name'] != "") {
if (($_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) {
if ($_FILES["file"]["size"] < 2097152) {
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
echo "File has been stored in your uploads directory.";}
else { echo "Please upload a file that is under 2 mb!";}
} else {
echo "Please upload a mp3 file!";
exit;}
} else { echo "Please enter a file.";}
}
?>
</head>
<body>
<p style="font-family: verdana;">Please select a file to upload: </p>
<br />
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="40" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
<br /><br />
<span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>
</form>
</body>
</html>
Hope you find it useful.