errors while uploading images in php

Notice: Undefined index: picture in C:xampphtdocspage1.php on line 33

Warning: pathinfo() expects parameter 2 to be long, string given in C:xampphtdocspage1.php on line 35 Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded. User Logged in

//<?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>

You're getting that first warning because you're using that entire code inside the same file and not using isset() or !empty() on your $_FILES array.

Use a conditional statement for this line and change the single quotes to doubles. Variables do not get parsed inside single quotes.

$target_file = '$target_dir' .basename($_FILES['picture']['name']);

as in

if(!empty($_FILES['picture'])){
   $target_file = "$target_dir" .basename($_FILES['picture']['name']);
...
}

do that, and the rest will follow.

  • Also make sure the folder has proper permissions to write to.
  • However this line:

    $target_dir= "htdocs" ."basketball/"
    

    is unclear and would read as htdocsbasketball as the folder. If that isn't what you want and that those are sub-folders, then add a / after htdocs .


    You also don't have an element to match your conditional statement:

    if(isset($_POST["submit"]))
    

    which what I think is associated with your submit button being named name="Upload Now"

    That should read as name="submit"

    You may also want to remove the value="picture" from
    <input name="picture" type="file" value="picture"> that could give you problems.


    You're also outputting before header, so uncomment the first part of your code, and get rid of the session_start(); where you have it now.

    Move session_start(); where it's located above $servername = "localhost"; and use

    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Basic Login Script</title>
    ...
    

    Sidenote:

  • Add exit; after header, otherwise your code may want to continue executing.

  • Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Error reporting should only be done in staging, and never production.

    Add $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened.

    $conn = new PDO("mysql:host=$servername;dbname=form", $username, $passwor);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    To catch other potential errors.


    Footnotes:

  • This line if(isset($_POST['logout'])){ - Your posted code doesn't have an element named "logout" for it, unless it's irrelevant.

  • Since you are using PDO, why not benefit from using PDO with prepared statements ?

  • 链接地址: http://www.djcxy.com/p/69642.html

    上一篇: 语法错误,意外的T

    下一篇: 在php中上传图片时出现错误