Page 1 of 1
can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 1:37 am
by attaboy
I found this example which uploads image files just fine:
Code: Select all
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["submit"])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
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";
}
}
?>
</body>
</html>
All I want to do is change it to upload mp3's .... warning this may fool you
audio/mp3 doesn't work also is there a better way to upload?
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 12:30 pm
by Christopher
Have you uploaded an MP3 and checked to see what the value in $_FILES["file"]["type"] is?
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 3:45 pm
by attaboy
If I could upload an mp3 then I wouldn't have a problem. Have you tested this yourself? If you create a directory called upload and run this code in directory that contains the new directory you will be be presented with a button for browsing for your file to upload if you select a image file you can upload just fine. I've added lines to echo the type in both if ($_FILES["file"]["error"] > 0) section and before where it echo's "invalid file" but nothing shows up.
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 3:56 pm
by requinix
As long as you add the mp3 extension to the list, add the audio/mp3 or whatever type to that list, and choose a file less than 20KB then it should work.
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 7:47 pm
by Christopher
Yes, my guess is that you are uploading files larger that 20kb. You should probably change that hard coded 20000 to a variable like $maxFileSize that you can set per application.
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 8:27 pm
by attaboy
I've tried all these things you guys suggest I've commented out the size limit, I've added mp3 to the extention list and used audio/mp3 as well as audio/mpeg so far nothing works I do appreciate that I'm getting responses though. If you're into PHP I'm sure you have an apache server set up and you can test at home if you do you'll see that the script works great with images. I've got to believe there's a way to make it work with mp3's but as I've mentioned it's not as easy as you think.
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 8:41 pm
by requinix
What does your code look like with all the changes you've just mentioned?
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 10:02 pm
by attaboy
OK. I've tried several permutations here's one:
Code: Select all
<?php
if (isset($_POST["submit"])) {
$allowedExts = array("mp3", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "image/mpeg")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
//&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Type: " . $_FILES["file"]["type"] . "<br>";
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 "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Invalid file";
}
}
?>
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 10:53 pm
by attaboy
I spent some time with this simplifed example:
Code: Select all
<?php print_r($_FILES);?>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="image" />
</div>
<input type="submit" value="Send" />
</form>
what I found was that you can post wmv and wav but you get error 1 every time you try to POST mp3 which leads me to the conclusion that you can't post mp3's .
Re: can anyone convert this to upload mp3's
Posted: Thu Apr 11, 2013 11:38 pm
by Christopher
Your problems seemed to be a number of either typos or incorrect values. The correct MIME type is 'audio/mpeg' not 'image/mpeg'. And the field name in the form should me 'file' not 'image'. There were a number of other little problems.
This version works for me:
upload.php
Code: Select all
<?php
if (isset($_POST["upload"])) {
$path = '/path/to/upload/dir/';
$maxFileSize = 1 * (1024 * 1024); // 1Mb
$allowedExts = array("mp3", "jpeg", "jpg", "png");
$allowedMimes = array("audio/mp3", "audio/mpeg", "image/jpeg", "image/jpg", "image/png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Type: " . $_FILES["file"]["type"] . "<br>";
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($path . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
echo "Stored in: $path" . $_FILES["file"]["name"];
}
}
} else {
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Invalid file<br/>";
echo '<pre>' . print_r($_FILES) . '</pre>';
}
} else {
echo "no POST['submit']<br/>";
}
uploadform.php
Code: Select all
<?php print_r($_FILES);?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="file" />
</div>
<input type="submit" name="upload" value="Upload" />
</form>
I would recommend separating your checks into separate if()s so you can set individual errors for size, MIME, extension, overwrite, etc.
Re: can anyone convert this to upload mp3's
Posted: Mon Apr 15, 2013 11:15 pm
by attaboy
Yeah it works. It doesn't work on my localhost but when I uploaded to my hosting account on godaddy it works fine.
Thanks for your support.
Re: can anyone convert this to upload mp3's
Posted: Tue Apr 16, 2013 11:23 am
by Christopher
For this kind of code I would recommend only checking one condition per if() and setting an error value if the check fails. You will have more nested if()s but you can identify and report to the user exactly what the problem with the upload was.