我有一个表单,用于在此表单中将文章上传到我的数据库我有一个文件上传字段我的文件上传目标如下:
每当我点击提交按钮时,表单似乎认为我已尝试上传不在允许扩展名列表中的文件,它会打印此错误(但字段会上传到数据库):
wrong files format , allowed only "Array"
我不太清楚为什么会这样,因为我知道我正在输入正确的文件格式。
public function insert ($field) {
if ($stmt = $this->mysqli->prepare("INSERT INTO articles (title, story, storyb, storyc, author, date_created, section, youtubeid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) {
/* Set our params */
$title = isset($_POST['title']) ? $this->mysqli->real_escape_string($_POST['title']) : '';
$story = isset($_POST['story']) ? $this->mysqli->real_escape_string($_POST['story']) : '';
$storyb = isset($_POST['storyb']) ? $this->mysqli->real_escape_string($_POST['storyb']) : '';
$storyc = isset($_POST['storyc']) ? $this->mysqli->real_escape_string($_POST['storyc']) : '';
$author = isset($_POST['author']) ? $this->mysqli->real_escape_string($_POST['author']) : '';
$date_created = isset($_POST['date_created']) ? $this->mysqli->real_escape_string($_POST['date_created']) : '';
$section = isset($_POST['section']) ? $this->mysqli->real_escape_string($_POST['section']) : '';
$youtubeid = isset($_POST['youtubeid']) ? $this->mysqli->real_escape_string($_POST['youtubeid']) : '';
/* Bind our params */
$stmt->bind_param('ssssssss', $title, $story, $storyb, $storyc, $author, $date_created, $section, $youtubeid);
/* Execute the prepared Statement */
$stmt->execute();
/* Echo results */
echo "Inserted {$title} into database\n";
/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
// Handling file upload
$extensions = array(".jpg",".jpeg",".gif",".png", ".JPG", ".JPEG", ".PNG", ".GIF");
$extension = strrchr($_FILES['uploadImage']['name'], '.');
$path = "../files/uploads/articles_gallery/" . $this->mysqli->insert_id;
$filename = uniqid(rand(), true);
if (!in_array($extension, $extensions))
{
echo'<center>wrong files format , allowed only <strong>"'.$extensions.'"</strong></center>';
} else {
if (!is_dir($path))
{
die('Error: ' . $mysqli->error());
}
echo "<h3>1 record added</h3>";
mkdir($path, 0777);
move_uploaded_file($_FILES['uploadImage']['tmp_name'], $path, $filename);
} // File Upload End
}
Insert.php
<div id="form">
<form action="insert.php" method="post" name="insert" id="articleform">
<input type="input" name="title" id="title" class="detail" id="title"/>
<textarea name="story" id="story" class="detail" placeholder="Insert article here"></textarea>
<input id="uploadImage" type="file" name="uploadImage" onchange="PreviewImage();" class="" />
<img id="uploadPreview" style="width: 250px; height: 200px;" />
<textarea name="storyb" id="storyb" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>
<textarea name="storyc" id="storyc" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>
<input type="input" name="author" id="author" class="detail"/>
<? $today = date("l j M Y"); // Monday 13 April 2013 ?>
<input type="hidden" name="date_created" id="date_created" class="detail" value="<? echo $today;?>" />
<input type="hidden" name="section" id="section" class="detail" value="game"/>
<input type="input" name="youtubeid" class="detail" id="youtubeid" />
<input type="submit" id="submit" name="submit" value="Submit Article " />
</form>
答案 0 :(得分:0)
从以下网址更新您的表单:
<form action="insert.php" method="post" name="insert" id="articleform">
致:
<form action="insert.php" method="post" name="insert" id="articleform" enctype="multipart/form-data">
答案 1 :(得分:0)
strchr()将给出true / false。因此,要获得文件扩展名,您可以使用以下内容:
$extension = end(explode('.', $filename));
..但在这种情况下,像pathinfo()这样的函数可能也很有用。
此外,在您的错误消息中使用$extensions
。这是一个类型为“array”的var,因此字符串表示为“Array”。我认为你的意思是$extension
(没有S)。或者,您可能希望以这种方式列出所有正确的扩展名:implode(',', $extensions)
。
请注意,检查扩展程序不是检查存在哪种内容的安全方法。我可以轻松上传.exe,只需将其重命名为.jpg。
哦,由于安全原因,我们强烈建议不要将目录/文件修改为模式777。
答案 2 :(得分:0)
如果要显示可用的扩展名,则不能仅使用消息将数组连接起来。你必须迭代所有的值。
答案 3 :(得分:0)
获取扩展程序的代码是错误的。
$extension = strrchr($_FILES['uploadImage']['name'], '.');
将其更改为
$extension = strtolower(substr($_FILES['uploadImage']['name'],-3,3));
如果扩展名为3个字符或爆炸() 并得到最后一个参数。无论如何这不是好方法,我更喜欢检查MIME类型
$extension = strtolower(end(explode('.', $_FILES['uploadImage']['name'])));
将您的扩展阵列更改为
$extensions = array("jpg","jpeg","gif","png");
也会添加到您的表单
enctype="multipart/form-data"
您的错误消息是错误的,因为您想要回显数组而不是字符串使用implode()来连接数组
implode(";",$extensions)