注意:未定义的索引:第33行的C:\ xampp \ htdocs \ page1.php中的图片
警告:pathinfo()期望参数2为long,第35行的C:\ xampp \ htdocs \ page1.php中给出的字符串 对不起,只有JPG,JPEG,PNG&允许使用GIF文件。因此,您的文件未上传。用户已登录
//<?PHP
//session_start();
//if (!isset($_SESSION['login_user'])) {
//header ("Location: page2.php");/
//}
//?>
<html>
<head>
<title>Basic Login Script</title>
</head>
<body>
<?php
session_start();
$servername = "localhost";
$username = "root";
$passwor = "";
$dbname = "form";
$name = $_SESSION['login_user'];
echo $name;
$conn = new PDO("mysql:host=$servername;dbname=form", $username, $passwor);
$stmt = $conn->prepare("SELECT id, username, password FROM users where username='$name'");
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "Connected successfully";
$target_dir= "htdocs" ."basketball/";
$target_file = '$target_dir' .basename($_FILES['picture']['name']); //heres an error
$uploadOk = 1;
$imageFileType=pathinfo('$targetfile','PATHINFO_EXTENSION');//heres an error
if(isset($_POST["submit"]))
{
$check = getimagesize($_FILES['picture']['tmp_name']);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES['picture']['tmp_name'], $target_file)) {
echo "The file ". basename( $_FILES["picture"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
User Logged in<br/><br/>
<form action="page1.php" method="post" enctype="multipart/form-data">
<input name="picture" type="file" value="picture">
<input name="Upload Now" type="submit" value="Upload Image">
</form>
<P>
<?php
if(isset($_POST['logout'])){
//$_SESSION['user_login'] = '';
//die;
session_destroy();
header("Location: login.php");}
?>
</body>
</html>
答案 0 :(得分:2)
您收到第一个警告是因为您在同一个文件中使用了整个代码而未在isset()
数组上使用!empty()
或$_FILES
。< / p>
对此行使用条件语句,并将单引号更改为双精度。变量不会在单引号内解析。
$target_file = '$target_dir' .basename($_FILES['picture']['name']);
,如
if(!empty($_FILES['picture'])){
$target_file = "$target_dir" .basename($_FILES['picture']['name']);
...
}
这样做,其余的将会跟进。
然而这一行:
$target_dir= "htdocs" ."basketball/"
不清楚,并且会将htdocsbasketball
读作文件夹。如果这不是你想要的,那些是子文件夹,那么在/
之后添加htdocs
。
您也没有符合条件陈述的元素:
if(isset($_POST["submit"]))
我认为与您的提交按钮名为name="Upload Now"
那应该是name="submit"
您可能还想从中删除value="picture"
<input name="picture" type="file" value="picture">
可能会给你带来麻烦。
您还要在标头之前输出,因此请取消注释代码的第一部分,然后摆脱现在拥有它的session_start();
。
将session_start();
移到$servername = "localhost";
上方并使用
<?php
session_start();
?>
<html>
<head>
<title>Basic Login Script</title>
...
旁注:
exit;
,否则您的代码可能希望继续执行。将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
在打开连接后立即添加$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
。
$conn = new PDO("mysql:host=$servername;dbname=form", $username, $passwor);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
捕捉其他潜在错误。
脚注:
此行if(isset($_POST['logout'])){
- 您发布的代码没有名为&#34; logout&#34;的元素。为此,除非它无关紧要。
由于您使用的是PDO,为什么不能从使用PDO with prepared statements中受益?