很抱歉,如果这个问题真的很长,但我想尽可能详细,希望得到我的问题的答案。
基本上我有一个多部分表单,我想添加文件上传功能。我创建了表单,一个处理将图像保存到目录的函数,以及一个处理将表单值发布到数据库的脚本。我的问题是我有图像存储我想要它现在提交表单但没有任何表单值被发送到数据库。我认为我的问题是我试图将新路径名称存储为变量,然后我在POST脚本中调用但我认为这是错误的?
引发的错误是:
注意:未定义的索引:第13行的C:\ xampp \ htdocs \ web_design_cms \ create_wireframe.php中的图像
以下是表单的代码:
<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;"> </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”字段中,但显然我做错了什么?
很抱歉再次发帖,但是你能给我的任何帮助都将非常感激!感谢
答案 0 :(得分:3)
在处理上传的文件(例如图片)时,您需要使用$ _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"];