0

EDIT

I have this PHP code now:

$name = isset($_POST['image_file']) ? $_POST['image_file'] : '';
            $date_added = date ("F d Y H:i:s.", filectime(basename($_FILES["image_file"]["tmp_name"])));
            $path = "../uploads/".basename($_FILES["image_file"]["name"]);

            $patient_id = $_POST['patient_id'];
            $remark = $_POST['remark'];
            //$date_added = $_POST['date_added'];


            $ext = pathinfo($path, PATHINFO_EXTENSION);
...

And the result of any file (except images) is: January 01 1970 01:00:00.

And when I try to upload an image it send me to an empty page where no errors are shown and the image isn't uploaded into folder.

END EDIT

I need to add scanned images into patient file using this form:

<form enctype="multipart/form-data" id="myForm" name="myForm" action="add_scan.php" method="post">
            <div class="box-body" id="toggleDiv">
              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <label style="float:left">File Description</label>
                    <input type="text" class="form-control" id="remark" name="remark"/>                        
                    <label style="float:left">Upload File</label>
                    <input type="file" class="form-control" id="image_file" name="image_file"/>
                    <input type="hidden" class="form-control" id="patient_id" name="patient_id" value="<?php echo $patient_id ?>"/>
                  </div><!-- /.form-group -->
   <button type="submit" class="btn btn-warning" id="add_scan" name="add_scan">Add File</button>
</form>

Usually, my client add a date but sometimes he forgot in what date the image is taken. So I need to access the system date of the image.

I tried the following: Add_scan.php page:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

require_once('../include/global.php');
//session_start();
$user = $_SESSION['username'];
$id_logged = $_SESSION['login_id'];

if(isset($_POST['add_scan']))
    {
    try
    {
        $name = isset($_POST['image_file']) ? $_POST['image_file'] : '';
        $path = "../uploads/".basename($_FILES["image_file"]["name"]);
        $date_added = date ("Y-m-d", filectime(basename($_FILES["image_file"]["name"])));
        $patient_id = $_POST['patient_id'];
        $remark = $_POST['remark'];
        //$date_added = $_POST['date_added'];


        $ext = pathinfo($path, PATHINFO_EXTENSION);
        move_uploaded_file($_FILES["image_file"]["tmp_name"], $path.$name);

        $sqlUpd = "INSERT INTO scan_image(id_logged, patient_id, image_file, remark, date_added)
                   VALUES(:id_logged, :patient_id, :image_file, :remark, :date_added)";
        $stmt = $conn->prepare($sqlUpd);
        $stmt->bindValue(':id_logged', $id_logged);
        $stmt->bindValue(':patient_id', $patient_id);
        $stmt->bindValue(':image_file', $path);
        $stmt->bindValue(':remark', $remark);
        $stmt->bindValue(':date_added', $date_added);
        $stmt->execute();

        header("Location: patients.php?patient=".$patient_id);
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
?>

Where I used this line: $date_added = date ("Y-m-d", basename($_FILES["image_file"]["name"])); to access the date according to PHP PDO documentations in this link.

But nothing added and I only see a blank page of my add_scan.php code and it is not redirected to patient.php page.

Martin
  • 19,815
  • 6
  • 53
  • 104
androidnation
  • 556
  • 1
  • 4
  • 17

2 Answers2

1

filemtime reports the files modification time, not its creation time (this is not available on most operating systems).

That's not the PDO documentation.

The file uploaded is a copy of the data held in the original file - so the mtime you see on the copy will always be about 'now'.

The user can see the modification time on their local machine. If they can't provide this information then the only option would be to take a backup of the files, restore it on your server (using an appropriate method which does not change the mtime, e.g. untar as root) then load the files directly from there rather than uploading over the web.

Martin
  • 19,815
  • 6
  • 53
  • 104
symcbean
  • 45,607
  • 5
  • 49
  • 83
1

SOLUTION:

Because you're uploading a file the date on the system is going to be the upload date. Your solution if it is an image file is to read the image file meta-data, which is a bit of a pain because there are multiple types of meta data for each type of image file and finding the one you want is not in itself efficient.

I think Imagick PHP plugin does this but I can't vouch for the quality or worth of this/these functions.

To clarify further:

When you upload a file from a computer to a server, the server will hold NO filesystem information about the file on the computer.

Meta Data is data about something, and typically data about a file (such as its created date) is not stored within the file itself as a standard, but in the operatng system that stores the file contents.

The server which recieves your uploaded file can not tell you when the file was saved on to the computer it came from, or anything else filesystem related because the server has only been given the file contents data, not the file systems metadata about storage and modification (etc).

If this information is available from within a file, it is stored in what is called "MetaData" inside the file itself, and to reach these bits of metadata you need to use something like Imagick for images .


Alternatively, if you simply want the date the file was added to this system you can read that in this Stack Answer.

NOTES:

  • You want filectime.
  • After you header add an exit to cease execution.
  • You have a $_POST and a $_FILES value with the same name, you are using $_POST['upload_file'] and $_FILES['upload_file'] I'm not sure if this will break your script but the POST values of this will be empty unless you have another field in your form with the same name which is clearly bad practise.
  • Your require and/or include do not need to be in brackets.
  • It is also bad practise for pathinfo to be given a relative path, you should as much as possible give PHP functions absolute paths, using $_SERVER['DOCUMENT_ROOT'] or other magical constants.
  • Remove basename in filectime, it's unneed.
  • it looks like $path.$name should infact be $path.$name.$ext when using move_uploaded_file

Your problem (from your comment) is that you are looking for the time of a string that is not the file. Replace $_FILES["image_file"]["name"] in you filectime call to instead be ['tmp_name'] because this is the location address of where the uploaded file is (temporarily) stored.

The name array value in $_FILES simply tells you the name of the file from the place it was uploaded from.

BUT This time will simply only tell you the uploaded time.

  • Your Error log should have shown you that your filectime is tying to get the data from a non-file entity.
Community
  • 1
  • 1
Martin
  • 19,815
  • 6
  • 53
  • 104