Php 如何将当前时间戳添加到图像路径中
我试图在php中将当前时间戳添加到图像路径中,并将图像路径存储到MySQL中。我在谷歌上搜索,看到了Php 如何将当前时间戳添加到图像路径中,php,android,mysql,image,Php,Android,Mysql,Image,我试图在php中将当前时间戳添加到图像路径中,并将图像路径存储到MySQL中。我在谷歌上搜索,看到了insert\u time=now()。我试图编写代码,但在php中出错 <?php if( $_SERVER['REQUEST_METHOD']=='POST' ){ if( !empty( $_POST['listItems'] ) ){ $mysqli = new mysqli("127.0.0.1:3307", "root", ""
insert\u 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();
}
}
?>
现在()似乎不存在:(。
Now()
是一个SQL函数,但不是PHP函数。您可能需要该函数。您以前使用过SQL函数。这就是问题所在。请根据您的要求尝试此操作
$path_parts = pathinfo($_FILES["p_image"]["name"]);
$image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension']
用这个
$path=date("mdYHis")."$id.png";
代替$path=NOW().“$id.png”;
修改此行
$path=NOW()."$id.png";
到
可能重复我很好奇为什么这里的人喜欢关闭别人的问题?你可以将我的问题标记为重复,但无需投票关闭问题:)并且提供的链接对其他问题没有帮助:)如果你认真地问,试试——我无法很快找到一个关于为什么的问题,但这个问题涵盖了这一点(以及更多):@tripleee好的,谢谢先生:)
011420160726511
前面是今天的日期,0726511怎么样?那是小时,分和秒。。你可以使用它来确保精度基本上没有重复值。。如果愿意,您只能使用最长date('mdY')
。这将为您提供01142016
我试图同时存储mysql的3个映像路径(通过arraylist)。但当我检查时,它有3条记录,但3个图像来自第一个图像…你知道为什么吗?因为你的$actualpath
始终只有一个值,你应该将整个内容放在while循环中。我想$id会自动递增吗?
$path=NOW()."$id.png";
$path=time()"$id.png";