【问题标题】:Php add string variable to the begining of file name before upload it [duplicate]php在上传之前将字符串变量添加到文件名的开头[重复]
【发布时间】:2019-12-16 16:02:36
【问题描述】:

假设我有一个会话变量

$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

所以在视频目录中,我会有这样的内容:uer345video.mp4

【问题讨论】:

    标签: php file


    【解决方案1】:

    上传前没有,但保存后是的:

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

    【讨论】:

    • 谢谢。但这不会更改目录中的文件名。也许我的问题模棱两可,我会编辑它。
    • 它会在您移动文件后更改文件名。你确定 $username 存在吗?你可以回显$path和$username查看
    • 是的,它存在。同样,当我打印 $path 时,我添加了用户名,但是目录中的文件名没有添加的字符串。
    • 你用我包含的内容替换了现有的两行,而不是刚刚添加的内容?
    • 是的,你是对的,对不起。我在 move_upload_file 之后添加了它,这对我来说非常愚蠢。
    猜你喜欢
    • 2018-12-07
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2016-01-26
    相关资源
    最近更新 更多