I am trying to upload files to an amazon EC2 from android. however whenever i go to upload the files I get a Unexpected response code 500 error. From what I uderstand this is because the user doesnt have the correct permissions to upload files to the database? I know that the problem is with the amazon EC2 instance rather than the code. but below is my php code for uploading to the server. first of all is that the correct way to enter the upload folder (/var/www/html/uploads) ? Any help on how i can get this working would be great.
<?php
if(isset($_POST['image'])){
echo "in";
$image = $_POST['image'];
upload($_POST['image']);
exit;
}
else{
echo "image_not_in";
exit;
}
function upload($image){
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = "pleeease";
$upload_folder = "/var/www/html/upload";
$path = "$upload_folder/$id.jpg";
if(file_put_contents($path, base64_decode($image)) != false){
echo "uploaded_success"
}
else{
echo "uploaded_failed";
}
}
?>
Just a few notes:
First, you should make sure to send your data from the app with the enctype of multipart/form-data before submitting to the server.
Second, try variants of this simplified code:
if(isset($_FILES['image']))
{
$fileTmp = $_FILES['image']['tmp_name'];
$fileName = $_FILES['image']['name'];
move_uploaded_file($fileTmp, "/var/www/html/uploads/" . $fileName);
echo "Success";
}
else
{
echo "Error";
}
And finally, assuming you're using Apache and the user name is www-data, you'll need to make sure it can write to the upload folder:
sudo chown www-data:www-data /var/www/html/uploads
sudo chmod 755 /var/www/html/uploads
Related
I am actually sending a file from my android app to the localhost application server . for this i am using xampp in ubuntu, but for some unknown reason the file that are creating in the htdocs folder are owned by some user called as daemon and it is locked. Due to this my python service module (watch dog) is not able to detect the creation of the file in that folder. Plzz help as i not getting any ideas?????
?php
if(isset($_FILES["uploaded_file"]["name"])){
$name = $_FILES["uploaded_file"]["name"];
$tmp_name = $_FILES['uploaded_file']['tmp_name'];
$error = $_FILES['uploaded_file']['error'];
if(!empty($name))
{
$location = './uploads/';
if(!is_dir($location))
mkdir($location);
if(move_uploaded_file($tmp_name, $location.$name))
{
echo "Uploaded";
}
}
else
echo 'Please choose a file';
}
?>
PHP code for uploading the file
i have the following code in android
String uploadId = UUID.randomUUID().toString();
//Creating a multi part request
new MultipartUploadRequest(getContext(), uploadId, url_upload_cert)
.addFileToUpload(path, "pdf") //Adding file
.addParameter("name", name) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
and in php
<?php
//importing dbDetails file
require_once __DIR__ . '/db_connect.php';
//this is our upload folder
$upload_path = 'android/';
//Getting the server ip
$server_ip = gethostbyname(gethostname());
//creating the upload url
//$upload_url = 'http://'.$server_ip.'/pavo_project/'.$upload_path;
$upload_url='/src';
//response array
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
//checking the required parameters from the request
if(isset($_POST['name']) and isset($_FILES['jpg']['name'])){
// connecting to db
$db = new DB_CONNECT();
//getting name from the request
$name = $_POST['name'];
//getting file info from the request
$fileinfo = pathinfo($_FILES['jpg']['name']);
//getting the file extension
$extension = $fileinfo['extension'];
//file url to store in the database
$file_url = $upload_url . $name . '.' . $extension;
//file path to upload in the server
$file_path = $upload_url . $name . '.'. $extension;
//trying to save the file in the directory
try{
//saving the file
move_uploaded_file($_FILES['jpg']['tmp_name'],$file_path);
}catch(Exception $e){
$response['error']=true;
$response['message']=$e->getMessage();
}
//closing the connection
// mysqli_close($db);
}else{
$response['error']=true;
$response['message']='Please choose a file';
}
//displaying the response
echo json_encode($response);
}
?>
when i run the android code it runs succesfully with no error but when i check the image in the src folder its not there
The same things happens when i test the script using postman
Am using wamp server on windows .
what might be the reason causing this?
Check the writing permissions of your folder.
For example in Linux, a level above your folder you can user the following command:
sudo chmod -R 777 <your-folder-name>
And that might let the php code save the file inside of it, because it will grant the permissions to write/read/delete to every user.
Also you can check the php.ini file, and check if the line:
file_uploads = On
Has the On value
If that doesn't work you can check for a default php.ini file and try replacing your php.ini file with that.
I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.
$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
else {
echo $_FILES['image']['error']."<br />";
echo $target;
}
Any help is appreciated.
Eric R.
Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:
Check if the $target directory exists:
$target = '/data/etc....';
if (!is_dir($target)) {
die("Directory $target is not a directory");
}
Check if it's writeable:
if (!is_writable($target)) {
die("Directory $target is not writeable");
}
Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:
$target = $target . basename($_FILES['image']['name']);
if (!is_writeable($target)) {
die("File $target isn't writeable");
}
Beyond that:
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:
if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
// file was properly uploaded
if (!is_uploaded_File(...)) {
die("Something done goofed - not uploaded file");
}
if (!move_uploaded_file(...)) {
echo "Couldn't move file, possible diagnostic information:"
print_r(error_get_last());
die();
}
} else {
die("Upload failed with error {$_FILES['images']['error']}");
}
You need to make sure that whoever is hosting your pages has the settings configured to allow you to upload and move files. Most will disable these functions as it's a sercurity risk.
Just email them and ask whether they are enabled.
Hope this helps.
your calls to is_uploaded_file and move_uploaded_file vary. for is_uploaded_file you are checking the 'name' and for move_uploaded_file you are passing in 'tmp_name'. try changing your call to move_uploaded_file to use 'name'
I have some code that uploads a file on AWS server. It was working fine until today, I decided to delete all code from my AWS server, and uploaded the latest copy of code, and it has stopped working from then on. Can someone please spot the error.
I tried to debug it a little bit, I found that mkdir(...) function is returning false. I don't know why it is returning false. I have all the permissions on my "uploads" folder - drwxrwxr-x. I am also using enctype="multipart/form-data" on my form.
Can someone please point me out in the right direction? I will do the rest.
Thanks. Please help!
$uniqueKey = md5(microtime(true)).uniqid();
$ret = array();
function makedir($dir){
if (!is_dir($dir) && strlen($dir)>0){
mkdir($dir, 0777, true);
chmod($dir, 0777);
}
}
$output_dir = "../uploads/".$uniqueKey."/";
makedir($output_dir);
if(isset($_FILES["myfile"])){
$error =$_FILES["myfile"]["error"];
$ret['unique'] = $uniqueKey;
$ret['error'] = $error;
if(!is_array($_FILES["myfile"]["name"])){
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
$ret[]= $fileName;
}else{
$fileCount = count($_FILES["myfile"]["name"]);
for($i=0; $i < $fileCount; $i++){
$fileName = $_FILES["myfile"]["name"][$i];
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
$ret[]= $fileName;
}
}
}
echo json_encode($ret);
It was the permissions. I changed the permission pf my folder from drwxrwxr-x to drwxrwxrwx, and it worked.
I'm converting an image to a binary file in IOS, which works just fine. This will be handled by my php script which is suppose to upload this image to my ubuntu server. The problem is i keep getting file=unsuccessful. i've tried different directory paths, but cant seem to solve this issue.
This $directory will return this: /var/www/User/core/ios/
<?
if(!empty($_POST))
{
$message = $_POST['message'];
$directory = $_SERVER['DOCUMENT_ROOT'] . '/User/core/ios/';
$file = basename($_FILES['userfle']['upload']);
$uploadfile = $directory . $file;
var_dump($_FILES);
$randomPhotoID = md5(rand() * time());
echo 'file='.$file;
echo $file;
if (move_uploaded_file($_FILES['userfle']['tmp_name'], $uploadfile)) {
echo 'successful';
}
else
{
echo 'unsuccessful';
}
}
else
{
echo('Empty post data');
}
?>
Check the error file of your php(you can make sure if you enabled the error log in php.ini),
if you don't have the permission or for some other reasons it can't move the file ,there will be a record in that file.
Some time you can try the command setenforce 0 if you confirm you(I means the user of apache) have the permission to move the file but it not work.
By the way if the file you want to move is not upload by post, there is no error log and the move function will return false.