Parse AndroidManifest attributes - php

I made a simple apk uploader with PHP, I want to parse manifest attributes form AndroidManifest.xml but I got the following errors, Any ideas !?
Fatal error: Call to a member function xpath() on a non-object in E:\AppServ\www\apkUploader\mainApi.php on line 21
line 21 = $versionName = $xml->xpath('/manifest/#android:versionName');
Source code :
/* For Apk File */
if (($_FILES["mFile"]["type"] == "application/vnd.android.package-archive") || ($_FILES["mFile"]["type"] == "application/octet-stream"))
{
if ($_FILES["mFile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["mFile"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["mFile"]["name"] . "<br />";
echo "Type: " . $_FILES["mFile"]["type"] . "<br />";
echo "Size: " . ($_FILES["mFile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["mFile"]["tmp_name"] . "<br />";
error_reporting(E_ERROR | E_PARSE);
$dom = new DOMDocument();
$dom->load('AndroidManifest.xml');
$xml = simplexml_import_dom($dom);
$versionName = $xml->xpath('/manifest/#android:versionName');
$versionCode =$xml->xpath('/manifest/#android:versionCode');
$package = $xml->xpath('/manifest/#package');
echo "VERSION NAME :".$versionName[0]->versionName."<br/>";
echo "VERSION CODE :".$versionCode[0]->versionCode."<br/>";
echo "VERSION PAC :".$package[0]->package."<br/>";
if (file_exists("upload/" . $_FILES["mFile"]["name"]))
{
echo $_FILES["mFile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["mFile"]["tmp_name"],
"upload/" . $_FILES["mFile"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["mFile"]["name"];
}
}
}
else
{
echo "Invalid file";
}

It looks like $xml = simplexml_import_dom($dom); does not return a valid object. This usually means that the file cannot be loaded or parsed.
Don't forget to check if there was an error :
$xml = simplexml_import_dom($dom);
if (!$xml)
{
// An error occurred
}

Related

HTML & PHP Information Table

So i'm hosting servers and trying to make a table displaying the information server name players etc i have a script that displays this but i suck at css, What i'm trying to get is a little like in this website http://echelon.kthx.at/pubbans.php
<?php
include ("BC2Conn.php");
// opens a connection to gameserver
$BC2Conn = new BC2Conn("31.185.143.136", 48888);
if (!$BC2Conn->isConnected()) {
echo "Connection could not be established. " .
"To debug, set '-d' as 3rd parameter to new connection.<br />" .
"<br />" .
"Example: \$BC2Conn = new BC2Conn(\"127.0.0.1\", 48888, \"-d\");";
return 0; // stop executing this script
}
// secure login
// $BC2Conn->loginSecure("password");
// unsecure login (not salted)
// some random serverinformation
echo "Servername: " . $BC2Conn->getServerName() . "<br />";
echo "Players: " . $BC2Conn->getCurrentPlayers() . "/" . $BC2Conn->getMaxPlayers() . "<br />";
echo "Playmode: " . $BC2Conn->getCurrentPlaymodeName() . "<br />";
echo "Current Map: " . $BC2Conn->getCurrentMapName() . "<br /><br /><br /><u>Players:</u><br /><br />";
// playerlist
$playerNames = $BC2Conn->getPlayerlistNames();
foreach ($playerNames as $key => $content) {
if ($BC2Conn->getPlayerClantag($content) != "") {
echo "[" . $BC2Conn->getPlayerClantag($content) . "]";
}
echo " " . $BC2Conn->getPlayername($content) . " - Kills: ";
echo $BC2Conn->getPlayerKills($content) . " | Deaths: ";
echo $BC2Conn->getPlayerDeaths($content) . " | Score: ";
echo $BC2Conn->getPlayerScore($content) . "<br />";
}
// logout
$BC2Conn->logout();
?>

PHP -- Undefined index:

I am having an issue with undefined error in my php. After research I know I know I should use isset but I'm not sure how to implement it. Here is the peice of code that causes the issue:
if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
{
Should I put the isset inside the if function before each $_FILES or where should it go ?
Here is the full piece of code, as you will probably notice I am quite new to PHP:
$ivalid_file='0';
if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "invalid_file";
}
As far as I understand, you want to know how to check if the file was ever uploaded and how to implement it in code. All you gotta do is, wrap you entire code in an if block which checks if $_FILES['files'] is set like so:
//Check if "file" exists here:
if(isset($_FILES["file"])) {
$ivalid_file='0';
if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "invalid_file";
}
} else {
// Display appropriate error
}

having difficulty in finding the upload file directory

I have made a php code to upload a file....It uploads it sucessfully...but how do i phsicaly go and check where it is...?
NOTE : i am getting fail as output from the move_uploaded_file ()
here's my code :
<?php
$allowedExts = array("c", "cpp", "py", "java");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ( ($_FILES["file"]["size"] < 100000) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
mkdir ("./upload");
if (move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"]))
{
echo "sucess";
}
else
{
echo "fail";
}
}
}
}
else
{
echo "Invalid file";
}
?>
you first check if move_uploaded_file actually able to move file or not as the directory you are moving must have write permissions otherwise move_uploaded_file fails
http://php.net/manual/en/function.move-uploaded-file.php
so must check like
if(move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"])
//file moved
else
//error

PHP iFrame Upload Confirmation

So i have a file upload feature on my site and rather than redirect to the php script page, I want to have an iframe that displays the message returned by the script. So here's my html:
<iframe class="iframe" name="my_iframe" src="upload_file.php" style="display:none;"></iframe>
<form id="uploadForm" action="upload_file.php" method="post" enctype="multipart/form-data" target="my_iframe">
So basically I want to hide my iframe UNTIL I the upload process finishes and the script returns a message. Here's my php script:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
$allowedExts = array("doc", "docx");
$extension = pathinfo( $_FILES["upload"]["name"],PATHINFO_EXTENSION);
$username = $_SESSION["username"];
if (($_FILES["upload"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["upload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
echo "Type: " . $_FILES["upload"]["type"] . "<br />";
echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";
$dir_exists = is_dir("/disks/*/*/*/*/". $_SESSION["FirstName"] ."-".$_SESSION["username"]."/");
$file_exists = file_exists("/disks/*/*/*/*/".$_SESSION["FirstName"] ."-".$_SESSION["username"]."/" . $_FILES["upload"]["name"]);
$folderName=$_SESSION["FirstName"];
$baseDir = "/disks/*/*/*/*/";
// Create directory if it does not exist
if (! $dir_exists) {
if (is_writable($baseDir)) {
mkdir($baseDir . $_SESSION["FirstName"]."-".$_SESSION["username"]);
} else {
trigger_error($baseDir. " is not writeable");
}
}
if ($file_exists) {
echo $_FILES["upload"]["name"] . " already exists. ";
} else {
$link = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***');
$proptype = $_POST["prop_cat"];
$stmt = $link->prepare("UPDATE Table SET `PType`=:proptype WHERE Username=:username");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':proptype', $proptype);
$stmt->execute();
move_uploaded_file($_FILES["upload"]["tmp_name"],
$baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]);
echo "Stored in: " . $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
So basically to sum up my question: How can I hide the iframe until upload finishes, and then display it with the message that the user can close like an alert box?
Can you echo the Iframe at the end of the upload instead of trying to hide it then show it at the top?
if ($file_exists) {
echo $_FILES["upload"]["name"] . " already exists. ";
} else {
$link = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***');
$proptype = $_POST["prop_cat"];
$stmt = $link->prepare("UPDATE Table SET `PType`=:proptype WHERE Username=:username");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':proptype', $proptype);
$stmt->execute();
move_uploaded_file($_FILES["upload"]["tmp_name"],
$baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]);
echo "Stored in: " . $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"];
/// load the iframe now ///////////////
echo '<iframe class="iframe" name="my_iframe" src="upload_file.php"></iframe>';
///////////////////////////////////////
}

File uploads empty string

Uploading an image into a directory, it seems to always try upload "" which is nothing. Rather then an actual image. What am I doing below. I know the value is "" because of what it inserts into the database.
<li class="formProductImage">
<span>Image:</span>
<input type="file" name="productImage" />
</li>
and the php:
if ($_FILES["productImage"]["error"] > 0)
{
echo "Return Code: " . $_FILES["productImage"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["productImage"]["name"] . "<br />";
echo "Type: " . $_FILES["productImage"]["type"] . "<br />";
echo "Size: " . ($_FILES["productImage"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["productImage"]["tmp_name"] . "<br />";
$filename = mysql_real_escape_string($_FILES['productImage']['name']);
$query = "UPDATE products SET image = '$filename' WHERE name = '$name'";
$result = mysql_query($query);
if (mysql_affected_rows() == 1) {
// Show thank you message
echo '<span style="color:green;">Your image was added to database correctly.</span>';
if (file_exists("uploaded/" . $_FILES["productImage"]["name"]))
{
echo $_FILES["productImage"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["productImage"]["tmp_name"],
"uploaded/" . $_FILES["productImage"]["name"]);
echo "Stored in: " . "uploaded/" . $_FILES["productImage"]["name"];
}
} else {
echo '<font color="red">Image note inserted into database.</font>';
echo mysql_error();
}
}
}
}
Make sure you have enctype="multipart/form-data" in the <form> tag.
change this line:
$filename = mysql_real_escape_string($_FILES['file']['name']);
to :
$filename = mysql_real_escape_string($_FILES['productImage']['name']);

Categories