Image upload error with PHP

Asked

Viewed 236 times

1

When sending the registration form name, email and password, but the image does not register and I can not find the error.

A few unsuccessful attempts:

  • When placing enctype="multipart/form-data" the file put to Mysql temp, but does not upload the image to the folder.

Code:

<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_REQUEST['username'])){
    $username = stripslashes($_REQUEST['username']); // removes backslashes
    $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
    $email = stripslashes($_REQUEST['email']);
    $email = mysqli_real_escape_string($con,$email);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($con,$password);

    $picture    = $_FILES['foto']['tmp_name'];
    $img_name = $_FILES['foto']['name'];
    $dir      = "/var/www/uploads/";
    $path     = $dir.$img_name;
    move_uploaded_file( $picture, $path );  

    $date = date("Y-m-d H:i:s");

    $query = "INSERT into `users` (username, password, email, picture, date) VALUES ('$username', '".md5($password)."', '$email', '$picture' ,'$date')";
    $result = mysqli_query($con,$query);
    if($result){
        echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
    }
}else{
?>

My form

 <form name="registration" action="" method="post">
 Upload Imagen : <input type="file" name="foto" />
 <input type="text" name="username" placeholder="Username" required />
 <input type="email" name="email" placeholder="Email" required />
 <input type="password" name="password" placeholder="Password" required />
 <input type="submit" name="submit" value="Register" />
  </form>

My table in the bank:

  CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `picture` varchar(100) NOT NULL,
  `level` tinyint(1) NOT NULL DEFAULT '0',
   `ative` tinyint(1) NOT NULL DEFAULT '0',
   `date` datetime NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
  • Ever tried to put enctype="multipart/form-data" on the form? It is necessary to have.

  • 1
  • already tried but it posts as /tmp/phpTWuYuc temporario and does not send the image to the upload folder

  • So have you identified a strange behavior in the code... Please edit your question and describe EXACTLY what you tried to do and what’s going on in the code.

  • Probably your briefcase /var/www/uploads/ does not have write and run permission. What error message?

  • First: it makes no sense you store $picture in Mysql since it is the temporary file path. It would be better to save $path. Second: the directory uploads is there? Third: check the function return move_uploaded_file and activate error messages. Official documentation.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.