php 注意:未定义索引:图像输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13108901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notice: Undefined index: image in
提问by user1203497
Possible Duplicate:
Undefined index: file
可能重复:
未定义索引:文件
Hi I'm learning right now how to upload images to database, but I'm getting this error/notice
嗨,我正在学习如何将图像上传到数据库,但我收到此错误/通知
Notice: Undefined index: image in C:\xampp\htdocs\Pildibaas\index.php on line 19
注意:未定义索引:第 19 行 C:\xampp\htdocs\Pildibaas\index.php 中的图像
Here is my index.php whole code:
这是我的 index.php 整个代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image upload</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
<?php
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("databaseimage") or die (mysql_error());
?>
</body>
</html>
Line 19 (this line gives error) cut out from index.php:
从 index.php 中截取的第 19 行(这一行给出了错误):
echo $file = $_FILES['image']['tmp_name'];
From google found that i need to change premission of tmp folder, but it allready shoud have all premissions it needs.
从谷歌发现我需要更改 tmp 文件夹的权限,但它已经应该拥有它需要的所有权限。
In tutorial he dont get this error
在教程中,他没有收到此错误
thank you
谢谢你
回答by Mathlight
echo $file = $_FILES['image']['tmp_name'];
should be
应该
if(isset($_FILES['image'])){
echo $_FILES['image']['tmp_name'];
}
This checks first if $_FILES['image']is set. If not, this wil not be run. Therefore, you will not have an error that it is out of index.
这首先检查是否$_FILES['image']设置。如果没有,这将不会运行。因此,您不会有索引外的错误。
Because you first have to submit the form before $_FILES['image']will be set...
因为你首先要提交表单,然后$_FILES['image']才会设置...
Also, the input tag is self closing, so your form will not be:
此外,输入标签是自动关闭的,因此您的表单不会是:
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
but:
但:
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" /> <input type="submit" value="Upload" />
</form>
回答by StaticVariable
echo $file = $_FILES['image']['tmp_name'];
should be
应该
echo $_FILES['image']['tmp_name'];
or
if(!empty($_FILES) && isset($_FILES['image'])){
echo $_FILES['image']['tmp_name'];
}
you can also use
你也可以使用
print_r($_FILES);

