我收到以下错误:
注意:未定义的索引:第5行的C:\wamp\www_upload\index.php中的图像
注意:未定义的索引:第7行的C:\wamp\www_upload\index.php中的图像
代码如下:
<?php
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']). " has been uploaded</p>";
} else{
echo "<p>There was an error uploading the image, please try again!</p>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
<fieldset>
<input name="theimage" type="file" />
<input type="submit" value="Upload" />
</fieldset>
</form>
</body>
</html>有什么办法解决这个问题吗?
发布于 2011-08-17 04:37:28
<?php
if(!empty($_FILES['theimage'])) {
// YOUR CURRENT PHP CODE HERE
?>问题是,即使没有发布任何内容(就像您第一次加载页面时),上传脚本也会执行。
发布于 2011-08-17 04:37:11
在尝试对其执行任何操作之前,请检查以确保获取的是$_FILES或$_POST格式的数据:
<?php
if(isset($_FILES)) { // if(isset($_POST))
// would work as well
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']).
" has been uploaded</p>";
} else {
echo "<p>There was an error uploading the image!</p>";
}
}
?>发布于 2011-08-17 04:37:20
将提交按钮更改为:
<input type = 'submit' name = 'theSubmitBtn' value = 'Upload'>然后,将所有php代码放在if check中的顶部:
if(isset($_POST['theSubmitBtn'])) {
... Your Code ...
}看看这对你是不是有用。另外,如果要在生产环境中使用它,还需要添加更多的错误检查。
https://stackoverflow.com/questions/7084656
复制相似问题