在数据库中存储文件路径

时间:2014-05-28 09:43:31

标签: php mysql filesystems

我试图在sql数据库中获取文件路径和文件名。

我在'/ music / single /'中有一个名为'run.mp3'的文件,为此我需要填写表格:

id artist filename filepath

ai single run      /music/single/run.mp3

我搜索谷歌有关此问题的任何解释,因为我对php和mysql很新,我真的需要帮助。

我尝试过:

$files = scandir(../music/); 

foreach($files as $file) {
    $paths  = explode("/", $file);

    $artist = $paths[1];
    $song = $paths[2];

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO playlist (artist, song,filepath) VALUES ('$artist', '$song', '$file')";

mysqli_close($con);

$print_r($files);的编辑输出:

Array ( [0] => . [1] => .. [2] => C418 - Alpha [3] => C418 - Beta ),
Array ( [0] => . [1] => .. [2] => C418 - Alpha [3] => C418 - Beta ),
Array ( [0] => . [1] => .. [2] => C418 - Alpha [3] => C418 - Beta ),
Array ( [0] => . [1] => .. [2] => C418 - Alpha [3] => C418 - Beta )

2 个答案:

答案 0 :(得分:0)

是SQL还是MySQL数据库?对于MySQL,您可以使用它。首先打开你的连接。所以填写你的数据库,用户名和密码。

$con=mysqli_connect("localhost","username","password","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO your_table (artists, filename, filepath)
VALUES ('single', 'run', ' /music/single/run.mp3' )");


mysqli_close($con);

请点击此处:http://www.w3schools.com/php/php_mysql_insert.asp了解更多信息。

答案 1 :(得分:0)

可能是this文档可以帮助您使用PHP获取路径。

一旦你有了路径,就可以执行mysqli_query命令来插入数据库

Here你可以找到一个有用的例子......