我已经查看了很多关于将文件上传到远程目录(通过GoDaddy托管; windows机器)的问题的帖子,但是未能成功实现这些更改。
这是我想要做的。我编写了一个非常简单的PHP来将本地KML文件上传到远程GoDaddy目录。我这样做是为了为KML创建一个面向公众的URL,然后可以将其添加到我的Google Map应用程序中。
我做过的事情:
工作原理:
我的限制是什么:
以下是AJAX帖子:
$.ajax({
url: 'http://www.example.com/dev/Drawings/upload.php',
type: 'POST',
processData: false,
data: data,
dataType: 'html'
})
.done(function () {
dKML = new google.maps.KmlLayer('http://www.example.com/dev/Drawings/Uploads/' + filename);
dKML.setMap(map);
})
.fail(function (xhr, errorType, exception) {
var errorMessage = exception + "\n" + xhr.status + "\n" + errorType;
alert(errorMessage);
});
这是我的PHP(upload.php):
<?php
header("Access-Control-Allow-Origin: *");
if ( !isset($_FILES['file'])) {
die();
} else {
$temp_file = $_FILES['file']['tmp_name'];
move_uploaded_file($temp_file, "/Uploads/" . $_FILES['file']['name']);
}
?>
我一直犹豫要不要在这里发帖,因为我确信我能解决这个问题,但说实话,我在无数个小时的试错中迷失了。
我的问题是:
在语法方面有什么不正确吗?
你们有没有遇到过类似的问题,如果有的话,你能做些什么来解决这个问题呢?
是否存在问题,因为在测试上传时我使用的是localhost?
提前谢谢!
答案 0 :(得分:0)
一旦我发现tmp_name值为null,我就开始寻找更好的错误处理来添加到我的PHP中。
我最终将PHP更改为:
`代码
<?php
$error = "";
$msg = "";
$fileElementName = 'file';
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{
case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;
case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code available';
}
}elseif(empty($_FILES['file']['tmp_name']) || $_FILES['file']['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}else
{
$msg .= " File Name: " . $_FILES['file']['name'] . ", ";
$msg .= " File Size: " . @filesize($_FILES['file']['tmp_name']);
//for security reason, we force to remove all uploaded file
@unlink($_FILES['file']);
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
?>
`
我还将表单上的.submit()函数更新为:
`代码
e.preventDefault();
var dataString;
var action = 'http://www.example.com/dev/Drawings/upload.php';
if ($("#form").attr("enctype") == "multipart/form-data") {
dataString = new FormData($("#form").get(0));
contentType = false;
processData = false;
}
$.ajax({
type: "POST",
url: action,
data: dataString,
dataType: "html",
contentType: contentType,
processData: processData
})
.done(function () {
delay(function () {
dKML = new google.maps.KmlLayer('http://www.example.com/dev/Drawings/Uploads/' + filename);
dKML.setMap(map);
}, 2000);
})
.fail(function (xhr, errorType, exception) {
var errorMessage = exception + "\n" + xhr.status + "\n" + errorType;
alert(errorMessage);
});
`
一旦我能够解析PHP中的$ _FILES变量,一切都开始按预期工作。