使用 pdo 添加 blob 的 zip 文件 in/out
add zip file in/out of blob using pdo
这是我第一次使用 BLOB,所以我仍在尝试弄清楚一些东西。
首先,我需要在我的数据库中添加一个额外的列,用于存储 zip 文件。我在 phpMyAdmin 中做了这个并设置了一些属性:Name = file, Type = BLOB, Default = NULL
,还有什么我需要预设的,比如 MIME type/Browser transformation/Attributes
?
现在我想使用 pdo 将 zip 文件添加(或更好地更新)到现有列中(只有更新部分的代码、pdo 连接等已经存在于我的旧代码中并且已经过测试和工作) :
$id = "id_i_got_from_somewhere_else"; //12345
$file_name= "path_to_my_zip_file"; //projectpath/packets/somename.zip, tested, the file exists
function merge_file($id, $file_name)
{
$merge_query = "UPDATE packets SET file=:file WHERE id=:id";
try
{
$stmt=$pdo->prepare($merge_query);
$stmt->bindParam(':file', $file_name, PDO::PARAM_LOB);
$stmt->bindParam(':id', $id);
$stmt->execute();
return true;
}
catch (PDOException $ex)
{
echo $ex->getMessage();
return false;
}
}
merge_file($id,$file_name);
我可以看到,即 [BLOB - 53b]
已添加到列中。为了测试,我单击 BLOB 并将其下载为 .bin-file
,将扩展名更改为 .zip
但无法打开它。 (虽然有点复杂,有没有更好的方法)?
使用 PHP 的 file_get_contents()
来做到这一点:
function merge_file($id, $file_name)
{
$merge_query = "UPDATE packets SET file=:file WHERE id=:id";
try
{
$stmt=$pdo->prepare($merge_query);
$stmt->bindParam(':file', file_get_contents($file_name), PDO::PARAM_LOB);
$stmt->bindParam(':id', $id);
$stmt->execute();
return true;
}
catch (PDOException $ex)
{
echo $ex->getMessage();
return false;
}
}
这是我第一次使用 BLOB,所以我仍在尝试弄清楚一些东西。
首先,我需要在我的数据库中添加一个额外的列,用于存储 zip 文件。我在 phpMyAdmin 中做了这个并设置了一些属性:Name = file, Type = BLOB, Default = NULL
,还有什么我需要预设的,比如 MIME type/Browser transformation/Attributes
?
现在我想使用 pdo 将 zip 文件添加(或更好地更新)到现有列中(只有更新部分的代码、pdo 连接等已经存在于我的旧代码中并且已经过测试和工作) :
$id = "id_i_got_from_somewhere_else"; //12345
$file_name= "path_to_my_zip_file"; //projectpath/packets/somename.zip, tested, the file exists
function merge_file($id, $file_name)
{
$merge_query = "UPDATE packets SET file=:file WHERE id=:id";
try
{
$stmt=$pdo->prepare($merge_query);
$stmt->bindParam(':file', $file_name, PDO::PARAM_LOB);
$stmt->bindParam(':id', $id);
$stmt->execute();
return true;
}
catch (PDOException $ex)
{
echo $ex->getMessage();
return false;
}
}
merge_file($id,$file_name);
我可以看到,即 [BLOB - 53b]
已添加到列中。为了测试,我单击 BLOB 并将其下载为 .bin-file
,将扩展名更改为 .zip
但无法打开它。 (虽然有点复杂,有没有更好的方法)?
使用 PHP 的 file_get_contents()
来做到这一点:
function merge_file($id, $file_name)
{
$merge_query = "UPDATE packets SET file=:file WHERE id=:id";
try
{
$stmt=$pdo->prepare($merge_query);
$stmt->bindParam(':file', file_get_contents($file_name), PDO::PARAM_LOB);
$stmt->bindParam(':id', $id);
$stmt->execute();
return true;
}
catch (PDOException $ex)
{
echo $ex->getMessage();
return false;
}
}