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

Codes here !

Moderators: egami, macek, gesf

Post Reply
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Fri Oct 30, 2020 6:37 am

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:
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 421
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Fri Oct 30, 2020 7:34 am

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.
Life is a fig newton of your imagination! https://www.phototechguru.com/
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Fri Oct 30, 2020 11:24 am

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...
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Sat Oct 31, 2020 1:47 am

Anyone able to help me? I am still stuck.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Sat Oct 31, 2020 12:01 pm

I suspect that the file is in a different directory as you are using a relative filepath:

Code: Select all

$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: Select all

  define('ROOT',$_SERVER['DOCUMENT_ROOT']);
// or
  $root=$_SERVER['DOCUMENT_ROOT'];
Then I can just use:

Code: Select all

$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = ROOT . '/uploads/' . $fileNameNew; # note the slash at the start of the directory name
move_uploaded_file($fileTmpName, $fileDestination);
Last edited by hyper on Mon Nov 02, 2020 1:27 pm, edited 1 time in total.
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Mon Nov 02, 2020 3:54 am

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:
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Mon Nov 02, 2020 1:33 pm

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.
Last edited by hyper on Wed Nov 04, 2020 2:42 pm, edited 1 time in total.
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Tue Nov 03, 2020 1:52 am

I added your code to the upload.php file. No effect.

Code: Select all


<?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!";
}
}

?> 
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Wed Nov 04, 2020 2:42 pm

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

Have you enabled error reporting?
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Sun Nov 08, 2020 3:30 am

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?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Sun Nov 08, 2020 3:37 am

Client side (browser) JavaScript cannot interfere with PHP.

Do you have error reporting on?
Siegfried
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Oct 29, 2020 5:19 am

Mon Nov 09, 2020 1:38 am

I could see the file destination, using console.log() and backend js. Is there a fast way to errorlog php?
Post Reply