首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uploadcare在数据库PHP中保存URL

Uploadcare在数据库PHP中保存URL
EN

Stack Overflow用户
提问于 2016-09-21 08:50:54
回答 1查看 972关注 0票数 0

我遇到了一些可能对使用uploadcare.com (或类似的)保存图片以保存用户配置文件的人有用的东西。如果这个问题已经回答了,但我还没有找到,我很抱歉。

这个问题:,我目前正在用Uploadcare.com编写一个脚本。下面是我使用的文档:start/php/

这样做的目的是将上传图片的URL与其他用户数据一起保存在数据库中。

我从

代码语言:javascript
复制
$file->getUrl(); 

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

只有URL和Uploadcare的脚本不会在一起-我没有得到上传图像的URL保存。

脚本:

registration.php:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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(); -->

我是否也搞砸了脚本应该执行的顺序?

EN

回答 1

Stack Overflow用户

发布于 2016-09-22 02:15:57

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

不知道为什么将formphoto.php嵌入到registration.php中,但是我将输入标记直接放在注册表单中,并且做了很少的小修改,这应该是可行的:

registration.php

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

您需要将两个文件放置在web服务器的DOCUMENT_ROOT下,并确保它们都具有正确的访问权限:

代码语言:javascript
复制
sudo chown www-data registration.php photoupload.php 
sudo chmod 700 registration.php photoupload.php
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39611750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档