如何使用PHP上传图像



我无论如何都不能在我的项目中上传文件。我有一个商店的管理员页面,用户应该能够在其中创建产品。我使用了一个表单,用户在其中输入所需的所有数据。我无法将产品映像上载到我的服务器。我的第一次尝试是将表单提交给数据库处理程序,但除了$_FILES属性之外的所有内容都已填充。所以我尝试了很多其他格式,但都不起作用。我最后一次尝试是在提交后保留在同一个文件中,但$_FILES仍然为空。

<?php
if ($_SERVER["REQUEST_METHOD"] === "POST"){
echo "<pre>";
print_r($_POST);
print_r($_FILES);
echo "</pre>";
die();
}
?>
<form id="create-item-form" action="" method="post">
<label for="new_product_image_field">Image:</label>
<input type="file" id="new_product_image_field" name="new_product_image">
<label for="new_product_name_field">Name:</label>
<input type="text" id="new_product_name_field" name="new_product_name" required>
<label for="new_product_alcoholContent_field">Alcohol Content:</label>
<input type="number" id="new_product_alcoholContent_field" name="new_product_alcoholContent"
min="1" max="20" step="0.1">
<label for="new_product_price_field">Price:</label>
<input type="number" id="new_product_price_field" name="new_product_price"
min="0" step="0.1">
<label for="new_product_description_field">Description</label>
<textarea name="new_product_description" id="new_product_description_field" cols="30" rows="10" required></textarea>
<input type="submit" value="Create" id="create_new_product_button" name="create_new_product">
</form>

结果:

Array
(
[new_product_image] => beer.jpg
[new_product_name] => b
[new_product_alcoholContent] => 2
[new_product_price] => 2
[new_product_description] => b
[create_new_product] => Create
)
Array
(
)

我非常感谢的帮助

请在表单中添加enctype atrribute。

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

如果你需要更多的帮助,请告诉我,因为你的代码中缺少很多部分,比如获取上传文件的临时名称,没有这样的方法move_uploaded_file()在你的代码中,所以我没有放下这些步骤,因为我假设你已经熟悉如何处理上传的文件

最新更新