我有以下PHP代码 -
<?php
$uploaddir = getcwd().'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
我想要做的是,通过向脚本发送HTTP标头来上传文本文件。这是我发送的标题 -
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------556304871068
POST内容 -
-----------------------------556304871068\r\n
Content-Disposition: form-data; name="MAX_FILE_SIZE"\r\n
\r\n
512000\r\n
-----------------------------556304871068\r\n
Content-Disposition: form-data; name="userfile"; filename="example.txt"\r\n
Content-Type: text/plain\r\n
\r\n
ThisText\r\n
\r\n
-----------------------------556304871068--\r\n
但它正在回复一条错误信息 -
<br />
<b>Notice</b>: Undefined index: userfile in <b>C:\HostingSpaces\Test\mysite.com\wwwroot\admin\upload.php</b> on line <b>4</b><br />
<pre><br />
我无法理解为什么会出现此错误以及如何解决此问题。
注意:当我正常上传文件时(使用HTML上传页面),PHP脚本运行良好。
编辑:HTML表单代码:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
答案 0 :(得分:0)
在代码的第一行检查isset($_FILES['userfile'])
:
if (isset($_FILES['userfile'])) {
....
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
...
}
//end of your file or
} else {
echo 'There is no file to send!';
}
//end of your file
答案 1 :(得分:0)
这是一个通过POST发送带有php / cURL文件的简单脚本:
<?php
$target_url = 'http://127.0.0.1/accept.php';
//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('./sample.jpeg');
/* curl will accept an array here too.
* Many examples I found showed a url-encoded string instead.
* Take note that the 'key' in the array will be the key that shows up in the
* $_FILES array of the accept script. and the at sign '@' is required before the
* file name.
*/
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
这是接受该文件的相应脚本。
<?php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['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";
?>