Uploadcare在数据库PHP中保存URL

Uploadcare在数据库PHP中保存URL

本文介绍了Uploadcare在数据库PHP中保存URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些可能对使用uploadcare.com(或类似)为用户配置文件保存图片的人有用的内容。



问题:我目前正在处理脚本, Uploadcare.com。以下是我使用的文档:



这个想法是将上传图片的网址与其他用户数据一起保存在数据库中。



I从

获取URL

  $ file-> getUrl(); 

在本地脚本上,我也可以保存数据库中用户的其他内容。



只有Uploadcare的网址和脚本不会共存 - 我没有保存上传图片的网址。



脚本:

registration.php:

 < form class =form-signin-register wow fadeInUpname =signupformid =signupformonsubmit =return false; method =POSTaction =photoupload.php> 
< h2 class =form-signin-heading>立即注册< / h2>
< div class =login-wrap>
< p>输入个人资料< / p>

< input id =avatarname =avatartype =textclass =hiddenvalue =<?php echo $ url;?>>

<?php include('formphoto.php'); ?>

< input id =firstNametype =textclass =form-controlplaceholder =First Nameautofocus>
< input id =lastNametype =textclass =form-controlplaceholder =Last Name>
< input id =emailonfocus =emptyElement('status')onblur =checkemail()onkeyup =restrict('email')maxlength =88type =textclass =form-controlplaceholder =Email>< span id =emailstatus>< / span>
< option value =>选择性别< / option>
< option value =m>男性< / option>
< option value =f>女性< / option>
< / select> .....< button id =signupbtnonclick =signup(); class =btn btn-lg btn-login btn-blockdisabled>创建帐户< / button>< / form>

formphoto.php:

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

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

?>


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

< script>
//将其设置为真实时!
UPLOADCARE_LIVE = false;
UPLOADCARE_IMAGES_ONLY = true;
//这里是定义的自由裁剪
UPLOADCARE_CROP ='1:1';
< / script>

< form method =POSTaction =photoupload.php>

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

<! - < input type =submitvalue =保存此个人资料图片! /> - >


< / form> b


$ b $ p $ > <?php
require_once'vendor / autoload.php';
使用\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;
?>




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






我也可能弄乱脚本的顺序应该执行?

解决方案

Michael,首先 - 我编辑了您的问题以删除密钥 - 您作为Uploadcare的第二个参数传递的密钥\Api() - 它不应该被任何人在公共场合看到。



不知道为什么你在registration.php中嵌入formphoto.php,但我放入了输入直接以注册形式标记,并做了一些小的更正,这应该工作:

registration.php

 < HTML> 
< head>
< script>
//将其设置为真实时!
UPLOADCARE_LIVE = false;
UPLOADCARE_IMAGES_ONLY = true;
//这里是定义的自由裁剪
UPLOADCARE_CROP ='1:1';
< / script>

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

< body>
< form class =form-signin-register wow fadeInUpname =signupformid =signupformmethod =POSTaction =photoupload.php>
< h2 class =form-signin-heading>立即注册< / h2>
< div class =login-wrap>
< p>输入个人资料< / p>


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

< input name =firstNameid =firstNametype =textclass =form-controlplaceholder =First Nameautofocus>
< input name =lastNameid =lastNametype =textclass =form-controlplaceholder =Last Name>
< input name =emailid =emailonfocus =emptyElement('status')onblur =checkemail()onkeyup =restrict('email')maxlength =88type =textclass =form-controlplaceholder =Email>< span id =emailstatus>< / span>
< option value =>选择性别< / option>
< option value =m>男性< / option>
< option value =f>女性< / option>
< / select> .....
< button id =signupbtnclass =btn btn-lg btn-login btn-block>创建帐户< / 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>

您需要将这两个文件放在您网络服务器的DOCUMENT_ROOT下,并确保它具有正确的访问权限对于这两个:

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


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?

解决方案

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

这篇关于Uploadcare在数据库PHP中保存URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:55