Yvalson Dronkers
Yvalson Dronkers

Reputation: 67

File upload form is not uploading

I'm running a small PHP project for school, and I'm trying to set up a form in which a user can upload a profile picture.

I tried working with W3Schools, but I couldn't get it to work with my code. Here is the link to that page: https://www.w3schools.com/php/php_file_upload.asp

First I declare some variables:

$target_dir = "profilepic/";
$target_file = $target_dir . basename($_FILES["image_to_upload"]["name"]);

And then I move the uploaded file to the right directory. After the registration it gets checked using this:

move_uploaded_file($_FILES["image_to_upload"]["tmp_name"], $target_file);

I already set parameters in the the form so a user can only select image files:

<form action='index.php' method='post' enctype='multipart/form-data'>
    <input type= 'text' name='leerlingid' placeholder='Username' required>
    <input type= 'text' name='mail' placeholder='Email Address' required>
    <input type= 'password' name='wachtwoord' placeholder='Password' required>
    <input type= 'password' name='wachtwoordcheck' placeholder='Password confirmation' required>
    <input type= 'text' name='voornaam' placeholder='First name' required>
    <input type= 'text' name='achternaam' placeholder='Last name' required>
    <select name='sekse'>
        <option value='Man'>Man</option>
        <option value='Vrouw'>Vrouw</option>
    </select>
    Profile picture:
    <INPUT id='image_to_upload' NAME=image_to_upload' TYPE='file' accept='image/x-png,image/gif,image/jpeg' required>
    <input type='submit' name='reg' value='Register'>

    </form>

wachtwoord is password
leerlingid is username
voornaam is firstname
achternaam is lastname
sekse is sex

The project is in Dutch so sorry for the Dutch names.

The form runs correctly and I get 0 errors, but after a user is registered no file has been uploaded. I'm using a school server, but if I'm correct the $target_dir should start from where the form.php is placed.

I don't know if file_uploads = On in the ini file, but I can upload my files from FileZilla so I thought it was. (I have no access to that file though so there is no way for me to check or change it).

If someone could point me to why it's not working that would be a great help.

Edit:

I now tried this code as well:

                    $upload = move_uploaded_file( $_FILES["image_to_upload"]["tmp_name"], $target_file );
                if( $upload ) {
                    header("Location: good.php");
                } else {
                    header("Location: error.php");
                //echo "Error:" . $_FILES["image_to_upload"]["error"];
                }

But my code skips this entirely. It still doesn't upload the picture and just goes to the confirmed registration page.

EDIT: I fixed my problem. Look at my other answer. It contains my errors.

Upvotes: 0

Views: 528

Answers (2)

Yvalson Dronkers
Yvalson Dronkers

Reputation: 67

I fixed the problem with the help of @04FS. I wrongly changed "name" to "image_to_upload" in the $target_file variable after changing that back to "name" and adding the missing ' to the HTML code it worked and successfully uploaded the picture.

Upvotes: 1

Luciano Carvalho
Luciano Carvalho

Reputation: 111

Have you verified that the folder that will receive the files has write permissions?

Edit the code to be as follows:

$upload = move_uploaded_file( $_FILES["image_to_upload"]["tmp_name"], $target_file );

if( $upload ) {
  echo "Success!";
} else {
  echo "Error:" . $_FILES["image_to_upload"]["error"];
}

This will return all errors.

Upvotes: 1

Related Questions