Display the number of files uploaded by user using angular - angularjs

I am trying to create a web application by which you can add projects and any files relating to it. There is an angular table which displays the project information and I want to include one column which displays the number of files uploaded for each of the projects. The uploaded files are saved in a seperate directory. I have posted some parts of code so that you guys can get an idea about it.
<th><button type="button" id="show"># Of Files</button></th>
This is the table header for number of files which is a button.
<tr ng-repeat="project in allProjects >
<td> {{ ??? }} </td>
The above is table data to get the number of files for each project.
<input type="file" nv-file-select="" id="myFile" uploader="uploader" multiple />
<button type="button" id="file"> Upload</button>
Above is file input with a button to upload the files.
$scope.get_files = function(){
$http.post('core/engine.php?method=route', {'class':'Upload', 'function':'get_project_files', 'data':$scope.project.PROJECT_NAME})
.success(function(data){
if (data != 0) $scope.fileList = data;
})
}
Above is the code to upload the files.
static function get_project_files(){
if(!file_exists(self::$root . DIRECTORY_SEPARATOR . self::$data)){
mkdir(self::$root . DIRECTORY_SEPARATOR . self::$data);
} else {
return array_diff(scandir(self::$root . DIRECTORY_SEPARATOR . self::$data), array('..', '.'));
}
}
This makes a directory and saves the uploaded file in them.
$scope.get_count = function(){
var fi = document.getElementById('file');
return fi.files.length;
}
I have tried to write this part of code and pass it to the table data but it does not work.
Guys any help with this is really appreciated and thanks in advance. Please let me know if you want to know more about this code.

In order to get number of files you can simply add a function bind it with change event in the file as follows
<input type="file" multiple (change)="chooseFile($event.target.files)" />
Create a choose file function to get the number of the files selected by the user. The following code will do the task
chooseFile(file){
console.log(file.length)
}
The file is an array containing the total number of the files selected by the user.

Related

Wordpress addon to add data to a database and then call the data

