有没有办法使用
PHP使用REST将文件从客户端上传到服务器,
我试图使用下面的代码,它不起作用.
<?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,CURLOPT_POST,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
$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"; ?>
试试这个.
原文链接:https://www.f2er.com/php/134371.html的index.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,'http://www.restServiceHost.com/file3/upload.PHP'); curl_setopt($ch,1); curl_setopt($ch,$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>
和upload.PHP在http://www.restServiceHost.com/file3
<?PHP echo "<pre>"; echo 'in upload.PHP<br/>'; print_r($_FILES); print_r($_REQUEST); move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_REQUEST["fileName"]); ?>