我正在使用Cordova文件上传插件上传图片。我正在尝试将图片从图库上传到服务器,但文件没有上传到服务器。
这是js代码。
function uploadImage() {
document.getElementById('picture_msg').innerHTML = "";
// Get URI of picture to upload
navigator.camera.getPicture(
function(uri) {
try {
// Pick image from div
var img = document.getElementById('pimage');
img.style.visibility = "visible";
img.style.display = "block";
var imageURI = uri;
if (!imageURI || (img.style.display == "none")) {
document.getElementById('picture_msg').innerHTML = "Tap on picture to select image from gallery.";
return;
}
// Verify server has been entered
server = document.getElementById('server').value;
server = 'http://domain_name/mobile_app/img_upload.php';
console.log("Server "+server);
if (server) {
// Specify transfer options
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
// Transfer picture to server
var ft = new FileTransfer();
ft.upload(imageURI, server, function(r) {
document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
img.src = uri;
img.width = 100;
img.height = 100;
},
function(error) {
document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
}, options);
}
else {
document.getElementById('picture_msg').innerHTML = "Server Not Found";
}
}
catch(exce) {
alert(exce);
}
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('picture_msg').innerHTML = "No Image Found";
},
{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
}
这是img_upload.php代码。
print_r($_FILES);
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "mobile_app/".$new_image_name);
我也试过这个。
$dirname = "user_img/";
// If uploading file
if ($_FILES) {
print_r($_FILES);
// mkdir ($dirname, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
}
我正在上传成功的消息但是当我检查服务器上的数据时它显示为空。没有文件。
请告诉我这里做错了什么。
修改
好像是PHP文件的问题。网络服务器中的路径为/public_html/app/user_imgs
我的PHP文件 img_upload.php 位于app文件夹下。