【发布时间】:2016-06-22 06:10:42
【问题描述】:
我要做的是保存通过 CURL 从另一台服务器发送的二进制文件。
我的php上传器是:
<?php
define("UPLOAD_DIR", "/tmp/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}
所以我在我的网站上有这个:
example.com/uploader.php
在 2 号服务器上,我运行命令:
curl --request POST --data-binary "@binary.jpg" http://example.com/uploader.php
然后我检查我的 /tmp/ 文件夹中是否有名为 binary.jpg 的文件,但它不存在,这是什么问题?
谢谢。
【问题讨论】:
-
@NicolasBouvrette No.
-
你可以试试那篇文章中建议的 curl 命令,看看你的上传器是否有效?
-
@NicolasBouvrette 它不起作用。
-
您能否尝试将您上传的 PHP 代码替换为此处的第一个示例,看看您在尝试上传时是否遇到任何错误? :php.net/manual/en/features.file-upload.php(复制/粘贴示例,获得 221 票)- 这可能是 PHP 设置在欺骗您。