Visualizzazione risultati 1 fino 3 di 3

Discussione: upload files

  1. #1
    Data registrazione
    30-11-2010
    Messaggi
    10

    Unhappy upload files

    Ciao a tutti. Sto elaborando un form per la registrazione ad un sito. Oltre a nome, mail e password vorrei permettere l'upload di una foto come profilo. Ho scritto questo codice anche grazie a diversi tutorial sul web ma mostra un errore "Notice: Undefined index: image". Sapreste aiutarmi? Grazie mille :)

    Codice PHP:
    <?php
    session_start
    ();

    if(isset(
    $_SESSION['userSession'])!="")
        
    {
        
    header("Location: home.php");
        
    }

    include_once 
    'dbconnect.php';

    if(isset(
    $_POST['btn-signup']))
    {
        
    $uname $MySQLi_CON->real_escape_string(trim($_POST['user_name']));
        
    $email $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
        
    $upass $MySQLi_CON->real_escape_string(trim($_POST['password']));
        
        
    $new_password md5($upass);
        
        
    $file rand(1000,100000)."-".$_FILES['image']['name'];
            
    $file_loc $_FILES['image']['tmp_name'];
        
    $folder="myupload";
     
        
    move_uploaded_file($file_loc$folder.$file);
      

        
    $check_email $MySQLi_CON->query("SELECT user_email FROM users WHERE user_email='$email'");
        
    $count=$check_email->num_rows;
        
        if(
    $count==0){
            
            
    $query "INSERT INTO users(user_name,user_email,user_pass, user_image) VALUES('$uname','$email','$new_password','$folder/$file')";
            
            if(
    $MySQLi_CON->query($query))
            {
                
    $msg "<div class='alert alert-success'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; successfully registered !
                        </div>"
    ;
            }
            else
            {
                
    $msg "<div class='alert alert-danger'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; error while registering !
                        </div>"
    ;
            }
        }
        else {
            
            
            
    $msg "<div class='alert alert-danger'>
                        <span class='glyphicon glyphicon-info-sign'></span> &nbsp; sorry email already taken !
                    </div>"
    ;
                
            }
        
        
    $MySQLi_CON->close();
    }

    ?>

    <html>
    <head>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen"> 
    <link rel="stylesheet" href="style.css" type="text/css" />

    </head>
    <body>

    <div class="signin-form">

        <div class="container">
         
            
           <form class="form-signin" method="post" id="register-form">
          
            <h2 class="form-signin-heading">Sign Up</h2><hr />
            
            <?php
            
    if(isset($msg)){
                echo 
    $msg;
            }
            else {
                
    ?>
                <div class='alert alert-info'>
                    <span class='glyphicon glyphicon-asterisk'></span> &nbsp; all the fields are mandatory !
                </div>
                <?php
                
    }
                
    ?>
              
            <div class="form-group">
            <input type="text" class="form-control" placeholder="Username" name="user_name" required  />
            </div>
            
            <div class="form-group">
            <input type="email" class="form-control" placeholder="Email address" name="user_email" required  />
            <span id="check-e"></span>
            </div>
            
            <div class="form-group">
            <input type="password" class="form-control" placeholder="Password" name="password" required  />
            </div>
            
            <div class="form-group">
            <input name="image" type="file" size="40" /><br /><br />
             </div>
             
             
             <hr />
            
            <div class="form-group">
                <button type="submit" class="btn btn-default" name="btn-signup">
                <span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account
                </button> 
                
                <a href="index.php" class="btn btn-default" style="float:right;">Log In Here</a>
                
            </div> 
          
          </form>

        </div>
        
    </div>

    </body>
    </html>

  2. #2
    mzanella non Ŕ connesso AlterGuru
    Data registrazione
    29-12-2015
    Messaggi
    1,684

    Predefinito

    Correggi il tag form aggiungendo l'enctype:
    Codice HTML:
    <form class="form-signin" method="post" enctype="multipart/form-data" id="register-form">

  3. #3
    Data registrazione
    30-11-2010
    Messaggi
    10

    Predefinito

    Perfetto, grazie dell'aiuto. Questo Ŕ il codice con l'implementazione dell'uploading, anche se non ho controllato se funziona anche il multiple upload..


    Codice PHP:

    <?php

    //inizializzo sessione.

    session_start();

    if(isset(
    $_SESSION['userSession'])!="")
        
    {
        
    header("Location: home.php");
        
    }

    include_once 
    'dbconnect.php';

    if(isset(
    $_POST['btn-signup']))
    {
        
    $uname $MySQLi_CON->real_escape_string(trim($_POST['user_name']));
        
    $email $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
        
    $upass $MySQLi_CON->real_escape_string(trim($_POST['password']));
        
        
    //password criptata
        
    $new_password md5($upass);
        
        
    //parametri della foto da caricare
        
    $target_dir "myupload/";
        
        
    $target_file $target_dir basename($_FILES["image"]["name"]);
        
        
    $uploadOk 1;
        
        
    $imageFileType pathinfo($target_file,PATHINFO_EXTENSION);
        
        
    // Check if image file is a actual image or fake image    
        
    $check getimagesize($_FILES["image"]["tmp_name"]);
        
        if(
    $check !== false) {
            
            echo 
    "File is an image - " $check["mime"] . ".";
            
            
    $uploadOk 1;
            
        } else {
            
            
    $msg "<div class='alert alert-danger'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Sorry, this file is not an image.
                        </div>"
    ;
                        
            
    $uploadOk 0;
        }

        
    // Allow certain file formats
        
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        
    && $imageFileType != "gif" ) {
            
    $msg "<div class='alert alert-danger'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Sorry, only JPG, JPEG, PNG & GIF files are allowed.
                        </div>"
    ;
        
    $uploadOk 0;
    }
        
    // Check if $uploadOk is set to 0 by an error
        
    if ($uploadOk == 0) {
        echo 
    "Sorry, your file was not uploaded.";
        
    // if everything is ok, try to upload file
        
    } else {
        if (
    move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            echo 
    "The file "basename$_FILES["image"]["name"]). " has been uploaded.";
        } else {
            echo 
    "Sorry, there was an error uploading your file.";
        }
    }

        
    $check_email $MySQLi_CON->query("SELECT user_email FROM users WHERE user_email='$email'");
        
    $count=$check_email->num_rows;
        
        if(
    $count==0){
            
            
    $query "INSERT INTO users(user_name,user_email,user_pass, user_image) VALUES('$uname','$email','$new_password','$target_file')";
            
            if(
    $MySQLi_CON->query($query))
            {
                
    $msg "<div class='alert alert-success'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; successfully registered !
                        </div>"
    ;
            }
            else
            {
                
    $msg "<div class='alert alert-danger'>
                            <span class='glyphicon glyphicon-info-sign'></span> &nbsp; error while registering !
                        </div>"
    ;
            }
        }
        else {
                
            
    $msg "<div class='alert alert-danger'>
                        <span class='glyphicon glyphicon-info-sign'></span> &nbsp; sorry email already taken !
                    </div>"
    ;
                
            }
        
        
    $MySQLi_CON->close();
    }

    ?>


    <html>
    <head>
    <title>Login & Registration System</title>
    <link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    </head>
    <body>

    <div class="signin-form">

        <div class="container">
            
           <form class="form-signin" method="post" enctype="multipart/form-data" id="register-form">    
          
            <h2 class="form-signin-heading">Sign Up</h2><hr />
            
            <?php
            
    if(isset($msg)){
                echo 
    $msg;
            }
            else {
                
    ?>
                <div class='alert alert-info'>
                    <span class='glyphicon glyphicon-asterisk'></span> &nbsp; all the fields are mandatory !
                </div>
                <?php
                
    }
                
    ?>
              
            <div class="form-group">
            <input type="text" class="form-control" placeholder="Username" name="user_name" required  />
            </div>
            
            <div class="form-group">
            <input type="email" class="form-control" placeholder="Email address" name="user_email" required  />
            <span id="check-e"></span>
            </div>
            
            <div class="form-group">
            <input type="password" class="form-control" placeholder="Password" name="password" required  />
            </div>
            
            <input type="file" class="form-control" name="image" />    
             
             <hr />
            
            <div class="form-group">
                <button type="submit" class="btn btn-default" name="btn-signup">
                <span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account
                </button> 
                
                <a href="index.php" class="btn btn-default" style="float:right;">Log In Here</a>
                
            </div> 
          
          </form>

        </div>
        
    </div>

    </body>
    </html>
    Ultima modifica di alessandroschiri : 31-01-2016 alle ore 10.40.13

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •