使用PHP将文件路径名保存到mySQL数据库



如果这个问题真的很冗长,我很抱歉,但我想尽可能详细地回答我的问题,希望能得到答案。

基本上,我有一个多部分的表单,我想添加文件上传功能。我创建了表单,一个处理将图像保存到目录的函数,还有一个处理向数据库发布表单值的脚本。我的问题是,在提交表单时,我将图像存储在我希望的位置,但没有任何表单值被发送到数据库。我认为我的问题是,我试图将新路径名存储为一个变量,然后在POST脚本中调用该变量,但我认为这是错误的?

抛出的错误是:

注意:第13行C:\examplep\htdocs\web_design_cms\create_wireframe.php中的未定义索引:image

这是表单的代码:

<form action="create_wireframe.php" method="post" enctype="multipart/form-data">

<!-- page_title -->

<p>
<label>Title:</label><br/>
<input type="text" class="text small" name="wireframe_title" id="wireframe_title" value=""  />
<span class="note">*required</span>
</p>    
<!-- page_meta_title -->

<p>
<label>Browser Title:</label><br/>
<input type="text" class="text small" name="browser_title" id="browser_title" value=""  >
<span class="note">*required</span>
</p>
<!-- url_key -->            

<p>
<label>Permanent Link:</label><br/>
<input type="text" class="text small" name="url_key" id="url_key" value=""/>
</p>
<!-- page_image -->
<div style="float:left" >
<!-- wireframe_type -->
<p>
<label>Type:</label><br/>
<select name="wireframe_type" id="wireframe_type" class="styled" style="width:240px">
<option value="design"  > Design Draft</option>
<option value="wireframe" selected > Wireframe</option>                          
</select>
<span class="note">*required</span>
</p>                                            
</div>
<div style="clear:both"></div>
<div class="message info"><p>
Allowed file types for upload: jpg,jpeg,gif,png.<br/>
Max file size: 10Mb<br/>
Picture size: 4096x4096 px
</p></div>

<p>
<label>Upload Image:</label><br/>                   
<input type="file" name="page_main_image" id="page_main_image" value=""  />
</p>

<!-- page_bg_color -->
<p>
<label>Color:</label><br/>
<input type="text" class="text small" maxlength="6" size="6" style="width:60px" id="colorpickerField"  name="page_bg_color" value="ffffff" />
<span id="colorSelector" style="background-color:#ffffff;padding:7px 10px;">&nbsp;</span>
<span class="note">*required</span>
</p>

<p>
<input type="submit" class="submit small" value="Save" name="submit" />
</p>
</form>

以下是处理表单数据的php脚本"create_wireframe.php":

<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_POST['submit'])) {
//Process the form
$image = upload_file();
$wireframe_title = mysql_prep($_POST["wireframe_title"]);
$browser_title = $_POST["browser_title"];
$url_key = $_POST["url_key"];
$wireframe_type = $_POST["wireframe_type"];
$image = $_POST["page_main_image"];
$page_bg_color = $_POST ["page_bg_color"];
$query  = "INSERT INTO wireframes (";
$query .= " wireframe_title, browser_title, url_key, wireframe_type, page_main_image, page_bg_color";
$query .= " ) VALUES (";
$query .= " '{$wireframe_title}', '{$browser_title}', '{$url_key}', '{$wireframe_type}', '{$image}', '{$page_bg_color}' ";
$query .= ")";
echo $query;
try { $result = mysqli_query($connection, $query);
} catch (Exception $e) {
return 'Caught exception: '+  $e->getMessage()+ "n";
}
//Test if there was a query error
if ($result) {
//Success
// would normally use a redirect ie redirect_to("somepage.php");
//$message = "Subject created.";
redirect_to("wireframes.php");
}else {
//failure
//$message = "Subject creation failed.";
//redirect_to("add_project.php");
echo $query;
}
} else {
// This is probably a GET request
redirect_to("add_edit_wireframe.php");
}
?>
<?php
// Close database connection
if(isset($connection)){ mysqli_close($connection); }
?>

最后是我创建的用于处理目录中图像存储的功能:

function upload_file(){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["page_main_image"]["name"]);
$extension = end($temp);
if ((($_FILES["page_main_image"]["type"] == "image/gif")
|| ($_FILES["page_main_image"]["type"] == "image/jpeg")
|| ($_FILES["page_main_image"]["type"] == "image/jpg")
|| ($_FILES["page_main_image"]["type"] == "image/pjpeg")
|| ($_FILES["page_main_image"]["type"] == "image/x-png")
|| ($_FILES["page_main_image"]["type"] == "image/png"))
&& ($_FILES["page_main_image"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["page_main_image"]["error"] > 0) {
echo "Return Code: " . $_FILES["page_main_image"]["error"] . "<br>";;
}
else {
echo "Upload: " . $_FILES["page_main_image"]["name"] . "<br>";
echo "Type: " . $_FILES["page_main_image"]["type"] . "<br>";
echo "Size: " . ($_FILES["page_main_image"]["size"] / 1024) . " kb<br>";
if (file_exists("uploads/" . $_FILES["page_main_image"]["name"]))
{
echo $_FILES["page_main_image"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["page_main_image"]["tmp_name"],
"uploads/" . $_FILES["page_main_image"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["page_main_image"]["name"];
$image="{$_FILES['page_main_image']['name']}";
}
}
}
else {
echo "Invalid file";
}
return $image;
}

我不完全确定,但我认为问题与我试图存储路径名的变量$image有关?在函数结束时,我返回变量,然后在post脚本中尝试获取该值并将其发布到数据库中的"page_main_image"字段中,但很明显我做错了什么?

很抱歉发了这么长的帖子,但你能给我的任何帮助都将不胜感激!感谢

在处理上传的文件(如图像)时,需要使用$_files而不是$_POST来访问数据。

查看此文档。

编辑

你需要改变主要的逻辑。

$image = upload_file(); //Good
$wireframe_title = mysql_prep($_POST["wireframe_title"]);
$browser_title = $_POST["browser_title"];
$url_key = $_POST["url_key"];
$wireframe_type = $_POST["wireframe_type"];
//Delete this, you're throwing out the value from upload_file()
$image = $_POST["page_main_image"];

最新更新