我不明白为什么$ _FILES对象似乎存在并告诉我文件路径,但是当我查找文件它不存在时(注意在php.ini中没有设置upload_tmp_dir)所以它应该转到/ tmp目录,但是我没有在命令行看到它。)
我在控制台得到的回复:
Array
(
[upload_file] => Array
(
[name] => Capture.PNG
[type] => image/png
[tmp_name] => /tmp/phplFIm0b
[error] => 0
[size] => 12612
)
)
file upload failed
界面页面:
<?php
include 'php/assets.php';
$sel_str = "selected='selected'";
$default_page_size = 100;
$num_assets = count_assets();
$num_pages = ceil($num_assets/$default_page_size);
?>
<html>
<header>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/sacs.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link href="css/sacs.css" rel="stylesheet">
</header>
<body>
<!--Popup Modal-->
<div id="modal" class="modal fade">
</div>
<!--Main Body Container-->
<div class="container">
<!--Navbar Controls-->
<div class="row">
<?php $_POST["PAGE"] = "MM"; ?>
<?php include 'php/navbar.php'; ?>
</div>
<!--Sort/Filter Controls-->
<div class="row">
<div class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Upload Metric Events</div>
<div id="metric_upload_panel" class="panel-body">
<div class="col-lg-6">
<div class="input-group">
<input id="file_path" type="text" class="form-control" placeholder="Uploading Metric..." aria-label="Search for...">
</input>
<span class="input-group-btn">
<label class="btn btn-primary">
<input id="fileinput" type="file" hidden>Select File</input>
</label>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<?php phpinfo(); ?>
<script>
function uploadFile(file){
var url = 'php/upload_metric_post.php';
var xhr = new XMLHttpRequest();
var fd = new FormData();
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// Every thing ok, file uploaded
console.log(xhr.responseText); // handle response.
}
};
fd.append("upload_file", file);
xhr.send(fd);
}
var fileinput = document.getElementById('fileinput');
fileinput.addEventListener('change', function () {
var file = this.files[0];
uploadFile(file); // call the function to upload the file
//alert(response);
}, false);
帖子文件:
<?php
$uploadedFile = $_FILES['upload_file']['tmp_name'];
$destination = "product_$newname";
//echo sys_get_temp_dir();
print_r($_FILES);
if(file_exists($uploadedFile)){
echo "file uploaded to temp dir";
}else{
echo "file upload failed";
exit();
}
if(move_uploaded_file($uploadedFile, $destination)){
echo "upload complete";
}else{
echo "move_uploaded_file failed";
exit();
}
if (isset($_FILES['upload_file'])) {
if(file_exists($_FILES['upload_file']['tmp_name'])){
echo "file uploaded to temp dir";
}else{
echo "file upload failed";
exit();
}
$uploaddir = '/var/www/html/DEV/gui/';
$uploadfile = $uploaddir . basename($_FILES['upload_file']['name']);
if(move_uploaded_file($_FILES['upload_file']['tmp_name'], $uploadfile)){
echo $_FILES['upload_file']['name']. " OK";
} else {
echo "Cannot Move File From ". $_FILES['upload_file']['tmp_name']. " TO ". $uploadfile;
}
exit;
} else {
echo "No files uploaded ...";
}
?>