简体   繁体   中英

Can't upload an image to a Linux server via Phonegap and PHP

I am making an app in PhoneGap that requires an image to be uploaded to a Linux server. I set up a test CentOS 7 VM and installed and configured the LEMP stack on it and then created a php script called upload.php (below). This script was just taken off of the INTERNET from multiple different tutorials and I just edited the path to save the file to my desired location.

I then created my javascript code to upload it to the server but it wasn't working so I just took the code from the online tutorial just for testing purposes to see if it was working properly (also below). I get the alert which indicates that it is working but no image was being uploaded so I tried disabling my firewall but still no luck.

I'm not sure if it is the server setup, the javascript, the php or just my ISP. Any help is much appreciated.

I am using the cordova plugins: Camera

File

File transfer


PHP:

<?php $new_image_name = urldecode($_FILES["file"]["name"]).".jpg"; move_uploaded_file($_FILES["file"]["tmp_name"], "/upload/".$new_image_name); ?>

Javascript:

 navigator.camera.getPicture(uploadPhoto, function(message) { alert('get picture failed'); }, { quality: 100, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }); } function uploadPhoto(imageURI) { var options = new FileUploadOptions(); options.fileKey = "file"; options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); options.mimeType = "image/jpeg"; console.log(options.fileName); var params = new Object(); params.value1 = "test"; params.value2 = "param"; options.params = params; options.chunkedMode = false; var ft = new FileTransfer(); ft.upload(imageURI, "http://192.168.1.11/upload.php", function(result){ alert(JSON.stringify(result)); alert('works'); }, function(error){ console.log(JSON.stringify(error)); alert('doesnt'); }, options); } 

There were a few problems that needed to be overcome. All of which were fairly simple.

I was using an absolute path but php interpreted it as a relative path so I just changed the path.

The directory also was owned by root so i fixed that using chown nginx:nginx uploads

The firewall was blocking access from my phone so I disabled the firewall but still need to come up with a better solution. I'm sure it's fairly simple but I haven't got round to it yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM