can anyone convert this to upload mp3's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

can anyone convert this to upload mp3's

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: can anyone convert this to upload mp3's

Post by Christopher »

Have you uploaded an MP3 and checked to see what the value in $_FILES["file"]["type"] is?
(#10850)
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: can anyone convert this to upload mp3's

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: can anyone convert this to upload mp3's

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: can anyone convert this to upload mp3's

Post 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.
(#10850)
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: can anyone convert this to upload mp3's

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: can anyone convert this to upload mp3's

Post by requinix »

What does your code look like with all the changes you've just mentioned?
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: can anyone convert this to upload mp3's

Post 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";
  }
}
?>
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: can anyone convert this to upload mp3's

Post 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 .
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: can anyone convert this to upload mp3's

Post 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.
(#10850)
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: can anyone convert this to upload mp3's

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: can anyone convert this to upload mp3's

Post 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.
(#10850)
Post Reply