我有一个表单,用于在此表单中将文章上传到我的数据库我有一个文件上传字段,我的文件上传目标如下:
- 将文件扩展名限制为图像格式
- 将文件重命名为随机的东西
- 根据条目的ID创建一个新目录进入数据库
- 保留上传文件的扩展名
每次我点击提交按钮时,表单似乎认为我已尝试上传不在允许扩展名列表中的文件,它会打印此错误(但字段已上传到数据库):
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
}
插入.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>