PHP uploaded file locked by daemon - php

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

Related

how to access php.ini and set file_upload directive to "on"

I am working on my school project which we use Winscp as a server.
So im new to php trying to work on image upload and I have read many articles saying I need to edit my php.ini file and set file_uploads directive to "on". But I just do not know where my php.ini file is at.
Here is my link to my phpinfo.php: http://cgi.sice.indiana.edu/~baehy/phpinfo.php
So it says my php.ini is at /etc/php.ini and I cannot find it on my computer(i know it may sound silly)
Every comment is appreciated! Thank you all in advance!
Here is my code
<?php
session_start();
include('database.php');
ini_set('max_exection_time', 60);
if(!isset($_SESSION['userid'])){
header('location: http://cgi.sice.indiana.edu/~baehy/team72index.php');
} else {
echo "Welcome " . $_SESSION['userid'] . "<br>";
if(isset($_POST['submit'])){
$title = $_POST['title'];
$category = $_POST['category'];
$description = $_POST['description'];
//get file from the form and get following information
$file = $_FILES['coverimage'];
$fileName = $_FILES['coverimage']['name'];
$fileTmpName = $_FILES['coverimage']['tmp_name'];
$fileSize = $_FILES['coverimage']['size'];
$fileError = $_FILES['coverimage']['error'];
$fileType = $_FILES['coverimage']['type'];
//retrieve file extention using explode()
$fileExt = explode('.', $fileName);
//because some file extentions might be in capital letters
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png');
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
//if the size of the file is lesser than 1M kb = 1000mb
if($fileSize < 1000000){
$fileNameNew = uniqid('',true).".".$fileActualExt;
chmod('uploads/',0777);
echo "permission granted to uploads directory!" . "<br>";
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
echo $fileNameNew . "<br>";
echo "Successfully uploaded your file" . "<br>";
} else {
echo "Your file is too big to upload" . "<br>";
}
} else {
echo "There was an error uploading your file" . "<br>";
}
} else {
echo "This file extention is not allowed to be uploaded" . "<br>";
}
$sql = "INSERT INTO `recipe` (title, category, description, coverimage, userid)
VALUES ('".$title."', '".$category."', '".$description."', '".$fileName."', '".$_SESSION['userid']."')";
$result = mysqli_query($conn, $sql);
if($result){
echo "successfully added to database";
} else {
echo "failed to add to database";
}
$showImage = mysqli_query($conn, "SELECT `coverimage` FROM `recipe`");
}
}
p.s. and also do I need to put the absolute path of the folder('uploads') to use it in the code? Thank you!
Php.ini is system file located at remote web server and it contains global configuration for PHP. Only privileged users can edit php.ini.
You can change local configuration for your script using function ini_set, for example:
ini_set('max_exection_time', 60);
According to phpinfo() you sent, you already have file_uploads set to On. So you don't need to edit anything. Just open link you sent, press CTRL+F and search for file_uploads.
By the way, WinSCP is only application used to transfer files to remote web server using FTP/SFTP or similar protocols. Actually your web server is running on RHEL Apache 2.4.6. Just see section SERVER_SOFTWARE in your phpinfo.
Using WinSCP, connect to remote server. Go to remote server's root directory and then go to /etc/php.ini.
It won't be on your computer, it's on the remote server. The server is running on Apache. You are using Winscp as a FTP software to access the files of remote server.
Learn how to use WinSCP - https://www.siteground.com/tutorials/ssh/winscp/

PHP SSH move file to another directory [duplicate]

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'

how to upload to files to amazon EC2

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

Uploading images from Windows Phone 8 app to a website

I want to be able to upload images from my Windows Phone 8 app, to my website.
For this, I followed the tutorial from this website:
https://vortexwolf.wordpress.com/2013/06/04/windows-phone-select-and-upload-image-to-a-website-over-http-post/
It all worked good, on the windows phone app side. But I have problems getting the website upload.php file working. In that tutorial, the author is using http://posttestserver.com/post.php?dir=wp7posttest to get the response of upload. In my case, using that URL for testing was working good, but when I put my website url, I get no response, and a crash in visual studio, with following error:
Additional information: The remote server returned an error: NotFound.
This error happens is thrown on this line:
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
The upload.php on my website, looks like this:
if(isset($_GET['dir']))
{
$dir = $_GET['dir'];
if($_FILES['photo']['name'])
{
if(!$_FILES['photo']['error'])
{
$new_file_name = strtolower($_FILES['photo']['tmp_name']);
if($_FILES['photo']['size'] > (1024000))
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
if($valid_file)
{
move_uploaded_file($_FILES['photo']['tmp_name'], '../$dir/' . $new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
else
{
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
echo $message;
}
I am out of ideas, and stuck on this problem for the second day. Any answers/ideas of how to get the website upload.php file working properly?
Thanks!

uploading binary file to server

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.

Categories