dongzichan2886 2015-06-11 09:29
浏览 203
已采纳

将时间戳和自定义名称添加到通过PHP上传的文件中

I was looking for a way to add a timestamp and a custom name to a file uploaded to my server via php. I does some readings on $_FILE[] to understand its arguments, but the official PHP docs give no detailed explanation.

This is the code I use to upload, where could I include the timestamp (date('m-d-Y_H:i:s')) and name (ie: "myFile") to create a uniq name for files that are uploaded?

Thanks!

$date = date('m-d-Y_H:i:s');    
$file_path = "uploads/{$_POST['name']}/";

    if (!file_exists("uploads/{$_POST['name']}")) {
        mkdir("uploads/{$_POST['name']}", 0777, true);  
    } else {
        echo 'folder already exists!';
    }

    $file_path = $file_path . basename($_FILES['zipFile']['name']);
    if (move_uploaded_file($_FILES['zipFile']['tmp_name'], $file_path)) {
        echo "success";
    } else {
        echo "fail";
    }
  • 写回答

6条回答 默认 最新

  • dourong8495 2015-06-11 09:43
    关注

    Hope you can get an idea

    $file_path = "uploads/".$_POST['name']."/";   
    $newfile = $_POST['NAME'].date('m-d-Y_H:i:s')'.zip'; //Please use date('m-d-Y_H-i-s') for Linux. 
    $filename = $file_path.$newfile;
    if(!file_exists($filename))
    {
        if(move_uploaded_file($_FILES['zipFile']['tmp_name'],$filename))
        {
            // Other codes
        }
    }
    else
    {
        echo 'file already exists';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制