我正在尝试使用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);
?>
有人可以让我知道我错过了什么或做错了吗?
提前致谢,
答案 0 :(得分:1)
当您将带有帖子的文件上传到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"];
}
}