SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    upload mp3 file and info into database how??

    I create a datebase named "mp3". Know I want to create a table called "contents" that will consist of

    ID Primary Key
    Song Name
    Artist
    Release Date
    Additional Notes
    File {contain mp3 file}

    I will collect all this info through a PHP form. How do I load all this info into the table called "contents" and can I load mp3 files into an mysql database??

    I have never dealt with MP3 file and MYSQL so I need some guidance

    this is the PHP form so far


    print<<<_HTML_
    <table>
    <form action="upload.php" method="post" enctype="multipart/form-data">

    <tr><td><b>MP3 Info/b></td></tr>
    <tr><td>Song Name/td><td><input type="text" name="name"></td></tr>
    <tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
    <tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
    <tr><td>Additional Notes</td></tr>
    <tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
    <tr><td>Select a File to Upload/td>
    <td><input type="file" name="mp3_file">
    <input type="Submit" value="Upload File"></td></tr>

    </form>
    </table>

  2. #2
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't recomend uploading mp3 files into a database since they are so large, but if you end up doing so, all you'd have to do is get the file contents and put it into a BLOB or LONGBLOB.

    Accessing it would be just like accesing an image from a database except you'd change the filetype header settings.
    -Melchior (Stephen Craton)

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question path on a directory file stored in mysql

    I decided to create a directory on my server to store the mp3 files instead and then to store the files path in the mysql

    so I would first create the table like this


    $sql = CREATE TABLE mp3_files (
    id int NOT NULL AUTO_INCREMENT Primary Key,
    Song Name VARCHAR,
    Artist Artist VARCHAR ,
    Release_Date date,
    Additional Notes VARCHAR,
    Path ?? );


    where "Path" would it me VARCHAR type?? and how would I store the absolute path of the file using PHP

    Lets say I had a folder called MP3_Files on my server that would contain all my mp3 files and I uploaded an mp3 song called big poppa? How would I store this location so that when I go to retrieve this file it know the exact one??

  4. #4
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is an example similar to this in Kevin Yank's book available from sitepoint. Basically the file is uploaded and stored in the mp3 directory. The url of this file created by the form processing script is then stored in the database to record where the file is.

    If you need an example email me and I'll send you a similar script where I upload images and insert the location into the database.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question lost with move_uploaded_file(???,"$folder" );

    I made some changed to make it easier I create the directory on my server called mp3_files I am using the htm form and php script to move the mp3 files to that location. I am lost in using the move_uploaded_file function to move the file to that directory?

    can someone take a look at my script and twll me what ??? should be
    move_uploaded_file(???,"$folder\???" );


    <?php

    if ($action == "load") {

    $folder = "mp3_files";
    move_uploaded_file(???,"$folder" );

    }


    ?>

    <html>
    <head>
    <title></title>
    </head>
    <body>
    <table>
    <form action="upload.php" method="post" enctype="multipart/form-data">

    <tr><td><b>MP3 Info/b></td></tr>
    <tr><td>Song Name/td><td><input type="text" name="name"></td></tr>
    <tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
    <tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
    <tr><td>Additional Notes</td></tr>
    <tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
    <tr><td>Select a File to Upload/td>
    <td><input type="file" name="mp3_file">
    <input type="Submit" name=action value="Load"></td></tr>
    </form>
    </table>
    </body>
    </html>

  6. #6
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you are looking for?

    move_uploaded_file($uploadedfile, $filename)

  7. #7
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should also confirm file type before processing the upload.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why is $uploadedfile the variable name I use??




    Quote Originally Posted by vscapes
    You should also confirm file type before processing the upload.

  9. #9
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cedtech23
    why is $uploadedfile the variable name I use??
    In your form the name of the file input: <input type = "file" name = "uploadedfile">

    Change the variable $uploaded file to the input name.

    Hope this helps

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to use the move_uploaded_file function to copy a file from my hard drive and move it into a directory on the server. The first agrument that I used with move_uploaded_file function is $uploadedfile because in my form I have the following <input type = "file" name = "uploadedfile"> the second argument is $folder because this variable contain the location of the directory that I want to save the file to

    $folder = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files";

    When I run a test of uploading a file and copying it to directory I get the following errors


    Warning: move_uploaded_file(C:\Program Files\Apache Group\Apache2\htdocs\mp3_files) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\TEMP\php4.tmp' to 'C:\Program Files\Apache Group\Apache2\htdocs\mp3_files' in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6


    what am I doing wrong??? How can I correct the problem??




    ?php

    if ($_POST['action']) {

    $folder = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files";
    move_uploaded_file($uploadedfile, $folder);

    }


    ?>

    <html>
    <head>
    <title></title>
    </head>
    <body>
    <table>
    <form action="upload.php" method="post" enctype="multipart/form-data">

    <tr><td><b>MP3 Info/b></td></tr>
    <tr><td>Song Name/td><td><input type="text" name="name"></td></tr>
    <tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
    <tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
    <tr><td>Additional Notes</td></tr>
    <tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
    <tr><td>Select a File to Upload/td>
    <td><input type = "file" name = "uploadedfile">
    <input type="Submit" name="submit" value="UpLoad">
    <input type="hidden" name="action" value=1>
    </td></tr>
    </form>
    </table>
    </body>
    </html>

  11. #11
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cedtech23
    Warning: move_uploaded_file(C:\Program Files\Apache Group\Apache2\htdocs\mp3_files) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\TEMP\php4.tmp' to 'C:\Program Files\Apache Group\Apache2\htdocs\mp3_files' in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6


    what am I doing wrong??? How can I correct the problem??
    You need to CHMOD the directory where the files are moved to to 777 so you have "write" permission for the file.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am doing my test on a windows 2003 sever and the folder's permission is set to allow everyone full control/read/write . I don't understand why I am still getting that permission error

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW blink.gif my web host is a pain in the $%@* I decided to load the file to my web host server and I start gettin this error

    Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2

    After an hour of research on google I found out this error is a result of safe mode being enabled on my hosting company web server:

    I got so frustrated that I tried a new approach :


    if ($_POST['action']) {

    $dir = "/public_html/mp3_files/";
    // create ftp connection
    $ftp_conn = ftp_connect('mywebsite.com');

    // logon to the ftp
    $ftp_log = ftp_login($ftp_conn, 'username', 'password');

    // error checking
    if (!ftp_conn || !ftp_log)
    echo 'There was an error connecting to the ftp';
    else
    ftp_put($ftp_conn, $dir . $_FILES['mp3_file']['name'], $_FILES['mp3_file']['tmp_name'], FTP_BINARY);
    echo $_FILES['mp3_file']['name'];
    ftp_close($ftp_conn);

    // $_FILES['userfile']['name'] would be the name you want to put in the db
    }


    it works but I am wondering is there anything I missed trying to use move_uploaded_file??

    Anyway know to conquer the major issue storing this info in a database

    How would you test to see if a certain database was present and if not creat it???

  14. #14
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cedtech23

    How would you test to see if a certain database was present and if not creat it???
    Glad to hear you found a way to get the files uploaded. What db are you using> MySQL or MSSQL?

  15. #15
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile database outline

    Quote Originally Posted by vscapes
    Glad to hear you found a way to get the files uploaded. What db are you using> MySQL or MSSQL?

    I am using MYSQL, it's easier to learn and it's FREE

    Speaking of database and I stuck in using if, else if statements to test the following

    Check if a "mp3db" database is present
    if not create the "mp3db" database
    if "mp3db" is present check to see if "mp3_files" tableis present
    if not create "mp3_files" table
    if table is present connect to table then goto to "input.php" file

    I basically need help create the layout/outline of statement for the above action.

    example:

    if (Check if a "mp3db" database is present){
    database is present}

    else
    create database

    etc.........

  16. #16
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, define $database, $host , $dbuser, $dpass

    PHP Code:
    [color=black]'try to connect to db[/color]

    [color=black]$mysql_link = @mysql_connect($host,$dbuser,$dbpass)[/color]
     
    if (! @mysql_select_db($database,$mysql_link)){
     
    '
    db does not exist so create it hereYou must have the permissions to create a new db
     
    }
    else
    ' db does exist so create tables here
     
    $query1 = mysql_query("CREATE TABLE table_name_here ( '
    table format )")
    $query2 = same_as_above_for_each_table_needed
     
         if (
    $query1){
         print "
    <p>xxxxx table created</p>";
         } else {
         
    $error = 1;
         echo("
    <p>Can't create xxxxx table ". mysql_error() ."<p>");
         }
     
         if ($query2){
         print "<p>xxxxx table created</p>";
         } else {
         $error = 1;
         echo("<p>Can'
    t create xxxxxx table ". mysql_error()."</p>");
         }
    }
     
    'If table already exist you will get an error for the query so no table needs to be created there... 

  17. #17
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ou have an error in your SQL syntax

    thanks for the outline I create this file listed below but I am getting this error when I run it:

    Can't create mp3_files table You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name VARCHAR, Artist VARCHAR , Release_Date date, Additional

    The syntax looks good to me what's wrong with it??


    <?php

    $host= "localhost";
    $dbuser= "unsernamer";
    $dbpass= "password";
    $database= "mp3_db";
    $table= "mp3_files";


    $link= @mysql_connect($host, $dbuser, $dbpass)
    or die ('I cannot connect to the database because: ' . mysql_error());



    if (! @mysql_select_db($database,$link)){

    mysql_query("CREATE DATABASE $database");

    }
    else
    {
    $sql="CREATE TABLE $table (
    id int NOT NULL AUTO_INCREMENT Primary Key,
    Song Name VARCHAR,
    Artist VARCHAR ,
    Release_Date date,
    Additional_Notes VARCHAR,
    Path VARCHAR )";

    $query1 = mysql_query($sql);


    if ($query1){
    print "<p>$table table created</p>";
    } else {
    $error = 1;
    echo("<p>Can't create $table table ". mysql_error() ."<p>");
    }


    }

    ?>

  18. #18
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    AZ
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The space in 'Song Name', add an underscore

  19. #19
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question unable to write to table

    I have two php files one check to see if a database and table is present if it's not it create both (check.php). The other collects info from a form and then inputs that data to the database and copy the mp3 file to a location on my computer(masterupload.php).

    So far the only to things I know thats working is that it will create a database or table if one is not present {check.php files does that} and that it will copy the file to a location on the server {masterupload.php does that} But the problem I am having is that it won't copy info from the form to the table here are the two php files in question:


    Why am I unable to write info from the form to the mysql table???


    this is the masterupload.php whick includes an call to the check.php file via include()

    <?php


    if ($_POST['action']) {

    /*$dir = "/home/nucity44/public_html/mp3_files/";*/
    $dir = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files\\";

    $uploadfile = $dir . $_FILES['mp3_file']['name'];

    move_uploaded_file($_FILES['mp3_file']['tmp_name'], $uploadfile);

    include 'check.php';

    $sql = "INSERT INTO $table (Song_Name, Artist, Release_Date, Additional_Notes, Path )
    VALUES ($_POST[name], $_POST[artist], $_POST[date], $_POST[notes],$uploadfile)";

    mysql_query($sql);
    }
    ?>


    check.php

    <?php

    $host= "localhost";
    $dbuser= "usernamer";
    $dbpass= "password";
    $database= "mp3_db";
    $table= "mp3_files";


    $link= @mysql_connect($host, $dbuser, $dbpass)
    or die ('I cannot connect to the database because: ' . mysql_error());


    if (! @mysql_select_db($database,$link)){

    mysql_query("CREATE DATABASE $database");

    }
    else
    {

    if(! @mysql_query("SELECT * FROM `$table` LIMIT 0,1")){


    $sql="CREATE TABLE $table (
    id INT NOT NULL auto_increment ,
    Song_Name VARCHAR(255) NOT NULL ,
    Artist VARCHAR(255) NOT NULL ,
    Release_Date DATE NOT NULL ,
    Additional_Notes VARCHAR(255) NOT NULL ,
    Path VARCHAR(255) NOT NULL ,
    PRIMARY KEY (id)
    )" ;

    mysql_query($sql);
    }
    }
    ?>

  20. #20
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cedtech23

    $sql = "INSERT INTO $table (Song_Name, Artist, Release_Date, Additional_Notes, Path )
    VALUES ($_POST[name], $_POST[artist], $_POST[date], $_POST[notes],$uploadfile)";


    mysql_query($sql) or die ('I cannot connect to the database because: ' . mysql_error());
    Put an error handler like I show above. That will cause the script to tell you what the problem is.
    Don't just advertise, AdverPrize!

  21. #21
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vscapes
    Put an error handler like I show above. That will cause the script to tell you what the problem is.

    Thanks I have to remember those error checking steps. I forgot to put quotes around the values.

    I am getting a strange output when I display the info in the table I am using the following php file below.

    I use the following code to to see what was being input into the table:

    $sql = "INSERT INTO $table (Song_Name, Artist, Release_Date, Additional_Notes, Path )
    VALUES ('$_POST[name]', '$_POST[artist]', '$_POST[date]', '$_POST[notes]','$uploadfile')";

    echo $sql;

    One example of what was inputed is:


    INSERT INTO mp3_files (Song_Name, Artist, Release_Date, Additional_Notes, Path ) VALUES ('Juicy', 'Biggy', '2000-18-01', 'this is a test','C:\Program Files\Apache Group\Apache2\htdocs\mp3_files\Biggie Smalls - Juicy.mp3')


    as you can see it input the Release_date as 2000-18-01 and the full path as

    C:\Program Files\Apache Group\Apache2\htdocs\mp3_files\Biggie Smalls - Juicy.mp3')

    but when I display the info in a table I get the following


    for the release_date 0000-00-00

    for the path C:Program FilesApache GroupApache2htdocsmp3_filesBiggie Smalls - Juicy.

    You can notice that it's not displaying the date and that it's not display the "\" backlash in the path

    Please, let me know what I am doing wrong in the display and how I can correct it??
    here is the code I cam using to dispaly the info in the table


    <?php
    include 'installdb.php';

    $query = "SELECT * FROM $table";
    $result = mysql_query($query);
    $numrows = mysql_num_rows($result);

    echo "<table>";
    echo "<tr><th>Song_Name</th><th>Artist</th><th>Release_Date</th><th>Additional_Notes</th><th>Path</th></tr>";

    while($row = mysql_fetch_array($result)){

    echo "<tr><td>$row[Song_Name]</td>";
    echo " <td>$row[Artist]</td>";
    echo " <td>$row[Release_Date]</td>";
    echo " <td>$row[Additional_Notes]</td>";
    echo " <td>$row[Path]</td></tr>";
    }
    echo "</table>";
    ?>

  22. #22
    SitePoint Enthusiast vscapes's Avatar
    Join Date
    Feb 2003
    Location
    Georgia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your string is being striped of slashes by php.
    Use this function:

    $uploadfile = htmlspecialchars($uploadfile);
    $date = htmlspecialchars($_POST[date]);
    Don't just advertise, AdverPrize!


Bookmarks

Posting Permissions

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