views:

48

answers:

2

After looking around on Google for some time, I'm stuck =/ Could someone help me out please?

Seems to work with most files I try, except .mp3 files.

The (X)HTML

<html>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="71680000" />
    Choose a file to upload: <input name="uploaded_file" type="file" />
    <input type="submit" value="Upload" />
  </form>
</body>
</html>

The PHP

<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check file extension and size
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "mp3") && ($_FILES["uploaded_file"]["type"] == "audio/mpeg") && 
    ($_FILES["uploaded_file"]["size"] < 71680000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/up/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .mp3 files under are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

Edit: This is the output of var_dump($_FILES);

array(1) {
  ["uploaded_file"]=>
  array(5) {
    ["name"]=>
    string(17) "03-AsWeTravel.mp3"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(0) ""
    ["error"]=>
    int(1)
    ["size"]=>
    int(0)
  }
}
A: 

From http://www.php.net/manual/en/features.file-upload.errors.php

UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

How are you handling the max_file_size from the form?

Try changing

upload_max_filesize = 70M

Or whatever your max size should be in your php.ini.

methodin
yes I think it has to do something with the max size in php.ini, i copy pasted the code on my localhost and it works perfectly, the mp3 file is uploaded to the up folder.
krike
I think you're right. Just checked phpinfo() and realised the max file upload size is 2MB. Suppose I can change this in the .htaccess file?
Dean
@methodin: Just noticed your comment on the form. Thanks!
Dean
Yeah you can do it like: php_value upload_max_filesize 70M
methodin
Well doing it via the .htaccess file and form field doesn't seem to take effect on the client server (123-reg.co.uk hosted)
Dean
You will have to change it one of the following places: php.ini, .htaccess or httpd.conf. Your host might be blocking this value from being changed? You might wanted to see what they allow or if you have access to any of the aforementioned files.
methodin
A: 

Do you have control over the host? Could it be that mp3s are simply blocked?

I have seen it before on the free web hosting plan of a major ISP...

Damien
Yes, I have full control since I'm running on the script on my own machine.
Dean