使用PHP将zip文件夹上传到远程服务器上


upload zip folder onto remote server using PHP?

我正试图使用PHP CURL将zip文件夹上传到远程服务器上,但我不知道为什么上传到远程server上的zip文件夹/文件是空的!

基本上,文件会被上传(有些方法),但当我查看上传的文件夹时,它显示为0 bytes,但zip文件夹中有700 bytes个文件!

这是我的代码:

<form enctype="multipart/form-data" encoding="multipart/form-data" method="post" action="myfile.php">
  <input name="uploadedfile" type="file" value="choose">
  <input type="submit" value="Upload">
</form>

<?php
if (isset($_FILES['uploadedfile']) ) {
    $filePath  = $_FILES['uploadedfile']['tmp_name'];
    $POST_DATA = array(
        'file' => '@'.  realpath($filePath)
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://remotesite/handle.php');
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
    $response = curl_exec($curl);
    curl_close ($curl);
     if($errno = curl_errno($curl)) {
        $error_message = curl_strerror($errno);
        echo "cURL error ({$errno}):'n {$error_message}";
    } else {
        echo "<h2>File Uploaded</h2>";
    }
}
?>

这是我的handle.php代码:

<?php
$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
/* Now you can copy the uploaded file to your server. */
file_put_contents('subins.zip', $decoded_file);
?>

有人能告诉我我遗漏了什么或做错了什么吗?

提前感谢,

当你上传一个带有post的文件到php时,它会创建一个临时复制的文件,当脚本结束时,这个文件就会消失。您需要将上传的文件存储到不同的位置:

Handle.php:

if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
      // Move the file to the desired directory
      move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
  }