1

Let's say I have a session variable

$username = $_SESSION['userUid'];

Can I add it as a string to the filename before upload it?

if (!empty($_FILES['uploaded_file'])) {
    $allowed = array("video/mp4", "video/webm", "video/ogg");//Allowed types

    $file_type = $_FILES['uploaded_file']['type'];

    $path = "../videos/";
    $path = $path . basename($_FILES['uploaded_file']['name']);

    if (!in_array($file_type, $allowed)) { 

        header("location:../add-video.php?lesson=" . $lesson . "&alert=wrongtype");
    }

    if (file_exists($path)) {
        header("location:../add-video.php?lesson=" . $lesson . "&alert=alreadyexist");
    } elseif (in_array($file_type, $allowed) && !file_exists($path)) {
        move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path);
        echo "The file " . basename($_FILES['uploaded_file']['name']) .
            " has been uploaded"; // if checks are successful upload the file

So in the videos directory, I will have something like that: uer345video.mp4

fingolfin
  • 37
  • 1
  • 6

1 Answers1

1

before upload no, but when you save it yes:

 $path = "../videos/";
 $path .=  $username.basename($_FILES['uploaded_file']['name']);
  • Thanks. But that doesn't change the filename in the directory. Maybe my question is ambiguous I will edit it. – fingolfin Aug 08 '19 at 22:25
  • it changes the filename after you have moved the file. Are you sure $username exists? you can echo `$path` and `$username` to check –  Aug 08 '19 at 22:28
  • Yes, it exists. Also when I print the $path I get it with the username added, however, the filename in the directory is without the added string. – fingolfin Aug 08 '19 at 22:37
  • you replaced the exiting two line with what i included right, not just added? –  Aug 08 '19 at 22:40
  • Yeah, you are right, sorry. I added it after the move_upload_file, that was very dumb of me. – fingolfin Aug 08 '19 at 22:45