2017-12-24 2 views
-1

Ich bin auf Ubuntu. Ich versuche den Upload von Benutzerdateien von kleinen Bildern zu übernehmen. Ich überprüfte die $ _FILES, die mit Daten gefüllt ist. Ich habe versucht, den Bewegungsbefehl zu debuggen, aber es gibt nichts zurück.PHP FileUpload funktioniert nicht

if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    //Now handle everything 
    if(isset($_POST["submit"])) { 
     print_r($_FILES); 
     //Store the image 
     if(!empty($_FILES['uploaded_file'])) 
     { 
      $path = "/neel/public/img/"; 
      $path = $path.basename($_FILES['uploaded_file']['name']); 
      if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) { 

      echo 'Its working'; 
      } else{ 
       echo 'I am done!!!'; 
       die(); 
      } 
     } 
    createnewEvent($conn); 
    header('Location:/neel/index.php'); 
    } 
} 
+1

können Sie u zeigen s die halojoy

+0

+0

Also was du zeigst ist die /neel/admin.php. Hast du irgendeine Ausgabe? – halojoy

Antwort

1

Sie können überprüfen, ob die Datei vorhanden ist, indem Sie ihren Namen überprüfen.

if(!empty($_FILES['file']['name'])) 

Wo file für Datei den Namen des Eingabefeldes ist.

+0

hat das nicht geholfen –

0

Das Skript, das Sie gezeigt haben, wird nur dann "nichts zurückgeben", wenn $ _SERVER ['REQUEST_METHOD'] nicht "POST" ist. Angenommen, Ihre Beschreibung der Ereignisse ist korrekt, dann ist das Problem in der Form, die @halojoy gebeten hat, dass Sie hier zeigen.

Ich hoffe, dass Sie nicht das Skript zurück zu sich selbst umleiten. Außerdem sollten Sie nicht versuchen, nach einem Echo eine Weiterleitung durchzuführen.

+0

+0

1) das ist nur ein Teil des Formulars. 2) Der Code, den Sie uns gezeigt haben, wird sich nicht so verhalten, wie Sie ihn beschrieben haben (es sei denn, er ist mit einem Core-Dump abgestürzt). Wir können nicht helfen, wenn Sie das Problem nicht beschreiben können. – symcbean

0

P. G oben ist hier richtig.
Statt Prüfung, wenn $ _POST [ 'submit']
Sie sollten dies überprüfen:
if(isset($_FILES['uploaded_file']['name']))

0

diesen Code Versuchen Sie es ist eine komplette Anmeldeformular mit PHP, Bootstrap

HTML

<div class="container"> 
    <div class="row"> 
     <br> 
      <h1 class="text-center">Create new account</h1> 
      <br> 
     <div class="col-lg-4 col-md-6 col-sm-12"> 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
       <div class="form-group"> 
        <input type="text" class="form-control form-control-lg" name="name" placeholder="Your Name"> 
       </div> 
       <div class="form-group"> 
        <input type="text" class="form-control form-control-lg" name="username" placeholder="Username"> 
       </div> 
       <div class="form-group"> 
        <input type="password" class="form-control form-control-lg" name="password" placeholder="Password"> 
       </div> 
       <div class="form-group text-center"> 
        <div class='file-input'> 
         <input type='file' name="profile-img"> 
         <span class='button label' data-js-label>Choose your profile image</span> 
        </div> 
       </div> 
       <div class="form-group"> 
        <input type="submit" class="btn btn-success form-control form-control-lg" name="signup" value="Signup"> 
       </div> 
      </form> 
     </div> 
    </div> 
</div> 

PHP

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $errors; 
    if (empty($_POST['name'])) { 
     $errors[] = "Name field is empty"; 
    } 
    if (empty($_POST['username'])) { 
     $errors[] = "Username field is empty"; 
    } 
    if (empty($_POST['password'])) { 
     $errors[] = "Password field is empty"; 
    } 
    if (empty($_FILES['profile-img'])) { 
     $errors[] = "You should upload profile image"; 
    } else{ 
     $img = $_FILES['profile-img']; 
     $ext = end(explode('.', $img['name'])); 
     $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif'); 
     $max_size = 4; //MegaBytes 
     if (! in_array($ext, $allowed_extensions)) { 
      $errors[] = "Please , Choose a valid image"; 
     } 
     $img_size = $img['size']/1000000; // By Megabyte 
     if ($img_size > $max_size) { 
      $errors[] = "This picture is so large"; 
     } 
    } 
    if (!empty($errors)) { 
     echo '<div class="container">'; 
     foreach ($errors as $error) { 
      echo ' 
       <div class="alert alert-danger" role="alert"> 
       ' . $error . ' 
       </div> 
      '; 
     } 
     echo "</div>"; 
    } else { 
     $username = filter_var(htmlentities(trim($_POST['username'])), FILTER_SANITIZE_STRING); 
     $name = filter_var(htmlentities(trim($_POST['name'])), FILTER_SANITIZE_STRING); 
     $password = sha1(filter_var(htmlentities(trim($_POST['password'])), FILTER_SANITIZE_STRING)); 
     // Profile Picture :- 
     $imgname = uniqid(uniqid()) . @date("Y-m-d") . "." . $ext; 

     $target_bath = "uploads/imgs/"; 
     $target_bath = $target_bath . basename($imgname); 
     $orginal_img = $img['tmp_name']; 

     if (move_uploaded_file($orginal_img, $target_bath)) { 
      $sql = "INSERT INTO users(name, username, password,profile_picture, r_d) VALUES ('$name', '$username', '$password', '$target_bath', NOW())"; 
      $result = mysqli_query($conn, $sql); 
      header('location: ./login.php'); 
     } 


    } 
}