使用REST上传文件 [英] Upload Files in php using REST

查看:138
本文介绍了使用REST上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以使用PHP从客户端上传文件到服务器,

Is there a way to upload a file from a client side to the server using REST using PHP,

我试图使用下面的代码,它是不工作。

I am trying to use the below code, and it is not working from me.

<?php

$file_to_upload = array('file_contents'=>'@c:\\test.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/upload.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, TRUE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
 curl_exec($ch) or die( curl_error($ch) );
$error = curl_error($ch);
curl_close ($ch);
echo " Server response: ".$result;
echo " Curl Error: ".$error;

?>

和我的upload.php

and my upload.php

$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_POST['file']['name']);

echo $uploadfile;
echo "\n";
echo '<pre>';
echo $_POST['file']['tmp_name'];
       if (move_uploaded_file($_POST['file']['tmp_name'], $uploadfile)) {
           echo "File is valid, and was successfully uploaded.\n";
       } else {
           echo "Possible file upload attack!\n";
       }
       echo 'Here is some more debugging info:';

      print_r($_FILES);
       echo "\n<hr />\n";
       print_r($_POST);
print "</pr" . "e>\n";
?>


推荐答案


正在寻找 $ _ FILES ['file_contents'] ,而不是
$ _ POST ['file'] 。 - user1190992

I think you should be looking for $_FILES['file_contents'] and not $_POST['file']. – user1190992

这篇关于使用REST上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