Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Download File

  1. #1
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Download File

    Hello! The following code, uploads an MP3 file to a folder located in the root directory of the server, whilst adding the files path to a database.

    For each record in the database, how do I display the path as a link and upon a link being clicked, download the file?

    Code:
    if (($_FILES["file"]["type"] == "audio/mpeg"))
    {
      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
        {
          $file = ("upload/" . $_FILES["file"]["name"]);
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          
          mysql_query("INSERT INTO Music (Path)
          VALUES('$file')");    
        }
      }
    }
    else
    {
      echo "Invalid file";
    }

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    you need to create a recordset and output that into the link

    PHP Code:

    //dbconnection
    $hostname_conn "localhost";
    $database_conn "database";
    $username_conn "username";
    $password_conn "password";
    $conn mysql_pconnect($hostname_conn$username_conn$password_conn) or trigger_error(mysql_error(),E_USER_ERROR);

    mysql_select_db($database_conn$conn);

    $sql "SELECT * FROM Music";
    $rs_music mysql_query($sql$conn);


    do
    {

          print(
    "<a href='".$rows_rs_music['file']."'>".$rows_rs_music['file']."</a><br>")

    }
    while(
    $rows_rs_music mysql_fetch_assoc($rs_music)); 

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    That do/while is going to cause an error in the first interation because $rows_rs_music isn't set yet. Use a while loop:
    PHP Code:
    while($rows_rs_music mysql_fetch_assoc($rs_music))
    {
        print(
    "<a href='".$rows_rs_music['file']."'>".$rows_rs_music['file']."</a><br>");



  •  

    Posting Permissions

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