我正在尝试将一些数据输入数据库并将图像上传到特定目录。我使用的是以下脚本,该脚本是此问题的最高投票答案的修改版本:How to store file name in database, with other info while uploading image to server using PHP?
require($_SERVER['DOCUMENT_ROOT']."/settings/functions.php");
// This is the directory where images will be saved
$target = $_SERVER['DOCUMENT_ROOT']."/safetyarticles/images/";
$target = $target . basename( $_FILES['article_img']['name']);
date_default_timezone_set("America/Chicago");
// This gets all the other information from the form
$article_name = $_POST['article_title'];
$article_date = date("m/d/Y");
$article_creator = $_POST['article_creator'];
$article_views = "0";
$article_content = $_POST['article_content'];
$article_content_2 = $_POST['article_content_2'];
$article_img = ($_FILES['article_img']['name']);
$article_credit = $_POST['article_credit'];
// Connect to database
$conn = getConnected("safetyArticles");
// moves the image
if(move_uploaded_file($_FILES['article_img']['tmp_name'], $target))
{
// if upload is a success query data into db
mysqli_query($conn, "INSERT INTO currentArticles (article_name, article_date, article_creator, article_content, article_content_2, article_img, article_credit)
VALUES ('$article_name', '$article_date', '$article_creator', '$article_views', '$article_content', '$article_content_2', '$article_img', '$article_credit')") ;
echo "The file ". basename( $_FILES['article_img']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
我已成功连接到我的数据库,因为我的getConnected()
函数包含错误处理,如果连接失败。
出于某种原因,我不断从脚本底部收到Sorry, there was a problem uploading your file.
错误。
我错过了什么吗?我所做的就是改变一些小行,例如数据库如何连接,以及变量。我还将查询移动到仅在文件上传时发生。
我不确定我错过了什么。
是否也可以修改当前脚本以将图像重命名为$article_name
的值?例如,如果文章名称是"这是第一篇文章"那么图像将是this-is-the-first-article.jpg
?
我的HTML表单是:
<form method="post" action="http://example.com/admin/articleCreate.php" enctype='multipart/form-data'>
<input type="text" name="article_title" placeholder="What Is The Name Of This Article?" id="article_title_input">
<textarea name="article_content" placeholder="Write The Top Half Of Your Article Here." id="article_content_input"></textarea>
<input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">
<textarea name="article_content_2" placeholder="Write The Bottom Half Of Your Article Here." id="article_content_2_input"></textarea>
<input type="text" name="article_creator" placeholder="Who Is Writing This Article?" id="article_creator_input">
<input type="text" name="article_credit" placeholder="If This Article Is Copied, What Website Was It Taken From?" id="article_credit_input">
<input type="submit" value="Submit">
</form>
我做了var_dump(is_uploaded_file($_FILES['article_img']['tmp_name']));
并且它已退回true
。
答案 0 :(得分:1)
侧面编辑:这是在您编辑问题之前,只有其中一个被重命名。 https://stackoverflow.com/revisions/36367407/4
$_FILES['photo']
$_FILES['uploadedfile']
是两个不同的文件数组,您使用name="article_img"
作为名称属性。
你需要对所有人使用相同的一个。
错误报告http://php.net/manual/en/function.error-reporting.php会告诉您这件事。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
旁注:只应在暂存时进行显示错误,而不是生产。
其他编辑:
$target = $target . basename( $_FILES['photo']['name']);
如果那是你真正的编辑,那么仍然是错误的数组名称。
答案 1 :(得分:0)
我认为问题出在这一行:
def even_splitters(str)
str.chars.uniq.map do |e|
str.split(e).reject(&:empty?).map(&:length).uniq.count <= 1 ? e : nil
end.compact
end
将其更改为:
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
答案 2 :(得分:0)
你的HTML有:
<input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">
你的php正在等待$ _FILES [&#39;照片&#39;] [&#39; tmp_name&#39;]
将您的html文件输入更改为:
<input type="file" name="photo" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">