REst API for Uploading Files - php

Can anyone help me how to use this function below... This function will upload file into to my storage but unfortunately I didn't know how to use the code. i tried numerous times already but still it doesn't work. This code is not mine I got it from github and try to use it into my project.
public function upload(){
// Check that the path is a directory
if(is_file($this->path)){
$this->status = "error";
$this->message = "Path Not A Directory";
}else{
// Handle upload
$target = $this->path . "/" . basename($_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
$this->status = "success";
}else{
$this->status = "error";
$this->message = "Upload Error";
}
}
$this->respond();
}
<form action="file.php" method="get">
<input type="hidden" name="action" value="upload" readonly>
<input type="hidden" name="path" value="class/" readonly>
<input type="file" name="upload" readonly>
<!-- <input type="submit" name="create" class="btn btnsuccess btn-
sm" value="create dir/file ">
<input type="submit" name="open" class="btn btn-success btn-sm"
value="Open a file">
-->
<input type="submit" class="btn btn-success btn-sm" value="upload to specific dir">
<!-- <input type="submit" name="modify" class="btn btn-success
btn-sm" value="modify file or dir">
<input type="submit" name="delete" class="btn btn-success btn-sm"
value="delete file">
<input type="submit" name="index" class="btn btn-success btn-sm"
value="list dir">
<input type="submit" name="dublicate" class="btn btn-success btn-
sm" value="duplicate"> -->
</form>
url: http://localhost/api/archiving%20system/file.php?action=upload&path=class%2F&upload=myfile.txt
OUtput: {"status":"error","message":"Upload Error"}

You need the enctype="multipart/form-data" added to your <form> tag - and it should probably be POST for the method and not GET, and a max file size...
<form action="file.php" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="hidden" name="action" value="upload" readonly>
<input type="hidden" name="path" value="class/" readonly>
<input type="file" name="upload" readonly>
<input type="submit" class="btn btn-success btn-sm" value="upload to specific dir">
</form>

Related

PHP CALCULATOR USING BUTTONS

HTML CODE :
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">
Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button"> <br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="0" name="button" style="padding- left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
PHP CODE :
<?php
if (isset($_POST["Calculate"]))
{
echo $_POST["button"];
} ?>
I'm developing a PHP calculator, actually my question is when i click to any of the input buttons the value should display in the text box. above is the code what i have tried with, but im unable to get the result. can any one help me in my work,so that how i can get the desired result.
There is nothing like you cannot do in PHP but if you could do it using Jquery it will be much more better in terms of User experience as well. Looking at your HTML , I can see you have a input type of submit which means on each click it will submit a form unless and until you have written any condition not to submit. Also your name for each button is also same and it will make difficult to track which one is clicked/entered, please make sure you have unique name field. If you want to do it in PHP only then on every submit you can save that value in session or cookies and while loading page you can check against that session variable or cookies and can show it the the input value.
You can see following code as a reference but it is using jquery.
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="" id="formID">
Enter value: <input type="text" name="value" id='btnValue'> <br> <br>
<div style="padding-left: 105px">
<input type="button" value="9" name="btn1">
<input type="button" value="8" name="btn2">
<input type="button" value="7" name="btn3">
<input type="button" value="+" name="btn4"> <br>
<input type="button" value="6" name="btn5">
<input type="button" value="5" name="btn6">
<input type="button" value="4" name="btn7">
<input type="button" value="-" name="btn8" style="padding-left: 9px"> <br>
<input type="button" value="3" name="btn8">
<input type="button" value="2" name="btn9">
<input type="button" value="1" name="btn10">
<input type="button" value="/" name="btn11" style="padding-left: 9px"> <br>
<input type="button" value="0" name="btn12" style="padding- left:33px">
<input type="button" value="." name="btn13" style="padding-right:9px">
<input type="button" value="x" name="btn14" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
$('input[type=button]').on('click',function(){
if($('#btnValue').val()==''){
$('#btnValue').val($(this).val());
}else{
$('#btnValue').val($('#btnValue').val()+($(this).val()));
}
})
Here is the jsfiddle you can look on https://jsfiddle.net/nef1qgzn/13/
Note: I have user Jquery libraray 3.3.1

upload files only after submit

I'm trying to use dropzonejs in order to upload multiple files.
I want to upload them only when i submit the form (post the page)
how can i do that?
<?PHP
if ($_POST)
print_r($_FILES);
?>
here is my form:
<form method='post' action='index.php' enctype='multipart/form-data'>
<input type='text' name='title' value="" class="form-control" />
<textarea name="msgText" class="form-control" ></textarea>
<label class="control-label col-sm-1">Attached File:</label>
<div class="dropzone"></div>
<input type="submit" value="Submit" class="btn btn-success form-control">
</form>
Add name attribute to submit imput
<input type="submit" value="Submit" name="button" class="btn btn-success form-control">
Then in PHP:
if(isset($_POST['button'])){
...some code...
}

why cant i get value of $accept_request_id

<form method="post>
<button input type="submit" id="click" class="btn btn-info " name="<?php echo$accept_request_id; ?>"></button>
</form>
Why can't I get value of $accept_request_id? I use following code
if(isset($_POST[$accept_request_id])) {
$accept_request_id=$_POST['accept_request_id'];
}
You can use below code to get value of form. This will work
<form method="post>
<input type="hidden" name="accept_request_id" value="<?php echo $accept_request_id ?>" />
<button input type="submit" id="click" class="btn btn-info">Submit
</button>
</form>
if(isset($_POST['accept_request_id'])) {
$accept_request_id = $_POST['accept_request_id'];
}

Here variable 'pass' can't parse to $_post['action']=='edit'

<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="edit" class="btn btn-primary" value="edit"/>
<input type="submit" name="delete" class="btn btn-danger" value="Delete"/>
</div>
</form>
then
if($_POST['action']=='edit'){
echo $rcode1=$_REQUEST['pass'];
$sqlup="select * from transport_details where rcode='$rcode1'";
There is no input named action. Your current code is unreachable.
The $_POST assoc gets its keys by attribute name. E.g.
<input type="text" name="authorName" />
<?php
echo $_POST['authorName'];
?>
Your code should be like (in PHP file)
if (isset($_POST['edit'])) { ... }
OR (in HTML file)
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
You can do this in your php...
if (isset($_POST['edit'])) {
echo $rcode1=$_POST['pass'];
$sqlup="select * from transport_details where rcode='".$rcode1."'";
}
assuming your html is like this...
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
you should be able to get the value

How to deal with two submit buttons in a form?

I don't understand this,
if I do this and I click the check out button, the page won't go to the check out page,
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
It only does if I change the name="checkout" to name="cart-checkout",
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="cart-checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
It works that way but does not make any sense to me, do you know why it does it that way?
So I tried to use <a> tag inside the <button> tag and it goes to the check out page,
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
But does it a valid html to put <a> tag inside the <button> tag?
Why not change the buttons to:
<input name="update" id="update" type="submit" value="Update cart">
<input name="cart-checkout" id="checkout" type="submit" value="Check out">
Then in PHP (on cart.php) you can do:
<?php
if($_POST){
if(isset($_POST['update']){
// process update
} else if(isset($_POST['cart-checkout']){
// process cart checkout
// or uncomment the below line to forward to checkout.php
// header("Location: checkout.php");
}
}
?>
What about making one <button> that simply acts like a <a href=""> ?
-edit-
Whoops, sorry, misread. <button onclick="window.location='/nextpage';"> ?
I would actually use JavaScript here with an onclick event, which will take you to the checkout page.

Categories