【问题标题】:upload zip folder onto remote server using PHP?使用 PHP 将 zip 文件夹上传到远程服务器?
【发布时间】:2014-09-16 04:54:57
【问题描述】:

我正在尝试使用 PHP CURL 将 zip 文件夹上传到远程服务器,但我不知道为什么上传到远程服务器的 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);
?>

有人可以告诉我我错过了什么或做错了什么吗?

提前致谢,

【问题讨论】:

  • 它是否适用于其他文件类型?检查以确保服务器没有遇到磁盘配额问题。
  • 您正在查看handle.php 中的$POST['file'] 但输入似乎被称为uploadefile。无论如何,您将内容放入 zip 文件中,但从不尝试将其“解压缩”到文件夹中...

标签: php curl


【解决方案1】:

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

句柄.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"];
    }
  }

【讨论】:

  • 无论如何,将上传的文件移动到根文件夹而不是将其保存在上传文件夹中?例如将其移动到 public_html 文件夹?我确实尝试了 rename();我还尝试了move_uploaded_file($_FILES["file"]["tmp_name"], "../" . $_FILES["file"]["name"]); 低于所有其他代码,但它不会将上传的文件夹移动到public_html。有什么建议吗?
  • @shell 您可以将完整路径 /home/user/public_html 例如或以更灵活的方式放置更多 move_uploaded_file($_FILES["file"]["tmp_name"], realpath('../').'/'. $_FILES["file"]["name"];
  • 谢谢,但仍然没有将文件移动到 public_html!它确实将其上传到上传文件夹,但不会将其复制或移动到 public_html!
  • @shell 试试move_uploaded_file($_FILES["file"]["tmp_name"], '/home/user/public_html '. $_FILES["file"]["name"]); // Adjust to your full desired path
  • 仍然没有将其移至公众)html。我确保路径也是正确的......无论如何,将上传文件夹中的任何内容移动到 public_html?
猜你喜欢
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多