【问题标题】:How to add current time stamp into image path如何将当前时间戳添加到图像路径中
【发布时间】:2016-04-19 08:58:46
【问题描述】:

我正在尝试将当前时间戳添加到 php 中的图像路径中,并将图像路径存储到 MySQL 中。我从谷歌搜索并看到insert_time=now()。我尝试编写代码,但我的 php 出现错误。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        if( !empty( $_POST['listItems'] ) ){

            $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
            if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";

            $image = $_POST['image'];

            $listItems = json_decode( $_POST['listItems'], true ); 

            $sql="SELECT id FROM staff_benefit ORDER BY id ASC";

            $id=1;

            $res=$mysqli->query( $sql );
            while( $rs=$res->fetch_object() ) $id=$rs->id;

            $path=NOW()."$id.png";
            $actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";

            $sql="INSERT INTO `staff_benefit` ( `type`, `amount`, `description`, `image`, `ts_id` ) VALUES ( ?, ?, ?, ?, ? )";
            $stmt=$mysqli->prepare( $sql );

            $pathelements=array( realpath( $_SERVER['DOCUMENT_ROOT'] ), 'CRUD', 'PhotoUpload', '' );
            $savepath = realpath( implode( DIRECTORY_SEPARATOR, $pathelements ) ) . "{$id}.png";

            $bytes=file_put_contents( $savepath, base64_decode( $image ) );
            if( !$bytes ){
                echo 'Error saving image';  
            }

            if ( $stmt && $bytes) {
                 foreach( $listItems as $item ){ 

                    $stmt->bind_param('sssss', $item['type'], $item['amount'], $item['description'], $actualpath, $item['ts_id'] );
                    $res=$stmt->execute();

                    if( !$res ) echo 'Query failed with code: '.$stmt->errno;
                } 
            }
            $mysqli->close();
        }
    }
?>

错误

call to undefined function Now()

似乎 Now() 不存在 :( .

【问题讨论】:

标签: php android mysql image


【解决方案1】:

您之前使用的是 Sql 函数。这就是问题所在。根据您的要求尝试此操作。

 $path_parts = pathinfo($_FILES["p_image"]["name"]);
    $image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension']

【讨论】:

    【解决方案2】:

    使用这个

    $path=date("mdYHis")."$id.png";
    

    而不是$path=NOW()."$id.png";

    【讨论】:

    • 011420160726511前面是今天的日期,0726511怎么样?
    • 那是小时、分钟和秒。你可以使用它来确保没有重复值基本上用于精度。如果你愿意,你最多只能使用date('mdY')。那会给你01142016
    • 我正在尝试将 3 个图像路径同时存储到 mysql(通过 arraylist)。但是当我检查时,它有 3 条记录,但 3 张图片来自第一张图片……你知道为什么吗?
    • 因为您的 $actualpath 始终只有一个值,您应该将整个内容放在 while 循环中。
    • 我以为 $id 会自动递增?
    【解决方案3】:

    NOW() 是 SQL 函数,但不是 PHP 函数。您可能需要date 函数。

    【讨论】:

      【解决方案4】:

      修改这一行

       $path=NOW()."$id.png"; 
      

       $path=time()"$id.png";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        相关资源
        最近更新 更多