我无法在远程服务器图像文件夹中找到从phonegap相机api拍摄的上传照片。照片的名称如“23497678900.jpg”保存在我的mysql表中,但实际照片未保存在指定的服务器文件夹中。
以下是我用来捕获,上传和在服务器上的代码:
<!DOCTYPE html>
<html>
<head>
<title>Submit form</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for device API libraries to load
//
document.addEventListener("deviceready",onDeviceReady,false);
// device APIs are available
//
function onDeviceReady() {
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Show the selected image
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = imageURI;
}
// A button will call this function
//
function getPhoto() {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI });
}
function uploadPhoto() {
//selected photo URI is in the src attribute (we set this on getPhoto)
var imageURI = document.getElementById('smallImage').getAttribute("src");
if (!imageURI) {
alert('Please select an image first.');
return;
}
//set upload options
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.params = {
firstname: document.getElementById("firstname").value,
lastname: document.getElementById("lastname").value
}
options.headers = {
Connection: "close"
};
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://hostname.com/image/upload.php"), win, fail,
options);
}
// Called if something bad happens.
//
function onFail(message) {
console.log('Failed because: ' + message);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
alert("Response =" + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
</script>
</head>
<body>
<button onclick="getPhoto();">Select Photo:</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" /><br>
<form id="regform">
First Name: <input type="text" id="firstname" name="firstname"><br>
Last Name: <input type="text" id="lastname" name="lastname"><br>
<input type="button" id="btnSubmit" value="Submit" onclick="uploadPhoto();">
</form>
</body>
</html>
upload.php代码是:
<?php
$server = "localhost";
$username = "username";
$password = "pw";
$database = "mydbase";
$con = mysqli_connect($server, $username, $password, $database) or die ("Could not
connect: " . mysqli_connect_error());
$firstName2=$_POST["firstname"];
$lastName2=$_POST["lastname"];
//storing file in filename variable
$fileName = $_FILES['file']['name'];
//This is the directory where images will be saved
$target = "image/";
$target = $target . basename( $_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'],$target);
$sql = "INSERT INTO postinfo (firstname,lastname,imgfile) ";
$sql .= "VALUES ('$firstName2', '$lastName2', '".basename($_FILES['file']'name'])."')";
if($con->query($sql)){
echo "Your comment has been sent";
}
else{
echo "Error in sending your comment";
}
?>
关于如何将实际图片上传到我服务器的图片文件夹中,我已经没想过了。
答案 0 :(得分:0)
这很可能是PHP脚本中带有$target
变量的问题。如果您有权访问服务器的根目录,则可能会在/image
中找到您的图像。
如果您希望图像与脚本位于同一目录中,则需要更多类似的内容:
$target = dirname(__FILE__) . '/image/';