无法将文件从服务器传输到另一台服务器

时间:2014-11-15 06:20:56

标签: php

大家好我无法使用php将文件从我的服务器上传到朋友服务器。起初我得到了26错误,现在我收到了这个错误:

Error: Possible file upload attack: filename ''.Array ( ) 1

以下是我的代码

<?php
 $ch = curl_init();
chmod("grademegood.us/uploadedfiles/hello.txt", 0755);

  $post =array(
  'file' => '@' . realpath('./uploadedfiles/hello.txt')
 );

 curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
 curl_setopt($ch, CURLOPT_URL, "http://friendsdomain.com/upload.php");
 curl_setopt($ch, CURLOPT_POST,true);

 $data = curl_exec($ch);

 if (curl_errno($ch))
{
 print curl_errno($ch);
}
else
{
  curl_close($ch);
 }

 echo $data;

 ?>

这是接收方代码:

<?php
$uploads_dir = './phpbackend/';
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) 
{
  echo  "File ".  $_FILES['userfile']['name']  ." uploaded successfully to
 $uploads_dir/$dest.\n";
 $dest=  $_FILES['userfile'] ['name'];
 move_uploaded_file ($_FILES['userfile'] ['tmp_name'], "$uploads_dir/$dest");
} 
 else 
  {
    echo "Possible file upload attack: ";
    echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
    print_r($_FILES);
  }
?>

我需要一些帮助。

1 个答案:

答案 0 :(得分:0)

当您设置cURL请求时,您正在设置POST个变量:

  $post =array(
    'file' => '@' . realpath('./uploadedfiles/hello.txt')
    );

数组键是$_POST数据中发送的变量名。

当您在接收服务器上解压缩文件时,您正在寻找

is_uploaded_file($_FILES['userfile']['tmp_name']

其中'userfile'应该是发送机发送的POST请求中使用的变量名。

POST数据中创建的变量必须与接收机器使用的变量名称匹配,否则两台机器将无法正确识别不同的字段。

目前,您正在使用file中的文件创建POST数据,但您的接收计算机正在userfile变量中查找该文件。您的测试失败,因为变量名称不匹配,因此消息。