Reputation: 311
I created a registration form. Also, i want to add a profile picture. However, my codes gives an error.
Warning: Undefined array key "fileupload" in C:\xampp\htdocs\db\register.php on line 29
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\db\register.php on line 29
Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\db\register.php:30 Stack trace: #0 C:\xampp\htdocs\db\register.php(30): file_get_contents('') #1 {main} thrown in C:\xampp\htdocs\db\register.php on line 30
Line29 = $fileName = $_FILES['fileupload']['name']; Line30 = $picture = file_get_contents($fileName);
How can I fix that?
<?php $fileName = $_FILES['fileupload']['name'];
$picture = file_get_contents($fileName);
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" >
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">First Name</label>
<textarea class="form-control" name="fName" rows="1" required></textarea>
<span class="error"> <?php echo $fNameErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Last Name</label>
<textarea class="form-control" name="lName" rows="1"></textarea>
<span class="error"> <?php echo $lNameErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" name="email" aria-describedby="emailHelp">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
<span class="error"> <?php echo $emailErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
<span class="error"> <?php echo $passwordErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Claim Password</label>
<input type="password" class="form-control" name="claimPassword">
<span class="error"> <?php echo $claimPasswordErr; ?></span>
<span class="error"> <?php echo $differentPasswordErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Phone Number</label>
<textarea class="form-control" value="1-(555)-555-5555" name="telephone" rows="1"></textarea>
<span class="error"> <?php echo $telephoneErr; ?></span>
<span class="error"> <?php echo $telephoneErr2; ?></span>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Department</label>
<textarea class="form-control" name="department" rows="1"></textarea>
<span class="error"> <?php echo $departmentErr; ?></span>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Institution</label>
<textarea class="form-control" name="institution" rows="1"></textarea>
<span class="error"> <?php echo $instErr; ?></span>
</div>
<div class="mb-3">
<label for="formFile" class="form-label">Pick a profile picture</label><br>
<input name="fileupload" type="file" class="form-control-file" id="exampleFormControlFile1" required><br><br>
<span class="error"> <?php echo $pictureErr; ?></span>
</div>
<div class="exampleFormControlTextarea1">
<label for="example-date-input" class="form-label">Date of birth</label>
<span class="error"> <?php echo $birthErr; ?></span>
<div class="col-10">
<input class="form-control" type="date" value="2021-01-01" name="birth">
</div>
</div>
<br>
<label for="exampleFormControlTextarea1" class="form-label">Gender</label>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" name="gender">
<label class="form-check-label" for="flexRadioDefault1">
Female
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" name="gender2" >
<label class="form-check-label" for="flexRadioDefault2">
Male
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" name="gender3" >
<label class="form-check-label" for="flexRadioDefault2">
I do not want to say
</label>
<span class="error"> <?php echo $genderErr; ?></span>
</div>
</div>
<br>
<button type="submit" name= "submit" class="btn btn-primary">Sign Up</button>
<br><br>
<a href="login.php">Do you have an account? Click here to sign in.</a>
</form>
</div>
</div>
Upvotes: 0
Views: 4756
Reputation: 23664
First issue is trying to access an uploaded file before the form submit happens. The first lines of your file could be changed to
<?php
if (isset($_FILES['fileupload'])) {
$fileName = $_FILES['fileupload']['tmp_name'];
$picture = file_get_contents($fileName);
}
?>
But you'll still get an error because you're referencing the name
property of $_FILES['fileupload']
- which will give you the actual filename, not the path to the file.
Use tmp_name
which gives you a full system path to the file. In short, access your uploaded file like this:
$picture = file_get_contents($_FILES['fileupload']['tmp_name']);
Here is what the $_FILES` object looks like (which will be different values for you depending on the type of file etc:
[fileupload] => Array
(
[name] => MyFile.txt (comes from the browser, so treat as tainted)
[type] => text/plain
[tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control)
[error] => UPLOAD_ERR_OK (= 0)
[size] => 123 (the size in bytes)
)
Upvotes: 1