使用php curl将文件上传到无后端文件存储中

时间:2019-06-20 10:04:55

标签: php rest backendless

我试图通过php表单将文件(可以是图像或文档)上传到“无尽文件”。

我已经以“ Uploads”的名称在无源文件(根)中创建了一个新文件夹,我希望将所有上传的文件都保存在那里。

我有一个基本代码,无法通过该代码上传文件,我需要一些帮助。

无后端版本v5.2.14

客户端SDK(REST)

应用程序ID:30BB5E5E-6AE8-53E0-FFE4-87303ACE0300

<?php
    //get the incident id from the url and if its missing throw an error    
    if( !isset($_GET['incident']) && !isset($_POST['incident']) )
        {
            echo "<script> alert('Incident id is missing.'); window.location.href = '#'; </script>";  
        }
    //set the incident id to $id            
    if( isset($_GET['incident']) )
        {
                // $id = hexdec($_GET['incident']) ^ 42; 
            $ID = $_GET['incident'];  //to test form without xor 
        }   

    // //Check if incident id is present in the Incident table          
    //   $user_info = $wpdb->get_row("SELECT * FROM Incident WHERE IncidentID = {$id} ");

    //retrieve the data from the backendless table and check if the incident id is present in it
    $service_url1 = 'https://api.backendless.com/<app-key>/<rest-api-key>/data/Incident';
    $curl1 = curl_init($service_url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
    $curl_response1 = curl_exec($curl1);
    if ($curl_response1 === false) {
        $info1 = curl_getinfo($curl1);
        curl_close($curl1);
        die('error occured during curl exec. Additioanl info: ' . var_export($info1));
    }
    curl_close($curl1);
    //getting the array which is stored in $curl_response1, putting it in decoded and pulling out only the incident id field and arranging it properly.
    $decoded = json_decode($curl_response1);
    $res1 = array_column($decoded, 'IncidentID');
    $res2 = implode("', '", $res1);

    // //displaying the results for learning purpose 
    //      echo "<br>res2 = ".$res2;
    //      echo "<br>ID  = ".$ID ; 
    //      echo "<br>res1 = ".$res1." : ";
    //      print_r($res1); 

    //checking if the incident id is present in the array(res1)
    if (!in_array($ID, $res1)) 
        {
            echo "<script> alert('Incident id not found.'); window.location.href = '#'; </script>"; 
        }       


$errmsg = '';
if (isset($_POST['Submit']))
{
    $filename = $_FILES['uploadoc']['name'];
    $filedata = $_FILES['uploadoc']['tmp_name'];
    $filesize = $_FILES['uploadoc']['size'];
    $filetype = $_FILES['uploadoc']['type'];

    $url = "https://api.backendless.com/<app-key>/<rest-api-key>/files/Uploads/".$filename;// e.g. http://localhost/myuploader/upload.php // request URL

    if ($filedata != '')
    {
        $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
        $postfields = array("filedata" => $filedata, "filename" => $filename);
        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => true,
            CURLOPT_POST => 1,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postfields,
            CURLOPT_INFILESIZE => $filesize,
            CURLOPT_RETURNTRANSFER => true
        ); // cURL options
        curl_setopt_array($ch, $options);
        curl_exec($ch);
        if(!curl_errno($ch))
        {
            $info = curl_getinfo($ch);
            if ($info['http_code'] == 200)
                $errmsg = "File uploaded successfully";
        }
        else
        {
            $errmsg = curl_error($ch);
        }
        curl_close($ch);
    }
    else
    {
        $errmsg = "Please select the file";
    }
}

?>

<html>
<head>
    <style>
        .upload-doc-submit{margin-top: 20px;}
    </style>
</head>
<body style="text-align:left;" onload="StartTimers();" onclick="ResetTimers();">
<div>
    <form method="post" id="upload-doc" onsubmit="return validate();" enctype="multipart/form-data">

        <table>
            <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap uploadoc">
                        <td>
                            <label>Upload Document : </label>
                            <input type="file" name="uploadoc" class="wpcf7-form-control wpcf7-file wpcf7-validates-as-required" accept=".docx,.jpg,.doc" aria-required="true" aria-invalid="false" required="">
                        </td>
                    </span>
                </div>  
            </tr>   

            <tr>
                <div class="col-sm-12 col-xs-12">
                        <td align="center" valign="middle">
                            <input type="hidden" name="incident" value="$id">
                            <input  type="submit" id="btnSubmit"  value="Submit" name="Submit" class="wpcf7-form-control wpcf7-submit upload-doc-submit center">
                        </td>
                    </span>
                </div>  
            </tr>
        </table> 

    </form>
</div>
</body>
</html>

<script>
    jQuery(document).ready(  function()
    {
        jQuery("#userregistration").validate
            ({
                ignore: ":hidden",
                rules: 
                    {
                        uploadoc: 
                            {
                                required : true
                            }       

                    },
                messages: 
                    {
                        uploadoc: 
                            {   
                                required : "Please choose a file." 
                            }       
                    },
            });

    });
</script>

<?php

if (isset($_POST['Submit']))
    {
        echo " <br />  <br />  <div style='text-align:center'>or</div>";
        echo '<br />  <br />  <th><strong><u><center><a target="_blank" href="javascript:window.close();">Click here to return.</a></center></u></th>';
    }   

?>

预期的行为: 1.我希望表格显示一个窗口,我必须将要保存的文件上传到无尽文件中。 (完成)

2。我希望以上传文件的人的名义创建一个新文件夹,该文件夹将位于“上传”文件夹中。

3。新文件应该放在该新文件夹中。

实际行为: 现在,它不会出现任何错误,也不会上传文件,就像是无效表格一样。

0 个答案:

没有答案