views:

527

answers:

2

I have a date issue here. I have a timestamp field in mysql table called filedate. However I have tried several times to declare it and also convert it to a dd-mm-yy format. Unfortunately when I load it the date is still in timestamp format and when I then upload it claims I haven't declared the variable filedate.

Any assistance pointing me in the right direction would be gratefully accepted. All the code is below. I also have a HTML file to show upload and description.

if (isset($_POST['action']) and $_POST['action'] == 'upload')
{
    // Bail out if the file isn't really an upload
    if (!is_uploaded_file($_FILES['upload']['tmp_name']))
    {
     $error = 'There was no file uploaded!';
     include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
     exit();
    }
    $uploadfile = $_FILES['upload']['tmp_name'];
    $uploadname = $_FILES['upload']['name'];
    $uploadtype = $_FILES['upload']['type'];
    $uploaddesc = $_POST['desc'];
    $uploaddata = file_get_contents($uploadfile);
    $filedate = time();


    include 'db.inc.php';

    // Prepare user-submitted values for safe database insert
    $uploadname = mysqli_real_escape_string($link, $uploadname);
    $uploadtype = mysqli_real_escape_string($link, $uploadtype);
    $uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
    $uploaddata = mysqli_real_escape_string($link, $uploaddata);


    $sql = "INSERT INTO filestore SET
      filename = '$uploadname',
      mimetype = '$uploadtype',
      description = '$uploaddesc',
      filedata = '$uploaddata'";
    if (!mysqli_query($link, $sql))
    {
     $error = 'Database error storing file!';
     include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
     exit();
    }

    header('Location: .');
    exit();
}

if (isset($_GET['action']) and
     ($_GET['action'] == 'view' or $_GET['action'] == 'download') and
     isset($_GET['id']))
{
    include 'db.inc.php';

    $id = mysqli_real_escape_string($link, $_GET['id']);

    $sql = "SELECT filename, mimetype, filedata, DATE_FORMAT(STR_TO_DATE( filedate, '%d-%b-%Y'), '%Y.%m.%d')
      FROM filestore 
      WHERE id = '$id'";
    $result = mysqli_query($link, $sql);
    if (!$result)
    {
     $error = 'Database error fetching requested file.';
     include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
     exit();
    }

    $file = mysqli_fetch_array($result);
    if (!$file)
    {
     $error = 'File with specified ID not found in the database!';
     include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
     exit();
    }

    $filename = $file['filename'];
    $mimetype = $file['mimetype'];
    $filedata = $file['filedata'];
    $disposition = 'inline';

    if ($_GET['action'] == 'download')
    {
     $mimetype = 'application/octet-stream';
     $disposition = 'attachment';
    }

    // Content-type must come before Content-disposition
    header("Content-type: $mimetype");
    header("Content-disposition: $disposition; filename=$filename");
    header('Content-length: ' . strlen($filedata));
    echo $filedata;
    exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'delete' and
     isset($_POST['id']))
{
    include 'db.inc.php';

    $id = mysqli_real_escape_string($link, $_POST['id']);

    $sql = "DELETE FROM filestore
      WHERE id = '$id'";
    if (!mysqli_query($link, $sql))
    {
     $error = 'Database error deleting requested file.';
     include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
     exit();
    }

    header('Location: .');
    exit();
}

include 'db.inc.php';

$sql = 'SELECT id, filename, mimetype, description, filedate
     FROM filestore';
$result = mysqli_query($link, $sql);
if (!$result)
{
    $error = 'Database error fetching stored files.';
    include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
    exit();
}

$files = array();
while ($row = mysqli_fetch_array($result))
{
    $files[] = array(
      'id' => $row['id'],
      'filename' => $row['filename'],
      'mimetype' => $row['mimetype'],
      'description' => $row['description'],
      'filedate' => $row['filedate']);
}

include 'files3.html.php';
?>
A: 

you have to use UNIX_TIMESTAMP() in your select statement

knittl
Thanks, I don't seem to be successful formatting using this code as there is a line in the html file include <?php htmlout($f['filedate']); ?> so trying to get this to format the date correctly as 04 Aug 2009 Thanks everyone for the input
Ddywalgi
+2  A: 

Use NOW() when inserting. When formatting, I don't think you need to parse the TIMESTAMP typed column, as DATE_FORMAT should be able to work with timestamps directly.

This works:

SELECT DATE_FORMAT( CURRENT_TIMESTAMP( ) , '%Y.%m.%d' )
nikc
as current_timestamp
SeanJA
`CURRENT_TIMESTAMP` is a function, not a datatype. `TIMESTAMP` is a datatype.
nikc