php - 如何使用 MySQL 记录 id 来命名图像?

标签 php mysql

我有一个脚本,可以保存 html5 canvas 中的签名并分配一个随机数作为图像名称,然后将其存储在文件夹中并将名称发布到 MySQL 中。

我已经在 MySQL 表中拥有一条与该图像关联的记录,当编辑该记录以包含该图像时,我想使用现有记录 ID 保存该图像。

所以基本上脚本保存像这样的图像sign_4354533225533.png我想删除数字生成器并将其替换为记录id,这意味着浏览器会将42的记录id传递给signature_pad.php我想采用它id 并用它来命名我的文件,使其看起来像sign_42.png。

这是我的代码:

<?php
$id=$_GET['id'];
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
//$session_id = $_SERVER['REMOTE_ADDR'];
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);

// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);

//echo "unencodedData".$unencodedData;
$sig_name = "sign_" . rand(5,1000) . rand(1, 10) . rand(10000, 150000) . rand(1500, 100000000) . ".png";
//Set the absolute path to your folder (i.e. /usr/home/your-domain/your-folder/
$filepath = "/xampp/htdocs/alpha/development/final/uploads/signatures/" . $sig_name;
$customer_signature=$_POST['customer_signature'];
$fp = fopen("$filepath", 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );

//Connect to a mySQL database and store the user's information so you can link to it later
$link = mysql_connect('localhost','root', 'pwd') OR DIE(mysql_error);
mysql_select_db("customer", $link);
mysql_query("UPDATE signature SET customer_signature=$customer_signature WHERE 'id'= '$id'"); OR DIE(mysql_error());
mysql_close($link);
}
?>
 <?php
session_start()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Signature Pad</title>

<!-- The Signature Pad -->
< script type ="text/javascript"    src="jquery.min.js"></script>
<script type="text/javascript" src="signature-pad.js"></script>
</head>
<body>
<center>
<fieldset style="width: 435px">
    <br/>
    <br/>
    <div id="signaturePad" style="border: 1px solid #ccc; height: 55px; width: 400px;"></div>
    <br/><br/><br/>
    <button id="clearSig" type="button">Clear Signature</button>&nbsp;
    <button id="saveSig" type="button">Save Signature</button>
    <div id="imgData"></div>
    <br/>
    </fieldset>
</center>
<!--<div id="debug"></div>-->
</body>
</html>

最佳答案

进行编辑以适应上述更改

所以你的 $id = $_GET['id']; 只需从数据库中获取行...

 $id=$_GET['id'];
 $customer_signature=$_POST['customer_signature']; 

 $q = mysql_query("SELECT * FROM signature WHERE id ='$id'");
 $row = mysql_fetch_assoc($q);

 //THEN SAVE IMAGE with the fetched row ID ($row['id'])
 $filepath = "/xampp/htdocs/alpha/development/final/uploads/signatures/".$row['id'];
 $fp = fopen("$filepath", 'wb' );
 fwrite( $fp, $unencodedData);
 fclose( $fp );


 //THEN WHEN YOU SAVE IT BACK ITS AN UPDATE
 mysql_query("UPDATE signature SET 'customer_signature'='".$_POST['customer_signature']."', 'sig_name'=$sig_name" WHERE id = ".$id);

但是...警告

您确实不应该使用已弃用的 mysql_* 函数。 相反,谷歌 PDO,学习它,然后使用它:)

关于php - 如何使用 MySQL 记录 id 来命名图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17037744/

相关文章:

php - 如何使用 magento 结构之外的 php 脚本在 Magento 1.7 中添加产品

mysql - 如何在 docker 容器中启动 mysql 服务器

mysql - 我应该添加多个索引吗?

php - 如果ID存在于其他表中,则显示为null

php - 选择随机行,如所见更新列

php - 为dataTable jquery插件获取数组时出现"Illegal string offset"错误

mysql - 替换 SQL 数据库中的短代码

mysql - Left Join 2 tables on 1 table

mysql - 无法连接到 Bitbucket 管道上的 MySQL (111 "Connection refused")

php - fetchall (PDO) 结果集迭代不返回 $key=>$value 的值