function addFlyer($db){
if (isset($_FILES['file_array'])) {
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for ($i = 0; $i < count($tmp_name_array); $i++) {
if (file_exists('images/' . sha1_file($name_array[$i]))) {
// File exists already in the folder.
$uploadOk = 0;
header("Location: admin.php?action=errorupload");
}
else {
if (move_uploaded_file($tmp_name_array[$i], "images/" . sha1_file($name_array[$i]))) {
//upload complete
$uploadOk = 1;
} else {
//upload failed.
header("Location: admin.php?action=errorupload");
}
}
}
if($uploadOk == 1 && isset($_POST['month'], $_POST['title'], $_POST['content'], $_POST['content1'], $_POST['content2'])) {
$query = $db->prepare("INSERT INTO version (month, title, content, content1, content2, imgurls, imgurlm, imgurll, imgurlfb) VALUES(:month, :title, :content, :content1, :content2, :imgurls, :imgurlm, :imgurll, :imgurlfb)");
$query->bindParam(':month', $_POST['month'], PDO::PARAM_STR);
$query->bindParam(':title', $_POST['title'], PDO::PARAM_STR);
$query->bindParam(':content', $_POST['content'], PDO::PARAM_STR);
$query->bindParam(':content1', $_POST['content1'], PDO::PARAM_STR);
$query->bindParam(':content2', $_POST['content2'], PDO::PARAM_STR);
$query->bindParam(':imgurls', sha1_file($_FILES['file_array']['name'][0]), PDO::PARAM_STR);
$query->bindParam(':imgurlm', sha1_file($_FILES['file_array']['name'][1]), PDO::PARAM_STR);
$query->bindParam(':imgurll', sha1_file($_FILES['file_array']['name'][2]), PDO::PARAM_STR);
$query->bindParam(':imgurlfb', sha1_file($_FILES['file_array']['name'][3]), PDO::PARAM_STR);
$query->execute();
header("Location: admin.php");
}
}}
目前我得到了这段代码,但它没有对文件进行哈希处理,也不会将其放入数据库中。现在出了什么问题?它只是转到我所做的错误页面,有人知道解决方案吗?
当然行
不通。
$name_array = $_FILES['file_array']['name'];
...
if (file_exists('images/' . sha1_file($name_array[$i]))) {
正如您可以从 PHP 文档中了解到文件上传的信息,
$_FILES['...']['name']
是:
客户端计算机上文件的原始名称。
您将该名称传递给sha1_file()
,并期望它在服务器上找到文件并对其进行处理。
如果您只想存储每个文件一次,并通过使用其内容的 SHA1 命名文件来执行此操作,那么您应该 SHA1 在您的服务器上等待处理的内容,以 $_FILES['file_array']['tmp_name']
为单位。或者$tmp_name_array
,如果你喜欢这样的话。
并且您应该只为每个文件调用sha1_file()
一次,而不是两次,因为这是一个耗时的操作。
试试这个(我只更改了包含$local_file_name
的行(:
for ($i = 0; $i < count($tmp_name_array); $i++) {
$local_file_name = sha1_file($tmp_name_array[$i]);
if (file_exists('images/'.$local_file_name)) {
// File exists already in the folder.
$uploadOk = 0;
header("Location: admin.php?action=errorupload");
} else {
if (move_uploaded_file($tmp_name_array[$i], 'images/'.$local_file_name)) {
//upload complete
$uploadOk = 1;
} else {
//upload failed.
header("Location: admin.php?action=errorupload");
}
}
}