简体   繁体   English

如何在PHP中使用filemtime函数?

[英]how to use filemtime function in PHP?

i have a database like picture bellow : 我有一个像波纹管这样的数据库:

database 数据库

i want to insert date and time based on last modified date from a file on my computer in uploaddate column automtically using filemtime() function in php. 我想插入日期,并根据最后修改日期从我的计算机上的文件时uploaddate automtically使用列filemtime()函数在PHP。

i have tried to use this code : 我试图使用此代码:

$namefile= $_FILES['filename']['name']; //from file i have uploaded
if (file_exists($namefile)) 
{
 $uploaddate = date ("Y-m-d H:i:s", filemtime($namefile));
}
echo $uploaddate;

and this is my SQL Query : 这是我的SQL查询:

$import="INSERT into scan (UploadDate, ScanDate, FileName) 
         values('$uploaddate', '$date', '$namefile')
         ON DUPLICATE KEY UPDATE UploadDate='$uploaddate', ScanDate='$date',  
         FileName='$namefile'";

echo function is running and true but i still can't insert into database. echo函数正在运行并且为真,但我仍然无法插入数据库。

May you know where is the problem? 您可能知道问题出在哪里吗? Thank you so much for your help. 非常感谢你的帮助。

The problem is - you are trying to get modified time of name(!) not a path of the file. 问题是-您正在尝试获取name(!)的修改时间,而不是文件的路径。 This sample will work, if you will create and upload_from directory and put it near your form-processor file. 如果您要创建和upload_from目录并将其放在表单处理器文件附近,则此示例将起作用。 You should then upload your files from there. 然后,您应该从那里上传文件。 Or you could specify another one (the path should be absolute). 或者,您可以指定另一个(路径应该是绝对的)。 Unfortunately, this decision will work only in those specific cases. 不幸的是,该决定仅在那些特定情况下有效。

However, the manipulations with modify-time of file - are tricky, because it could be set by users manually, it could come from users with different time-settings, so, you can only believe to your machine with that local decision, that I provided. 但是,使用文件修改时间的操作非常棘手,因为它可以由用户手动设置,它可能来自具有不同时间设置的用户,因此,您只能相信本地决定的机器,提供。

PS: I did not find the ways to access the absolute file name on uploaded file. PS:我找不到在上载文件中访问绝对文件名的方法。

index.php contents: index.php内容:

<form method="POST" action="index.php" enctype="multipart/form-data">
    <input type="file" name="filename">
    <input type="submit">
</form>
<?php
$upload_from_dir = 'upload_from';
if (!empty($_FILES)) {
    $namefile = $upload_from_dir . DIRECTORY_SEPARATOR . $_FILES['filename']['name'];
    if (file_exists($namefile)) {
        $uploaddate = date("Y-m-d H:i:s", filemtime($namefile));
        echo $uploaddate;
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM