简体   繁体   English

使用 REST 在 php 中上传文件

[英]Upload Files in php using REST

Is there a way to upload a file from a client side to the server using REST using PHP,有没有办法使用 REST 使用 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;

?>

and my upload.php和我的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";
?>

Try this.尝试这个。

index.php索引.php

<?php
    echo "<pre>";
    print_r($_FILES);
    error_reporting(9);
    if($_REQUEST['action'] == 'submit') {
        $ch = curl_init();
        $filePath = $_FILES['file_upl']['tmp_name'];
        $fileName = $_FILES['file_upl']['name'];
        $data = array('name' => 'Foo', 'file' => "@$filePath", 'fileName' =>$fileName);             
        curl_setopt($ch, CURLOPT_URL, 'http://www.restServiceHost.com/file3/upload.php');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_exec($ch);
        curl_close($ch);
    }
?>

<form name="file_up" action="" method="POST" enctype="multipart/form-data">
Upload your file here
<input type="file" name="file_upl" id="file_upl"/>
<input type="submit" name="action" value="submit"/>
</form>

and upload.php in http://www.restServiceHost.com/file3并在http://www.restServiceHost.com/file3 中上传.php

<?php
    echo "<pre>";
    echo 'in upload.php<br/>';
    print_r($_FILES);
    print_r($_REQUEST);
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_REQUEST["fileName"]);
?>

I think you should be looking for $_FILES['file_contents'] and not $_POST['file'] .我认为您应该寻找$_FILES['file_contents']而不是$_POST['file'] – user1190992 – 用户 1190992

php5.5+ php5.5+

$filePath = $_FILES['file_upl']['tmp_name'];
$type=$_FILES['file_upl']['type'];
$fileName = $_FILES['file_upl']['name'];
    
$data = array('file_upl' => curl_file_create($filePath, $type, $fileName));

$ch = curl_init();     
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/upload.php');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);

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

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