www.webdeveloper.com
Results 1 to 4 of 4

Thread: Store Upload File Path (Database)

  1. #1
    Join Date
    Nov 2007
    Posts
    362

    Store Upload File Path (Database)

    Hi, I have come up with the following code, that uploads gif images to the server. How do I allow only MP3 files to be uploaded, and also store the path of the image in a database?

    Code:
    <?php
    if (($_FILES["file"]["type"] == "image/gif")
    && ($_FILES["file"]["size"] < 20000))
    {
      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";
    }
    ?>

  2. #2
    Join Date
    Aug 2007
    Posts
    86
    try this code

    PHP Code:


        $file
    =$_FILES["file"]["name"];
        
    $get_ext=explode(".",$file);
        
    $file_ext=strtolower($get_ext[sizeof($get_ext)-1]);


        if ((
    $get_ext == "mp3") && ($_FILES["file"]["size"] < 20000))
        {
          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
            {
              
    $destination="upload/" $_FILES["file"]["name"];
              
    move_uploaded_file($_FILES["file"]["tmp_name"],$destination);
              echo 
    "Stored in: " "upload/" $_FILES["file"]["name"];
            }
          }
        }
        else
        {
          echo 
    "Invalid file";
        } 
    Now Simply you can store the $destination in your database with simple query

  3. #3
    Join Date
    Nov 2007
    Posts
    362
    Do I really need a file size limit on mp3's, as it depends on how long the track is to how big the file is. If the track is long, then the file may not be able to be upload - Correct? How do I get around this, by not checking for file size?

  4. #4
    Join Date
    Aug 2007
    Posts
    86
    this line gives you the size of MP3
    $fileSize=$_FILES["file"]["size"] < 20000;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles