$_FILES is not working in PUT and DELETE - php

I am writing API for my mobile app. I have a problem in PUT and DELETE. In post method , I can use $_FILES for uploading file. However, it return NULL value in PUT and DELETE method.
I just use simple code for testing
<?php
echo "FILE: ";
var_dump($_FILES["file"]["name"]);
?>
I am using POSTMAN chrome extension for testing the PUT and DELETE method.

Are You using Your form with enctype="multipart/form-data" attribute?
There is almost the same problem thread - check it: How to receive a file via HTTP PUT with PHP

Related

How to pass data to Advanced REST client chrome extension

I have a simple php file which just echoes $_POST['email']. I want to access it through rest client extension.
New extension looks totally different. This answer uses old extension.
I just added a check isset($_POST['email']). It returns else part data.
How do I post data with new extension?
Here's how to setup the request:
Since you're accessing email in $_POST you'll need to choose the POST method
Set the Content-Type to application/x-www-form-urlencoded
Add the data form entries (in your case email=emailvalue)
Then your PHP script will be able to read the value of $_POST['email']

Deleting a file on server by DELETE form method

I am trying to delete a file on my server using DELETE method in my html form.
I have a file ,and url to that file is :
http://example.com/file2.php
I am using the following code for deleting the file :
<form action="file2.php"method="DELETE">
<input type="submit"value="delete file">
</form>
When I press "delete file" button I get redirected to "file2.php" but it is not deleting the file.
Server is receving the request header in GET method :
GET /file2.php HTTP/1.1
instead of DELETE :
DELETE /file2.php HTTP/1.1
Does someone know why the delete method is not working? or how can I delete my file using DELETE method?
Most browser do not support DELETE as method parameter for <form ...>. Indeed it's not even part of the HTML specification:
The method and formmethod content attributes are enumerated attributes with the following keywords and states:
The keyword get, mapping to the state GET, indicating the HTTP GET method.
The keyword post, mapping to the state POST, indicating the HTTP POST method.
And here is some kind of explanation:
It seems that we currently do not understand how PUT and DELETE will be useful for HTML forms.
For DELETE, it's indeed easy to create a useful request. However, server implementations usually respond with 200 and a minimal response body ("deleted") or 204 (no content). So it's not clear how this can be used in a web application.
For PUT, it seems there's no real use case as long as the web page doesn't have full control over the payload, and also can set the content type.
Please consider removing this feature until there's a clearer understanding about what it's good for.
Frameworks like laravel provide method spoofing:
<input type='_method' value='DELETE' />
This will override the method of the request and will - for example - call a destroy function:
Route::delete('/items/{id}', 'ItemController#destroy');
If you don't use a framework, you can try to mimic this behaviour:
if($_SERVER['REQUEST_METHOD'] === 'DELETE' || (isset($_REQUEST['_method'] && $_REQUEST['_method'] === 'delete') {
...
}
Mozilla only specifies only GET and POST
I have 2 recommendations:
Use a POST to send data to your PHP code and handle it from there
Don't actually delete stuff that simple. Create some function that will just say it is deleted till after you are really sure it has to be gone

$_GET not return any value in php

Hi I am facing a problem with respect to $_GET in PHP . I am trying to do payment gateway Integration with EBS. The problem is I am not able to read response I get from URL.
My code to read response is:
$RR=$_GET["DR"];
echo $RR;
$DR = preg_replace("/\s/","+",$_GET["DR"]);
When I try to check the value using echo command it returns null. Can some one please help me understand if I need to follow some other process. Also , this works well in WAMP Server I have which uses PHP 5.4.12 however this code is not working when I am hosting it to different server bought from a vendor. PHP version used there is 5.3.
The URL I have is
http://companyururl.com/response.php?DR=IXc9laP5EPzkG8rJUEkT9GPYZKb+340d1KINeq1DJAbrqc5GeRs3RVwRJ7YShbNZUyaxTmSW46lexsfKVHpZGaEckYB8kpxGvDoGUG9XULC1ZM6XhHu125hgs12+1Ql5jETUw9t8LKV32SFu+2e9n2eDeWfaVS23HN6kQFdC+0KFK8/QdCeEXY1DBNUMX2/1eUCJrLX16tG038+FnvqxqWy6U4nGM7dcwNPnq0PMioKTQs3yXTX3RaVhC83LSbeniKgkYcEBcpZsGMetiadcjQF9qYNNwzL0FgBuah7z+MgIaz1KEDx7/HJ3T8xdunrx8CeGq19oOSp5+lDqFGZZfz8Vb+tCJ4lVzfYoy+037m3jqFtkee4vHmZEkEl2Rl68PZ9shBoLH8iZaO4imURPT53+Kgm3nwFITwyztV1tk/NlG+Y9w3kKV7hlrem/c0SsqLAomL8WORidK0AXxwpitLLuXXWt5bLs14xjVTHfVjRrluSXAg5ylE5M7wt8XV6O1aniKoeapRkKOYyo2BArkIk+92SRWzA24Wb3r+fN5lpyb9aCwOWCi2Of2tikMc/XotK9X5lxix6x0Ec4YRCV2m5f/x7xRDktcKZeJuLOVyxn3Zdx5JzdAseuc/P2vzH8xDCJ6GVcvD+7eeyv1+ZqCnapQmyAWyaCZJ0cQG+YZfKkd30IE5UMRquym0KqrOvrCJZtV4F4T+n8wPq1U4V2j57hfnTp3WzF/l3rjNizCW68Akkmk9d17sVKkGg=
I am trying to read DR value in the form .Looking for a reply.
Check weather request contains data or not by using
var_dump($_REQUEST);
and then check using
var_dump($_GET);
var_dump($_POST);
if you are get null in get and data in post it means you have to check form method from get to post as post is default method for sending data. change it to get method. like this one:
<form action="" method="get">
....
</form>

php $_FILES empty

I got a file upload form using enctype=multipart/form-data, but my $_FILES array is always empty. max_upload_size is big enough (2MB, I'm testing with 100kb files). the exactly same code is working on other hosts, so I think something is wrong with my webhoster's php settings. does anyone know what I could check for? when I ask them they always tell me it can't be their fault -_-
EDIT:
You asked for code, I didn't post it because it's pretty complicated (a Shopware plugin), but I think I can extract the important parts:
HTML (Smarty):
<form method="post" action="{url action=saveRegister}" enctype="multipart/form-data">
PHP (inside my event handler):
file_put_contents('ul.log',serialize($_FILES));
Result (var_dumped unserialized):
array(0) {}
Full Form HTML: http://pastebin.com/Wq1yBTux
Found the error: some "security feature" of the framework "cleaned up" $_POST, so move_uploaded_file couldn't check if the file was uploaded correctly
Install Firebug (see network tab) or Livehttpheaders to see if your file is actually sent to the server and if the HTTP headers are correctly send by the browser.
They might be wrong for any reason (Content-Type not allowed, POST method not allowed)
It should give you data to look at and configuration parameters to check.

Strange behaviour when trying to send data with ajax (jQuery)

I'm coding an website which must have same functionality as SO. My server use Litespeed, which increase the speed of runing php files.
When I'm trying to send php code in regular form, everything seems to be ok, data being sent to the action file without interpreting the code inside the variable. But when I'm trying to send the same data in comments textareas, with jQuery $.post method, I'm getting Forbidden 403 error. Data is blocked on the road by Litespeed server. I don't know why is happening this, why is $_POST data auto-evaluated when using ajax?
This situation doesn't appear everytime, but just in some cases, for example:
this is a new message which presents this php code: <?php mysql_query("SELECT * FROM my_table"); ?>
Note that if I remove <?php and ?> from my message, it is ok. I've already tried to use encodeURI() method but this don't change anything.
Is there any setting which must be applied in .htaccess for not auto-evaluate $_POST data?
or I should replace the open and close tags, before sending the comment?
Later edit
This is how I managed to resolve this right now, but I'm still waiting for a suggestion with .htaccess:
$but.prev('textarea').val().replace(/(<\?php)/gi,'-?php ').replace(/(<\?)/gi,'-? ').replace(/\?>/gi,' ?-');
and on the server side
str_replace(array('-?php','?-','-?'), array('`<?php','?>`','`<?'), $_POST['comment']);

Categories