简体   繁体   English

使用PHP上传文件时出错

[英]Error in uploading file using PHP

I am asking the user to upload a file, through the following form making a POST Request to another page Upload.php 我要求用户通过以下形式向另一个页面Upload.php发出POST请求来上传文件

<form name="input" action="upload.php" method="post" enctype="multipart/form-data">
    Username: <input type="text" name="username" /><br/>
    <input type="file" name="file" /> <br/>
    <input type="submit" value="Submit" />
</form>

At Upload.php, I use the data filled in the previous form to make an POST Request using Curl function for which I have written the following code: 在Upload.php中,我使用上一表格中填写的数据使用Curl函数发出POST请求,为此我编写了以下代码:

$data = array(
        "username" => $username,
        "password" => $password,
        "title" => $title,
        "srcfile" => "@".$_FILES['file']['tmp_name']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response=curl_exec($ch);

echo $response;

This way I am able to upload the file successfully but the file uploaded has a name without any extension which is set by this 这样我就可以成功上传文件,但是上传的文件的名称没有由此设置的任何扩展名

$_FILES['file']['tmp_name'] $ _FILES [ '文件'] [ 'tmp_name的值']

and thus I am not able to use the file on the server. 因此我无法在服务器上使用该文件。

How should I use the file obtained from the POST Request to make another POST Request? 我应该如何使用从POST请求获得的文件来发出另一个POST请求?

Thanks in advance. 提前致谢。

The problem in the above case is that though we can have the file using $_FILES['file']['tmp_name'], however, the file extension is not the same as that of original file and even the following code does not work which should run as filed in this bug https://bugs.php.net/bug.php?id=48962 in PHP 5.0 在上述情况下的问题是,尽管我们可以使用$ _FILES ['file'] ['tmp_name']来保存文件,但是文件扩展名与原始文件的扩展名不同,甚至以下代码也不起作用在PHP 5.0中应该按照此bug https://bugs.php.net/bug.php?id=48962中的说明运行

"srcfile" => "@".$_FILES['file']['tmp_name'].";filename=".$_FILES['file']['name'].";type=".$_FILES['file']['type']

I have finally used the following solution to solve the above problem: 我终于使用以下解决方案来解决上述问题:

STEP 1: Save the file sent through the form's POST request in a folder temporarily on the server or your machine. 步骤1:将通过表单的POST请求发送的文件临时保存在服务器或计算机上的文件夹中。

STEP 2: Now use the path where file has been saved temporarily to make a POST request using CURL for which the code is used same as above mentioned with changes in file location. 步骤2:现在使用临时保存文件的路径,使用CURL发出POST请求,该CURL的使用与上述代码相同,但文件位置有所变化。

"srcfile" => "@".temp_file_path

$_FILE['file']是一个数组,表示SimpleXMLElement属性的complex types

You should read the file, encode it and assign the encoded value to the XML element. 您应该阅读文件,对其进行编码,然后将编码后的值分配给XML元素。 For example you can use something like: 例如,您可以使用类似:

$xml->file = base64_encode( file_get_contents( $_FILE['file'][ 'tmp_name' ] ) );

Of course in a real code you should add error checking etc. 当然,在真实代码中,您应该添加错误检查等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM