从mySql ID创建文件夹并在其中插入数据

时间:2011-10-03 19:12:58

标签: mysql directory

我有一个非常大的记录表(大约30,000),其中包含一个id作为主键。我需要为每个记录创建一个文件夹,并在每个文件夹中放置一个图像(图像URL保存在表内)。有什么想法可以做到吗?

3 个答案:

答案 0 :(得分:3)

$sql = "SELECT id, url FROM yourtable";
$result = mysql_query($sql) or die(mysql_error()) {
while($row = mysql_fetch_assoc($result)) {
    mkdir("/path/to/location/{$row['id']}");
    file_put_contents(file_get_contents($url), "/path/to/location/{$row['id']}/image.jpg");
}

连接到数据库,选择适当的文件名,最绝对地添加一些错误处理等等......留给读者练习。

答案 1 :(得分:1)

使用您喜欢的脚本语言,只需遍历记录,创建目录并下载图像。在大多数脚本语言中,它不应该超过几行代码。

答案 2 :(得分:0)

如果您可以制作包含ID和URL列的CSV文件,请说:

User204293,http://mywebsite.org/thepath/myavatar.png

您可以使用以下perl代码段的变体:

#!/usr/bin/perl
$DestRoot="/my/path/to/the/folders/";

open(IN,shift) or die;

while($line=<IN>){
  if ($line =~ m/^([^,]+),(http:\S+)/){
     my($id,$avatar)=($1,$2);
     mkdir($DestRoot.$id);
     chdir($DestRoot.$id);
     `wget $avatar`;
  }
}
close(IN);

并使用它

perl abovescript.pl myCSVfile.csv

Marc B指出的建议也适用!