Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 8th, 2006, 11:49 AM
Friend of Wrox
 
Join Date: Mar 2005
Location: , , .
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to write files information to mysql db using p

Hi all .I wonder how i can write the file path and file name that i read from mp3 folder and write them to mysql database table. Currently print_r($playlist); outputs the file names with .mp3 extention as shown below .Furthermore,print_r(htmlentities($code)) output path and file title as shown below. I be happy if an expert show me how i can write the file name and file path and write them to mysql db with file number.Thanks

Code:
<?php

// setting the directory to search for mp3 files
$dir = "mp3/";

// reading the directory and inserting the mp3 files in the playlist array
$n = 0;
$playlist = array();
$fdir = opendir($dir);
while($i = readdir($fdir)) {
   // if a .mp3 string is found, add the file to the array
   if (strpos(strtolower($i),".mp3") !== false) {
         $playlist[$n] = $i;
         $n++;
      } 
}
// close the directory and sort the array
closedir($fdir);
array_multisort($playlist);

print_r($playlist);

// echoing the playlist to flash
 $code = "<player showDisplay=\"yes\" showPlaylist=\"no\" autoStart=\"yes\">\n";
 for ($i=0; $i<sizeof($playlist); $i++) {
   // for the title it filters the directory and the .mp3 extension out
    $code .= "  <song path=\"$dir.$playlist[$i]\" title=\"".str_replace(".mp3","",$playlist[$i])."\"  />\n";
 }
 $code .= "</player>";

print_r(htmlentities($code));
?>
print_r($playlist); output
Code:
Array ( 

[0] => songname1.mp3 
[1] => songname2.mp3 
[2] => songname3.mp3 
[3] => songname4.mp3 
[4] => songname5.mp3 
[5] => songname6.mp3 
[6] => songname7.mp3 
[7] => songname8.mp3 
[8] => songname9.mp3 
[9] => songname10.mp3 
[10] => songname11.mp3 
[11] => songname12.mp3 
)
print_r(htmlentities($code)); output
Code:
<player showDisplay="yes" showPlaylist="no" autoStart="yes"> 
<song path="mp3/songname1.mp3" title="songname1" /> 
<song path="mp3/songname2.mp3" title="songname2" />
.
.
.
 </player>
  #2 (permalink)  
Old February 12th, 2006, 07:10 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My SQL input output data is pretty straight foward.. your code looks pretty nice so whats is the issue ??

let me re explain this:

You have done all the hard work in your code. I can't see where the difficulty is for you ???

Thats pretty self evident for example:
<player showDisplay="yes" showPlaylist="no" autoStart="yes">
<song path="mp3/<?php echo $songname1; ?>" title="<?php echo $songname1; ?>" />
<song path="mp3/<?php echo $songname2; ?>" title="<?php echo $songname2; ?>" />
.
.
.
 </player>

???
  #3 (permalink)  
Old February 12th, 2006, 07:46 PM
Friend of Wrox
 
Join Date: Mar 2005
Location: , , .
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Mantis
 My SQL input output data is pretty straight foward.. your code looks pretty nice so whats is the issue ??

let me re explain this:

You have done all the hard work in your code. I can't see where the difficulty is for you ???

Thats pretty self evident for example:
<player showDisplay="yes" showPlaylist="no" autoStart="yes">
<song path="mp3/<?php echo $songname1; ?>" title="<?php echo $songname1; ?>" />
<song path="mp3/<?php echo $songname2; ?>" title="<?php echo $songname2; ?>" />
.
.
.
 </player>

???
There a few problem that i do not how do it. I want to write mp3/songname1.mp3(song path) and songname(title to)my sql database for each file in the mp3 directory.Furthermore my code does not read files inside mp3 subdirectory (mp3/subfolder).I be happy if u help me fix these problems.Thanks
  #4 (permalink)  
Old February 14th, 2006, 01:22 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know why you want to store the filename in a database to be honnest.. not my problem you would say. lol

I found this nice Mp3 flash player this morning:
http://www.jeroenwijering.com/

