Registration form + Upload.php -> not in the folder

A

Anonymous

Guest
Hi everyone

i am trying to upload a file using a registration.php and an upload.php. The uploaded file cannot be found in the dedicated folder. I created a dedicated folder with the name. I checked my code a hundred times and cannot find a mistake. I checked the upload.php, it seems to be fine as well.

registration.php:

<?php

[...]


return '<form id="signinform" action="upload.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>' . __('Name') . '</label>
<input placeholder="' . __('Your Name') . '" class="form-control" type="text" data-parsley-required="true" data-parsley-error-message="' . __('Enter your name.') . '" name="Name" id="Name">
</div>
<div class="form-group">
<label>Phone</label>
' . $phone_html . '
</div>
<div class="form-group">
<label>File</label>
<input placeholder="Your file" class="form-control-file" type="file" data-parsley-max-file-size="4200" data-parsley-required="true" data-parsley-error-message="' . __('Please upload file.') . '" data-parsley-trigger="change" name="file" id="file">
</div>
<div class="form-group">
<label>Email</label>
<input placeholder="' . __('Your Email') . '" class="form-control" type="email" data-parsley-type="email" data-parsley-required="true" data-parsley-error-message="' . __('Please enter your email.') . '" data-parsley-trigger="change" name="email" id="email">
</div>
<div class="form-group">
<label>password</label>
<input placeholder="' . __('Your Password') . '" class="form-control" type="password" data-parsley-required="true" data-parsley-error-message="' . __('Please enter your password.') . '" name="password">
</div>
<button class="btn btn-theme btn-lg btn-block" type="submit" name="register" id="register">Register</button>
<br />
</form>';

?>



Upload.php:


<?php
if(isset($_POST['register'])){
$file = $_FILES['file'];

$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];

$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

if(in_array($fileActualExt, $allowed))
{
if ($fileError === 0){
if (fileSize < 4000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);


}
else {
echo "File too big!";
}
}
else {
echo "There was as error uploading your file!";
}
}
else{
echo "You cannot upload files of this type!";
}
}

?>

The name of the folder is 'uploads'. I am using a mysql database to store the users and created the folder using the cpanel -> filemanager. :help: :help:
 
Your directory has to have the security permission in order to upload to that directory. I believe it's Chmod 755 to set the permission to the correct level.
 
Thank you for your help.

I had already changed the permission to 0777. For some it doesn't work.
There must be a mistake in the code... But I can't find any...
 
I suspect that the file is in a different directory as you are using a relative filepath:
Code:
$fileDestination = 'uploads/'.$fileNameNew;

You should include the full path to your uploads folder, I normally have a ROOT variable or constant set like this:
Code:
  define('ROOT',$_SERVER['DOCUMENT_ROOT']);
// or
  $root=$_SERVER['DOCUMENT_ROOT'];
Then I can just use:
Code:
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = ROOT . '/uploads/' . $fileNameNew; # note the slash at the start of the directory name
move_uploaded_file($fileTmpName, $fileDestination);
 
Hi there
Thanks for your reply. I added your code to the upload.php and it still doesnt work. Is there anything else that i can try? :help: :help: :help:
 
I only offered example code so that you could see where changes might help.

Did you try looking through your file directory to see if you can find where the files ended up?

Have you enabled error reporting?

If you provide your revised code using the </> code tags and I will have a look.
 
I added your code to the upload.php file. No effect.

Code:
<?php
define('ROOT',$_SERVER['DOCUMENT_ROOT']);

if(isset($_POST['register'])){
$file = $_FILES['file'];

$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];

$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

if(in_array($fileActualExt, $allowed))
{
if ($fileError === 0){
if (fileSize < 4000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = ROOT . '/uploads/' . $fileNameNew; # note the slash at the start of the directory name
move_uploaded_file($fileTmpName, $fileDestination);


}
else {
echo "File too big!";
}
}
else {
echo "There was as error uploading your file!";
}
}
else{
echo "You cannot upload files of this type!";
}
}

?>
 
Did you try looking through your file directory to see if you can find where the files ended up?

Have you enabled error reporting?
 
Hi there, I checked all my folders. I think the file is in some temp folder that I can not access and see. Is there a problem, when the php file gets hanlded by a javascript file at the backend? Could there be some kind of an interference?
 
Client side (browser) JavaScript cannot interfere with PHP.

Do you have error reporting on?
 
I could see the file destination, using console.log() and backend js. Is there a fast way to errorlog php?
 
Back
Top