gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:10 25 4
gpt4 key购买 nike

抱歉,如果这个问题真的很啰嗦,但我想尽可能详细地回答我的问题。

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

抛出的错误是:

注意:未定义索引:C:\xampp\htdocs\web_design_cms\create_wireframe.php 第13行的图片

表单代码如下:

<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 有关?在函数结束时,我返回变量,然后在后脚本中尝试获取该值并将其发布到数据库中的“page_main_image”字段中,但显然我做错了什么?

再次抱歉发了这么长的帖子,但如果您能给我任何帮助,我将不胜感激!谢谢

最佳答案

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

看看这个 documentation.

编辑

您需要更改主要逻辑。

$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"];

关于php - 使用PHP将文件路径名保存到mySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com