and I put a bit of code together which happen to be close to what you need (lucky you!)
<?php
$NBFile=0;
$NBDir=0;
$mp3folder="mp3";

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "..") {

if ( strlen($file) >= 3 ) {

if ( substr($file, strlen($file)-4,1) != "." ) {
        $DirArray[] = $file;
        $NBDir=$NBDir+1;
} }

        }
    }
}
closedir($handle);
sort($DirArray);
$NBDir=$NBDir-1;

for ($d=0; $d<=$NBDir; $d++) {
$folder_Name1=$mp3folder."/".$DirArray[$d];


if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="mp3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }

        }
    }
}
closedir($handle);

if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }

        }
    }
}
closedir($handle);
sort($FileArray);
sort($DirArray2);
}

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="mp3" ) {
            $FileArray[] = $mp3folder."/".$file;
            $DirArray2[] = $mp3folder;
            $NBFile=$NBFile+1;
        }

        }
    }
}
closedir($handle);

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $mp3folder."/".$file;
            $DirArray2[] = $mp3folder;
            $NBFile=$NBFile+1;
        }

        }
    }
}
closedir($handle);


$NBFile=$NBFile-1;
$data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n";
$data=$data."<player showDisplay=\"yes\" showPlaylist=\"no\" autoStart=\"yes\">"."\n";

for ($i=0; $i<=$NBFile; $i++) {
$file_Name1=$FileArray[$i];
$file_Name2=$DirArray2[$i];
$file_Title=substr($file_Name1, strlen($file_Name2)+1, strlen($file_Name1)-5-strlen($file_Name2));
$data=$data."<song path=\"".$file_Name1."\" title=\"".$file_Title."\" />"."\n";
}
$data=$data."</player>";

$fp = fopen("mp3player.xml", "w");
fwrite($fp, $data);
fclose($fp);?>


<html>
<head>
<title>JW Flash MP3 Player</title>
</head>
<body bgcolor="#C0C0C0">
<p align="center">

<object type="application/x-shockwave-flash" data="mp3player.swf" width="300" height="300" wmode="transparent">
  <param name="movie" value="mp3player.swf" />
  <param name="wmode" value="transparent" />
</object>
</p>
<p align="center" style="margin-top: 200; margin-bottom: 0">
<i>Credit to Jeroen Wijering for This MP3
Flashplayer. </i></p>
<p align="center" style="margin-top: 0; margin-bottom: 0"><i>
<a target="_blank" href="http://www.jeroenwijering.com/">
http://www.jeroenwijering.com/</a></i></p>

</body>
</html>

If you still want to place the title / song name in the database, you need to create a database table with a minimum of 3 fields: ID . Name . Title

If you look at the code the file name / directory is in the Array.
  #5 (permalink)  
Old February 14th, 2006, 09:18 PM
Friend of Wrox
 
Join Date: Mar 2005
Location: , , .
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many Many thanks for your nice code. I see you did a good job in scanning all files encluding sub foldrs which i wanted to know:). I assume it should work for all folders inside mp3.To what deapth it will read mp3?mp3\subfolder1\subfolder1 or just mp3\subfolder\? May i know why the code below is repeated a few times :

Code:
if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }            

        }
    }
}
I be happy if u explain to me the logic of code and how it scans all sub folders?

Suppose i want the user to select a few songs and play his own playlist by player?php=1,2,3.Now u might know why i am thinking of database!At that stage writing playlist in xml might be problomatic .What might happend if 2 user at the same time use the script and both get same songs!! How about dealing Browserer Cache at that point? It was nice if u can show me how i can avoid using xml and just echo all the songs to flash directly .


Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying Information Php/mysql TheSchwa MySQL 1 November 12th, 2007 04:14 PM
Is It Possible to write record to DB Dwizz VB.NET 2002/2003 Basics 1 May 6th, 2005 07:16 AM
Write a script to select particular field from DB choudhmh Pro VB.NET 2002/2003 1 January 28th, 2005 08:37 AM
accessing information from a db darkhalf PHP Databases 1 March 12th, 2004 04:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.