I know this question is probably going to get downvoted and I will probably get into trouble but I am hoping someone may be able to help me with my situation.
On my site I use json to download data from an external source, and then I style it beautifully.
Within the json data is an individual ID for each data set.
What I want to accomplish is to have a database where I can insert the ID and a url link.
I have created the table within the wordpress database via phpMyAdmin, but I want to create a page within the admin section where I can simply add the data in.
For displaying the json data I use a php insert addon, within that php clip i want to do a piece of code that checks the database for the id within my custom database and displays the link.
I will be honest I don't know where to start on this, even if its just a link to a source that shows me how to create an admin page and submit data to the database within wordpress dashboard.
I really appreciate any help given and like I say I know I should try harder, but when ever I do a search all I get is 100's of references to add an admin to the database manually.
Thanks,
Adam
Edit I just realized I never put any table information in my question.
The table name within wordpress is called: wp_home_tickets
within that are 3 fields: id (auto increasement), gameid (numeric) and ticketlink (text)
thanks.
For adding a custom settings page in your admin, use the Settings API https://codex.wordpress.org/Settings_API
Here is a nice tutorial using it https://deliciousbrains.com/create-wordpress-plugin-settings-page/#wp-settings-api
To fetch data from your custom table, use the wpdb class https://developer.wordpress.org/reference/classes/wpdb/. More specifically, you can use wpdb::get_results if you will have multiple rows sharing the same id https://developer.wordpress.org/reference/classes/wpdb/get_results/
Or wpdb::get_row if you will ever only have one https://developer.wordpress.org/reference/classes/wpdb/get_row/
Hope this helps you out!
For anyone wishing to see how it was done, here is how I did it.
I created a file in my theme called db_admin_menu.php and added the following to it:
<?php
function ticket_admin_menu() {
global $team_page;
add_menu_page( __( 'Tickets', 'sports-bench' ), __( 'Tickets', 'sports-bench' ), 'edit_posts', 'add_data', 'ticket_page_handler', 'dashicons-groups', 6 ) ;
}
add_action( 'admin_menu', 'ticket_admin_menu' );
function ticket_page_handler() {
$table_name = 'wp_home_tickets';
global $wpdb;
echo '<form method="POST" action="?page=add_data">
<label>Team ID: </label><input type="text" name="gameid" /><br />
<label>Ticket Link: </label><input type="text" name="ticketlink" /><br />
<input type="submit" value="submit" />
</form>';
$default = array(
'gameid' => '',
'ticketlink' => '',
);
$item = shortcode_atts( $default, $_REQUEST );
$gameid = $item['gameid'];
if ($wpdb->get_var("SELECT * FROM wp_home_tickets WHERE gameid = $gameid ")) { echo 'Ticket already exists for this game.'; goto skip; }
if (!empty($_POST)) { $wpdb->insert( $table_name, $item ); }
skip:
}
?>
I then put this code in my script that fetches and displays the json:
$matchid = $match['id'];
$ticket_url = $wpdb->get_var("SELECT ticketlink FROM wp_home_tickets WHERE gameid = '$matchid' ");
if ($ticket_url) { echo 'Get Tickets'; }
I hope someone does find it of use, i did have to use a wordpress plugin called `Insert PHP Code Snippet' by xyzscripts to be able to snippet the php to a shortcode, but that is not the purpose of this post.
Thanks again for your help.

JPG Image extension not inserting into Custom table of wp

I've created a custom table on database and I need to upload image from my template page member. In this page I've done all the codings. I'm able to insert data but stuck on the image insert. I can get the file name but unable to get the extension.
Here is my inserting code of my template file.
$tmpfile = explode(".", $_FILES['image']['name']);
$newfile = round(microtime(true)) . '.' . end($tmpfile);
move_uploaded_file($_FILES['image']['tmp_name'], ABSPATH . '/wp-content/themes/themename/inc/images/' . $newfile);
$sql = $wpdb->query($wpdb->prepare("INSERT INTO members(member_image) VALUES (%s)", $newfile));
The above code shows the field value without extension as: https://screenshots.firefox.com/w7aDRfXESHZEw39s/localhost
Here is my html form field:
<form action="" method="post">
<input type="file" name="image" class="form-control">
</form>
All other text fields are inserted but only image can't be derived with extension. I can't figure out the problem. I've also tried by adding .jpg extension but it didn't work.

How to Upload png file to database using url?

hi fellow programmers I am new in web programming, I have a task that I need to upload png files to sql database using url.
$connection = mysqli_connect ($db_host,$db_user,$db_pass);
mysqli_select_db($connection,$db_name);
$sql1 = "INSERT INTO figures (Id, Name, Image) VALUES ('".$_GET['id']."', '".$_GET['Name']."', '".$_GET['Image']."')";
mysqli_query($connection,$sql1);
Those lines used to send data , in this way I can upload VARCHAR to the database how ever I cannot upload png ,I don't know how to define file path here. (Maybe if you know easier solution for my task, could you please share it? )
Thanks in advance!!
First of all you need to set the sending form to multipart and also change the method to POST:
<form method="POST" action="uploader.php" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="text" name="id">
<input type="submit" value="Upload">
</form>
in PHP page you can get the file data using $_FILES:
// I dont know what is id here? shouldn't be the auto Primary key?
$id=$_POST['id'];
//Get name of file
$imageName=$_FILES["myfile"]["name"];
//Get the binary data of the image
$imageData= addslashes(file_get_contents($_FILES['myfile']['temp_name']));
$connection = mysqli_connect ($db_host,$db_user,$db_pass);
mysqli_select_db($connection,$db_name);
$sql1 = "INSERT INTO figures (Id, Name, ImageData) VALUES ('$id', '$imageName', '$imageData')";
mysqli_query($connection,$sql1);
Please note that the type of column imageData should be BLOB.
As you have mentioned the you are new to programming, I have to say that you have this option too to uplaod the file directly to the server and only save the url in database.

How to copy and paste the content in selenium Webdriver?

Can any one please guide me how I can copy content from one site and migrate to other side with the help of selenium WebDriver ?
Just to be clear, you want to perform an copy/paste of the website directory to another folder ?
Or do you want to parse the html contents, and save that in an external file ?
The former is not really selenium friendly.
So an simple example of what you want is this :
Website 1:
<span id="spanID"> content in here </span>
Website 2:
<form id="inputID"> [ you want content in here ] </form>
Please note the pseudo html.
What you need to do in order to make this work is..
browser.get("http://www.website1.com);
var tempElement = $("spanID");
tempElement .getText().then(function (contentOfSpan) {
console.log(contentOfSpan); // will print the content of the span... now you want to save this value somewhere in your scope.
});
/////////////////////////////////////////////////
browser.get("http://www.website2.com);
var tempElement = $("inputID");
tempElement.sendKeys(RefferenceTocontentOfSpan)

retain value of input type file in a jsp file while being dynamically generated

i have a jsp page where i have a input type file and i m allowing the user to browse files. i have one such input on jsp and the rest i m generating dynamically by javascript.
this is my jsp code:
<div id="labelContainer" style="display:inline">
<table align="center">
<tr>
<td align="left"><input type="text" id="label0" name="label0" size="15"></td>
<td align="center"><input type="file" id="filePath0" name="browsetrainset0"></td>
<td id="addlabel" ><img src="../images/add.png" title="Add Label" onclick="addLabel()"></td>
<td id="removelabel"><img src="../images/remove.png" title="Remove Label" onclick="removeLabel('labelDiv0')"></td>
</tr>
</table>
</div>
and this is my javacsritp code:
function addLabel(){
var text="";
lCount++;
text+=("<table id='labelDiv"+lCount+"' align='center'>");
text+=("<tr>");
text+=("<td align='left' style='display:none'><input type='checkbox' checked='true' name='labelchkbox'/></td>");
text+=("<td align='left' id='label'><input type='text' id='label"+lCount+"' name='label"+lCount+"' size='15'></td>");
text+=("<td align='center'id='filePath' ><input type='file' id='filePath"+lCount+"'name='browsetrainset"+lCoun t+"'></td>");
text+=("<td id='addlabel' ><img src='../images/add.png' title='Add Label' onclick='addLabel()'></td>");
text+=("<td id='removelabel'><img src='../images/remove.png' title='Remove Label' onclick=\"removeLabel('labelDiv"+lCount+"')\"></td>");
text+=("</tr>");
text+=("</table>");
document.getElementById("labelContainer").innerHTM L+=text;
}
but i m not able to retain the value of the file path i browse, on the jsp page once i click the add label and generate another input type file.
I am using IE7. Please tell me how to reatin the value of the browsed files so that i can use them further.
document.getElementById("labelContainer").innerHTM L+=text;
Never, ever use += on innerHTML. It does not do what you think.
Instead, it laboriously serialises the entire contents of the div into HTML, adds your string onto the HTML code, and then parses the whole lot back into objects.
This is slow, and loses all information that cannot be remembered in an HTML string, such as JavaScript references, event handlers (all your onclicks will stop working) and form field contents (including file uploads).
You could add all the new HTML content to a temporary container instead, then move that using DOM methods to the destination:
var div= document.createElement('div');
div.innerHTML= text;
while (div.firstChild)
document.getElementById("labelContainer").appendChild(div.firstChild);
Or you can use DOM methods to create and add the nodes directly. In your case since the new nodes are so similar to the old ones, you could clone them:
function addLabel() {
var container= document.getElementById('labelContainer');
var tables= container.getElementsByTagName('table');
var n= tables.length;
var table= tables[0].cloneNode(true);
table.id= 'labelDiv'+n;
var inputs= table.getElementsByTagName('input');
inputs[0].id=inputs[0].name= 'label'+n;
inputs[1].id=inputs[1].name= 'browsetrainset'+n;
container.appendChild(table);
}
(Care: IE has some issues with changing field names with things like radio buttons though.)
If you want to be accessible without JavaScript (generally a good idea), the usual approach is to include the maximum number of entries you might, and use script to hide the ones that aren't in use.
If I remember correctly, it is not possible to programmatically set the filepsec associated with an <input type="file"> element, as this would constitute a security risk, by allowing malicious scripts to upload files that had not been specifically chosen by the user.
The only way to set the filespec is to browse to it from the web browser.

Resources