Uploadcare save URL in database PHP

2019-09-13 08:57发布

问题:

I have encountered something that might be useful to anyone using uploadcare.com (or similar) to save pictures for user profiles. Sorry in advance if the question was answered and I haven't found it.

The question: I'm currently working on a script with Uploadcare.com. Here is the documentation I work with: https://uploadcare.com/quick_start/php/

The idea is to save the URL of the uploaded picture together with the other user data in a database.

I get the URL from

$file->getUrl(); 

on a local script and I'm able to also save everything else from the user in the database.

Just the URL and the script for Uploadcare won't woork together - I don't get the URL of the uploaded image saved.

Scripts:

registration.php:

<form class="form-signin-register wow fadeInUp" name="signupform" id="signupform" onsubmit="return false;" method="POST" action="photoupload.php">
        <h2 class="form-signin-heading">Register now</h2>
        <div class="login-wrap">
            <p>Enter personal details</p>

            <input id="avatar" name="avatar" type="text" class="hidden" value="<?php echo $url; ?>">

            <!-- M: The 'Choose a File' button. This also loads the widget -->              
            <?php include('formphoto.php'); ?>              

            <input id="firstName" type="text" class="form-control" placeholder="First Name" autofocus>
            <input id="lastName" type="text" class="form-control" placeholder="Last Name">
            <input id="email" onfocus="emptyElement('status')" onblur="checkemail()" onkeyup="restrict('email')" maxlength="88" type="text" class="form-control" placeholder="Email"><span id="emailstatus"></span>
            <select id="gender" onfocus="emptyElement('status')" class="form-control">
                <option value="">Select Gender</option>
                <option value="m">Male</option>
                <option value="f">Female</option>
            </select> ..... <button id="signupbtn" onclick="signup();" class="btn btn-lg btn-login btn-block" disabled>Create Account</button></form>

formphoto.php:

<?php require_once 'vendor/autoload.php';
use \Uploadcare;

$api = new Uploadcare\Api('ab11954d8908bc4b0e35', 'secretkey_removed');

?>


<?php echo $api->widget->getScriptTag(); ?>

<script>
    //set this to true when live!
    UPLOADCARE_LIVE = false;
    UPLOADCARE_IMAGES_ONLY = true;
    //here is free croping defined
    UPLOADCARE_CROP = '1:1';    
</script>

<form method="POST" action="photoupload.php">

    <?php echo $api->widget->getInputTag('qs-file'); ?>

    <!-- don't need the following line, it saves also without to uploadcare :) -->
    <!-- <input type="submit" value="Save this profile picture!" /> -->


</form>

photoupload.php:

<?php
require_once 'vendor/autoload.php';
use \Uploadcare;

$file_id = $_POST['qs-file'];
$api = new Uploadcare\Api('ab11954d8908bc4b0e35', 'secretkey_removed');

$file = $api->getFile($file_id);
$file->store();

$url = $file->getUrl(); 

header registration.php; 
?>



<!-- M: for saving the avatar picture, a hidden field. The value is the URL     of pic in Uploadcare.com -->

<!-- $url = $file->getUrl(); -->

Do I maybe also mess up the order the scripts should be executed?

回答1:

Michael, first - I have edited your question to remove the secret key - one that you passed as the second argument to Uploadcare\Api() - it is not supposed to be seen by anyone in public.

Not sure why you embedded formphoto.php in registration.php, but I placed input tag directly in registration form and did few minor corrections, this should work:

registration.php

<html>
<head>
<script>
    //set this to true when live!
    UPLOADCARE_LIVE = false;
    UPLOADCARE_IMAGES_ONLY = true;
    //here is free croping defined
    UPLOADCARE_CROP = '1:1';    
</script>

 <?php
require_once 'vendor/autoload.php';
use \Uploadcare;
$api = new Uploadcare\Api('ab11954d8908bc4b0e35', 'YOUR_SECRET_KEY');
echo $api->widget->getScriptTag();
?>
<head>

<body>
<form class="form-signin-register wow fadeInUp" name="signupform" id="signupform"  method="POST" action="photoupload.php">
        <h2 class="form-signin-heading">Register now</h2>
        <div class="login-wrap">
            <p>Enter personal details</p>


            <!-- M: The 'Choose a File' button. This also loads the widget -->              
            <?php
echo $api->widget->getInputTag('qs-file');
?>          

            <input name="firstName" id="firstName" type="text" class="form-control" placeholder="First Name" autofocus>
            <input name="lastName" id="lastName" type="text" class="form-control" placeholder="Last Name">
            <input name="email" id="email" onfocus="emptyElement('status')" onblur="checkemail()" onkeyup="restrict('email')" maxlength="88" type="text" class="form-control" placeholder="Email"><span id="emailstatus"></span>
            <select name="gender" id="gender" onfocus="emptyElement('status')" class="form-control">
                <option value="">Select Gender</option>
                <option value="m">Male</option>
                <option value="f">Female</option>
            </select> ..... 
            <button id="signupbtn"  class="btn btn-lg btn-login btn-block">Create Account </button>
</form>
<body>
</html>

photoupload.php

<html>
<head>
<?php
require_once 'vendor/autoload.php';

useUploadcare;
$file_id = $_POST['qs-file'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$api = new UploadcareApi('ab11954d8908bc4b0e35', 'YOUR_SECRET_KEY');
$file = $api->getFile($file_id);
$file->store();
?>

</head>

<body>
<?php
echo $firstName, ' ', $lastName, ' ', $email, ' ', $gender, ' ', $file->getUrl(); ?>
<br />

</body>

You need to place both files under DOCUMENT_ROOT of your web-server and make sure it has correct access rights for both:

sudo chown www-data registration.php photoupload.php 
sudo chmod 700 registration.php photoupload.php