phpsignature

How to append unique php variable to upload of signature


I have a working signature form which uploads the resulting jpeg file to server. Within the PHP processing there is also a unique ID which I would like to append to the filename so that it can be unique as currently its uploading with a generic name as you can see below, how would I go about this i.e. so that the $id is used as the filename reference?

<?php
$id=$_GET['id'];
$signature = $_POST['signature'];
$signature = str_replace('data:image/png;base64,', '', $signature);
$signature = str_replace(' ', '+', $signature);



$file = "uploads/uploaded_signature.png";


$success = file_put_contents($file, base64_decode($signature));

print $success ? $file : 'problem saving.';


?>

Solution

  • Signatures can be classed as sensitive information, so storing them in the upload directory may not be an appropriate solution.

    As for setting the filename of those images, why not just have the filename just be the ID?

    // At least store your signature files outside the web root.
    $dir = dirname(ABSPATH) . '/uploaded_signature/';
    if (!file_exists($dir)) {
        mkdir($dir, 0700, true);
    }
    
    // Generate the file name.
    $name = $id . ".png"; // You mentioned JPEG in your original question, but you show 'image/png' in your code example.
    $file = $dir . $name;
    

    This image will then be saved to ../uploaded_signature/{$ID}.png.