Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数据库中存储文件路径_Php_Mysql_Filesystems - Fatal编程技术网

Php 在数据库中存储文件路径

Php 在数据库中存储文件路径,php,mysql,filesystems,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($fil

我正在尝试获取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)的输出


它是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);
请看这里:了解更多信息。

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

一旦找到了路径,就可以执行
mysqli\u query
命令将其插入数据库


您可以找到一个有用的示例…

请发布一些代码以显示您的困境。用代码更新。不会抛出错误,但不会获取表格条目。请使用打印结果($files)更新您的问题(编辑);谢谢你的编辑。。。你对此有什么想法吗?我的意思是我可以看到文件夹,但不能看到文件夹中的任何文件。我也不知道应该如何在表中获得此输出。我想自动执行此操作,以便php扫描文件夹并插入值本身。事实上,使用此代码调用文件时,我没有收到任何错误,但在我的表中也没有得到任何结果。请看$file中的内容:echo$file;在foreach中,不显示任何内容。php文件位于/www/includes中。要扫描的目录位于/www/music中。是$files=scandir(../music/);在这一点上正确吗?没有结果。php文件位于“/www/includes/”中,我要扫描的目录是“/www/music”。
$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);