使用PHP CURL上传文件 - 文件丢失
我正在测试使用cURL上传文件的可能性。 服务器的版本是PHP 5.4使用PHP CURL上传文件 - 文件丢失
要从“发送”服务器我使用这个代码上传:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="Filedata" id="Filedata" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if ($_POST['submit']) {
$real_name = $_FILES['Filedata']['name'];
$ch = curl_init();
$data = array('name' => 'test.txt', 'file' => '@'.$real_name);
curl_setopt($ch, CURLOPT_URL, 'http://www.site1.com/upload_remote.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
我使用的print_r($数据)见$ data数组和一切似乎没问题。
在“远程”服务器(地址:http://www.site1.com/upload_remote.php)我有这样的代码:
$file = 'test2.txt'
if(move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
echo "The file ". basename($_FILES['file']['name']). " has been uploaded";
}
但文件不存在 有一个在error_logs文件中没有错误。 我忘了什么吗? 谢谢。
move_uploaded_file的第二个参数是目标文件应该移动的路径,而您只是传递不是目录的$文件。
我已将目标更改为:$ file ='/'; (相同的目录),即使该文件不存在... – 2014-09-05 19:05:52
您是否收到任何类型的错误?你可以添加else语句来检查move_uploaded_file是否返回true或false。 – 2014-09-05 19:09:30
返回false并且我的error_log文件也为空 – 2014-09-05 19:16:59
在这里看到:http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/ – 2014-09-05 18:58:45
我tryed代码但不成功 – 2014-09-05 19:20:07
我的错误..工作正常!谢谢。 – 2014-09-05 19:42:36