0

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.

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 :

$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.

May you know where is the problem? Thank you so much for your help.

Shafira
  • 3
  • 4
  • 1
    Display: your actual query (variables substituted), function call that actually query the dbms and function call that gets the last error from dbms. Your question is not detailed enough to analyze for problems. – LeleDumbo Apr 12 '16 at 07:19
  • Possibly removing the `.` in your datetime could help – scrowler Apr 12 '16 at 07:19
  • Can you also show the code that does the actual insertion? – apokryfos Apr 12 '16 at 09:23

1 Answers1

0

The problem is - you are trying to get modified time of name(!) not a path of the file. This sample will work, if you will create and upload_from directory and put it near your form-processor file. 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.

index.php contents:

<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;
    }
}
?>
aderushev
  • 818
  • 8
  • 11
  • Could [move the uploaded file](http://php.net/manual/en/function.move-uploaded-file.php) first. – apokryfos Apr 12 '16 at 09:27
  • @apokryfos we are not going to check the uploaded file, because the modify time will be changed for the uploaded one and will be equals to `now` It is useless in this case.. – aderushev Apr 12 '16 at 09:30