Page 1 of 1

User Profile Music Upload Help

Posted: Sun Jun 22, 2008 1:56 pm
by derek barnstorm
Hi,

I am converting a script that will let users register with my website and have a user profile page. I want them to be able to upload a media file and have it play back from their page with something like JW Player.
I've converted a piece of script and I've got the upload working, so when a user goes to their profile page they can upload a media file and it is stored in my database with their profile ID. But I am having real problems downloading/retrieving the path to the media file so that I can display it in a flash player.

I'm assuming it's because to download media you should use '$id = $_GET['id'];' but I need it to be the users ID.

Here are the scripts that I have been working on so far... Any help or a point in the right direction would be much appreciated...

I converted the below 'Bookmarks' script (which allows users to upload a simple hyperlink to their profile) into a media upload script:

Original Bookmark Script:

Code: Select all

<?php
if($_SERVER[PHP_SELF]=="/include/bm_add.inc.php")
{
   header("Location: /index.php");
   exit;
}
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
$sql = "SELECT profile_url FROM $tbl_profiles WHERE profile_id = \"$auth[member_id]\"";
$result = @mysql_query($sql,$connection) or die("Couldn't execute profile URL query.");
 
while ($row = mysql_fetch_array($result)) {
$my_url = $row['profile_url'];
$my_fullurl = "$userurl/$my_url";
}
 
if($_POST['action'] == "create") {
 
$bm_name = $_POST['bm_name'];
$bm_url = $_POST['bm_url'];
 
if(empty($bm_name)) {
echo "<p>You didn't enter a title!</p>
<p align=\"center\"><input id=\"button\" type=button value=\"BACK\" onClick=\"history.go(-1)\"></p>";
include("include/footer.inc.php");
exit;
}
if(empty($bm_url)) {
echo "<p>You didn't enter a URL!</p>
<p align=\"center\"><input id=\"button\" type=button value=\"Try again\" onClick=\"history.go(-1)\"></p>";
include("include/footer.inc.php");
exit;
}
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
$sql = "INSERT INTO $tbl_bookmarks (bm_owner, bm_name, bm_url)
VALUES (\"$auth[member_id]\", \"$bm_name\", \"$bm_url\")";
$result = @mysql_query($sql,$connection) or die("Couldn't execute new bookmark query.");
 
echo "<p>Your link has been added!</p>
<p align=\"center\"><a class=\"small\" href=\"$PHP_SELF\">ADD ANOTHER?</a></p>
<p align=\"center\"><a class=\"small\" href=\"$my_fullurl\">BACK TO PROFILE</a></p>";
include("include/footer.inc.php");
exit;
 
} else {
?>
<form action="<? $PHP_SELF ?>" method="POST">
<table border="0" align="center" cellpadding="3" cellspacing="0">
    <tr>
        <td valign="top">
            <p>TITLE:</p>
        </td>
        <td valign="top">
            <p><input class="form" type="text" name="bm_name" maxlength="30" size="30"></p>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <p>URL:</p>
        </td>
        <td valign="top">
            <p><input class="form" type="text" name="bm_url" value="http://" maxlength="150" size="30"></p>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <p><input type="hidden" name="action" value="create"></p>
        </td>
        <td valign="top">
            <p><input id="button" type="submit" value="SUBMIT">
        <input id="button" type=button value="CANCEL" onClick="history.go(-1)"></p>
        </td>
    </tr>
</table>
</form>
<p>&nbsp;</p>
<? } ?>
Above script converted to Media Upload:

Code: Select all

<?php
if($_SERVER[PHP_SELF]=="/include/music_add.inc.php")
{
   header("Location: /index.php");
   exit;
}
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
$sql = "SELECT profile_url FROM $tbl_profiles WHERE profile_id = \"$auth[member_id]\"";
$result = @mysql_query($sql,$connection) or die("Couldn't execute profile URL query.");
 
while ($row = mysql_fetch_array($result)) {
$my_url = $row['profile_url'];
$my_fullurl = "$userurl/$my_url";
}
 
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
 
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
 
 
 
 
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
$sql = "INSERT INTO upload (music_owner, name, size, type, content ) ".
"VALUES ('$auth[member_id]', '$fileName', '$fileSize', '$fileType', '$content')";
 
 
$result = @mysql_query($sql,$connection) or die("Couldn't execute new music query.");
 
echo "<br />File $fileName uploaded<br />
 
<p align=\"center\"><a class=\"small\" href=\"$PHP_SELF\">ADD ANOTHER?</a></p>
<p align=\"center\"><a class=\"small\" href=\"$my_fullurl\">BACK TO PROFILE</a></p>";
include("include/footer.inc.php");
exit;
 
} else {
?>
<form action="<? $PHP_SELF ?>" method="POST" enctype="multipart/form-data">
<table border="0" align="center" cellpadding="3" cellspacing="0">
    <tr>
 
    </tr>
    <tr>
        <td valign="top">
            <p>IMAGE/MP3:</p>
        </td>
        <td valign="top">
            <p><input class="form" type="file" name="userfile" id="userfile" size="30"></p>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <p>MUSIC:</p>
        </td>
        <td valign="top">
            <p><input class="form" type="file" name="music_song1" size="30"></p>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <p><input type="hidden" name="action" value="create"></p>
        </td>
        <td valign="top">
            <p><input id="upload" name="upload" type="submit" value="SUBMIT">
        <input id="button" type=button value="CANCEL" onClick="history.go(-1)"></p>
        </td>
    </tr>
</table>
</form>
<p>&nbsp;</p>
<? } ?>
The Display Bookmarks Script (which I would like to convert to media upload):

Code: Select all

<?php
 if($_SERVER[PHP_SELF]=="/include/profiles/bm_show.inc.php")
 {
    header("Location: /index.php");
    exit;
}
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
$sql = "SELECT * FROM $tbl_bookmarks WHERE bm_owner = \"$profile_id\" ORDER BY bm_id DESC";
$result = @mysql_query($sql,$connection) or die("Couldn't execute bookmark query.");
$num=mysql_num_rows($result);
 
if($num < 1){
echo "<center>No Links yet.</center>";
 
}
?>
<ul>
<?
while ($row = mysql_fetch_array($result)) {
 
$bm_id = $row['bm_id'];
$bm_name = $row['bm_name'];
$bm_url = $row['bm_url'];
 
if($profile_id == $auth[member_id]) {
$usredit = "<a href=\"$siteurl/bm_edit.php?bm_id=$bm_id\">
<img src=\"$imgurl/edit.gif\" align=\"absmiddle\" alt=\"Edit\" title=\"Edit\" border=\"0\"></a> ";
}
 
echo "<li>$usredit<a href=\"$bm_url\" target=\"_blank\"> $bm_name</a></li>";
}
?>
</ul>
And the below script is what I've been trying to trying to mix in with the above script:

Code: Select all

<?
if(isset($_GET['id']))
{
    include 'library/config.php';
    include 'library/opendb.php';
 
    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);
 
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;
 
    include 'library/closedb.php';  
    exit;
}
 
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?
include 'library/config.php';
include 'library/opendb.php';
 
$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
} 
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?      
    }
}
include 'library/closedb.php';
?>
</body>
</html>
I'm still a bit of a noobie, that's why I've been taking bits of script from here and there...

Cheers!

Re: User Profile Music Upload Help

Posted: Sun Jun 22, 2008 7:55 pm
by derek barnstorm
I apologize in advance if I am not supplying enough information or if it makes no sense...

Anyway, I've actually got this to sort of work, but I think I must have a path problem.
All files are uploaded to the database and it is collecting the correct ID for the user but when clicking on the link to the file it just displays the same page that the link is on.

For example, the link to the file is http://mysite.com/userpage/index.php?id=00000002 but the link just displays http://mysite.com/userpage/index.php

This is an include script being brought into the index page... Just wondered if anybody knows why or how there's a better way to find the path.

The script:

Code: Select all

<?php
 if($_SERVER[PHP_SELF]=="/include/profiles/music_show.inc.php")
 {
    header("Location: /index.php");
    exit;
}
 
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
 
 
    
?>
<?
 
$sql  = "SELECT id, name FROM upload WHERE music_owner = \"$profile_id\"";
$result = @mysql_query($sql,$connection) or die("Couldn't execute bookmark query.");
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
} 
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <a href="<?php echo $_SERVER['PHP_SELF']?>?id=<?=$id;?>"><?=$name;?> <br>
<?      
    }
}
 
 
?>
.