PHP在上传文件之前将字符串变量添加到文件名的开头

时间:2019-08-08 21:57:57

标签: php file

假设我有一个会话变量

  

$ username = $ _SESSION ['userUid'];

我可以在上传文件之前将其作为字符串添加到文件名吗?

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

因此,在videos目录中,我将看到类似的内容:uer345video.mp4

1 个答案:

答案 0 :(得分:1)

在上传否之前,但是在保存时是:

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