1

So I am using a raspberry pi and want to use it as a place where I can upload and download any kind of file with any kind of extension.

Here is my code

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="upload">
</form>
<?php  
if(isset($_FILES['file'])) {
    $file = $_FILES['file'];

    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));

    $allowed = array('txt', 'jpg');

    if(in_array($file_ext, $allowed)) {
        if($file_error === 0) {
            if($file_size <= 1073741824) {

                $file_name_new = uniqid('', true) . '.' . $file_ext;
                $file_destination = './files/' . $file_name_new;

                if(move_uploaded_file($file_tmp, $file_destination)) {
                    echo $file_destination;
                }
            }
        }
    }
}
?>
</body>
</html>

It works great but when the file uploaded i got random file name not the original name How i can keep the original filename uploaded not a random name !

Dr Jay
  • 415
  • 2
  • 14
  • Possible duplicate of [How to upload & Save Files with Desired name](https://stackoverflow.com/questions/3509333/how-to-upload-save-files-with-desired-name) – Progrock May 19 '18 at 10:29

2 Answers2

0
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = './files/' . $file_name_new;

Here above you are defining the new name of the file. Just give the file his name (you have it in the $file_name variable).

//$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_name_new = $file_name;
$file_destination = './files/' . $file_name_new;

at this point since $file_name_new is the same of $file_name you can directly write:

$file_destination = './files/' . $file_name;
DaFois
  • 2,197
  • 8
  • 26
  • 43
  • $file_name_new = $file_name; does this make sense? Instead you just replace $file_name_new with just $file_name – Hetal Chauhan May 19 '18 at 10:43
  • @HetalChauhan It was to make him undestand I know it doesn't make senseand now I'm going to improve the answer... – DaFois May 19 '18 at 11:33
0

In your code,comment this line:

//$file_name_new = uniqid('', true) . '.' . $file_ext;

After then, replace each variable named $file_name_new with only $file_name

Hetal Chauhan
  • 787
  • 10
  • 22