将上传的文件发布到另一个 PHP 页面?

发布于 2024-08-14 13:01:07 字数 1262 浏览 5 评论 0原文

这是我的问题。我有一个不太“用户友好”的票务跟踪系统,但我希望我的用户能够在不让他们看到的情况下向系统提交“票证”。

我只使用自定义 HTTP 表单并将其发布到票证跟踪系统。一个问题是“成功/完成”页面容易让人感到困惑。所以我想......好吧,因为我无法更改票证系统以使用不同的“成功”页面。我将仅使用 CURL 处理 HTTP Post 交换并报告自定义成功或问题页面。这是一些抽象代码。

文件:tickethelper.php

<?php
extract($_POST);
$url = 'TICKETSYSTEMURL';
$fields = array(
        'fullname'=>urlencode($fullname),
        /*many more fields*/
        );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch); 
curl_close($ch);
if((strlen(strstr($contents,'Your ticket has been submitted')))>0){
        header("Location: http://THANKYOU");
}
        else{
                header("Location: http://OOPS");
        }

?>

但是,我意识到我缺少文件上传。我见过的大多数 CURL 示例都与将本地文件上传到远程 HTTP POST 页面有关。

如何处理从用户 HTTP 表单接收文件、在“tickethelper”中处理该文件并将其发布到“TICKETSYSTEMURL”?

-以色列

Here is my problem. I have a ticket-tracking system that is not very 'user friendly' but I have a desire for my users to submit 'tickets' to the system without having them to see it.

I got as far as just using a custom HTTP Form and posting to the ticket tracking system. One problem is the 'success/completion' page has a tendency to confuse people. So I figured... well since I cant change the ticket system to use a different 'success' page. I will just handle the HTTP Post exchange with CURL and report a custom success or problem page. Heres some abstracted code.

File: tickethelper.php

<?php
extract($_POST);
$url = 'TICKETSYSTEMURL';
$fields = array(
        'fullname'=>urlencode($fullname),
        /*many more fields*/
        );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch); 
curl_close($ch);
if((strlen(strstr($contents,'Your ticket has been submitted')))>0){
        header("Location: http://THANKYOU");
}
        else{
                header("Location: http://OOPS");
        }

?>

However, what I realized is that I am missing my file upload. Most of the CURL examples I have seen are to do with uploading local files to a remote HTTP POST page.

How do I handle receiving a file from my users HTTP form, process this in the 'tickethelper' and POST that to the 'TICKETSYSTEMURL' ?

-Israel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2024-08-21 13:01:07

好吧,我不能说我自己做到了这一点,但根据有关 CURL 和文件处理的 PHP 文档 (PHP.net)

CURLOPT_INFILE |上传时应读取传输的文件。

对于选项参数的以下值,值应该是流资源(例如使用 fopen()):

您应该能够对已上传的文件调用 fopen()(请参阅有关处理文件上传的文档)我不确定,但您可能需要在调用 fopen() 之前先调用 move_uploaded_file() 到服务器上的受控目录,

例如:

$ch = curl_init();

$file = fopen($_FILES['file-field-name']['tmp_name'], 'r');

// OTHER CURL CONFIGURATION
curl_setopt($ch, CURLOPT_INFILE, $file);

curl_exec($ch);

Well, I can't say I've done this myself, but according to the PHP documentation regarding CURL and File Handling (PHP.net)

CURLOPT_INFILE | The file that the transfer should be read from when uploading.

value should be a stream resource (using fopen(), for example) for the following values of the option parameter:

You should be able to call fopen() on the file that has been uploaded (refer to documentation regarding Handling File Uploads) I'm not sure, but you may need to call move_uploaded_file() to a controlled directory on the server before calling fopen()

For example:

$ch = curl_init();

$file = fopen($_FILES['file-field-name']['tmp_name'], 'r');

// OTHER CURL CONFIGURATION
curl_setopt($ch, CURLOPT_INFILE, $file);

curl_exec($ch);
浮世清欢 2024-08-21 13:01:07

首先,您的脚本可以通过 $_FILES 数组访问上传的文件。

来自 http://www.tizag.com/phpT/fileupload.php

$_FILES 数组是 PHP 存储的位置
有关文件的所有信息。那里
是这个数组的两个元素
需要了解这一点
示例。

  • uploadedfile - uploadedfile 是我们在 HTML 中指定的引用
    形式。我们需要这个来告诉
    $_FILES 数组我们想要哪个文件
    玩弄。
  • $_FILES['uploadedfile']['name'] - name 包含用户上传文件的原始路径。
  • $_FILES['uploadedfile']['tmp_name'] -
    tmp_name 包含路径
    驻留在的临时文件
    服务器。该文件应该存在于
    服务器在临时目录中
    临时名称。

要将此文件包含在使用 libcurl 发送的 POST 中,请使用字段数组而不是字符串。将文件包含为字段之一:

$post_fields = array (
    $key => $value,
    ...
    $file_key => "@" . $filename,
);

如果使用“@”符号,libcurl 将读取该文件并将其包含在 POST 中。

然后使用

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

First, your script can access the uploaded file through the $_FILES array.

From http://www.tizag.com/phpT/fileupload.php

The $_FILES array is where PHP stores
all the information about files. There
are two elements of this array that we
will need to understand for this
example.

  • uploadedfile - uploadedfile is the reference we assigned in our HTML
    form. We will need this to tell the
    $_FILES array which file we want to
    play around with.
  • $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
  • $_FILES['uploadedfile']['tmp_name'] -
    tmp_name contains the path to the
    temporary file that resides on the
    server. The file should exist on the
    server in a temporary directory with a
    temporary name.

To include this file in the POST you send with libcurl, use an array of fields instead of a string. Include the file as one of the fields:

$post_fields = array (
    $key => $value,
    ...
    $file_key => "@" . $filename,
);

If you use the "@" symbol, libcurl will read the file and include it in the POST.

Then use

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文