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 5 of 5
  1. #1
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why Doesn't This Work?

    Hi. The following code, is my attempt of uploading an MP3 file to the server, and then storing the path of the file inside a database. Why doesn't the line of code shown in bold work?

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

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,092
    Thanks
    2
    Thanked 322 Times in 314 Posts
    All you are doing is setting a variable named $sql. The code is not executing any database query. Reading this might help - http://php.net/mysql_query
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works fine when changing the following code from:

    this:

    Code:
    if (($_FILES["file"]["type"] == "audio/mpeg3"))
    to this:

    Code:
    if (($_FILES["file"]["type"] == "image/gif"))

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,092
    Thanks
    2
    Thanked 322 Times in 314 Posts
    Why doesn't the line of code shown in bold work?
    LOL. That is kind of funny in a sad sort of way.

    There is a difference between a line of code not working and the block of code it is present in not executing because a conditional statement is false and is skipping over it. I am guessing that you were getting the "Invalid file" output? If that was the case, you should have stated so in your post.

    Using a programming language requires that you have a basic understanding of what the code means and is doing. Changing the conditional test and having the code start "working" would indicate that the upload ['type'] element is different than what you expect and different than what the conditional statement was comparing it with. Echo out $_FILES["file"]["type"] to see what it actually contains.

    Edit: In your previous thread on the same subject http://www.codingforums.com/showthread.php?t=126043, you were also told how to determine and debug the ['type'] information -
    Also, when an invalid file type is detected, the code should display the type information it did receive in the ['type'] element so that you have some debugging information.
    Last edited by CFMaBiSmAd; 11-14-2007 at 03:45 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You're missing a concatenation operator(.) after $_FILES["file"]["name"] in your query too.


  •  

    Posting Permissions

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