So a form like this:
<form action="/appapi/checkout/" method="post" mimetype="text/xml" enctype="text/xml" name="form1">
<input type="file" name="xmlfile">
<br />
<input type="submit" name="Submit" value="Submit">
</form>
Will pass me a xml file to appapi/checkout/
How can I read this file? Or do I need to save it on my server before I can read it?
Like it has been done here: receive xml file via post in php
I tried to:
$url = 'php://input';
$xml = simplexml_load_file($url);
But wouldnt work out. How can i do this?
I'm not specifically sure that enctype="text/xml" in the <form> element will make the browser to send in the file's content as raw input to the server. One might want to test if that's possible, but I don't know.
However, you could perform a standard file upload with PHPDocs and work on the temporary file that will be automatically created:
$xml = simplexml_load_file($_FILES['xmlfile']['tmp_name']);
This needs the form to have enctype="multipart/form-data". Also it's wise to first check if the file was successfully uploaded by looking into $_FILES['xmlfile']['error'], it is 0 when no error occured:
$upload = (object) $_FILES['xmlfile'];
$xml = $upload->error ? NULL : simplexml_load_file($upload->tmp_name);
BTW, the tempfile will be automatically removed when the PHP script finishes.
First, fix the HTML.
<form> has no mimetype attribute. Remove it.
The enctype attribute does not accept text/xml as a value replace it with multipart/form-data
The name attribute is pointless unless you need to access the form with JavaScript, in which case use an id instead.
Since you are using PHP, the file will then appear in $_FILES[] read it from there.
Related
I have a form on my website. I wanted to save to input to txt but when I submit data, the .txt file get larger (bytes increase) but no text shows up,
Here is the code
<div>
<form action="controller.php">
<input name="card" id="card" type="email">
<button type="submit" name="submit" >Submit</button>
</form>
</div>
Here is the .php code
<?php
$card = $_POST['card'];
$file = fopen ('file.txt', "a");
fwrite($file, $card . "\n");
fclose($file);
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
I have a sub domain with the same code and it works perfectly fine.
why is it not working and how do I fix?
I tried to change the id and the type
You have a very simple issue here. Let's see why by trying to do this on your file:
print_r($_POST);
You will see that after posting your form, you have no POST data. Your form is, by default (on your server or PHP configuration) sending the data not using the POST method, but the GET one (your other server probably is setup the opposite way).
To fix this, you can either change your $card variable to:
$card = $_GET['card'];
Or rather, and that would be a better option to make it more clear and avoid problems if you migrate your website on another server/PHP version, you could simply specify the method on your tag:
<form action="controller.php" method="post">
Please, don't forget to secure the data that will be written in this file, malicious users exist and if you keep your code that simple, it might be a serious security issue.
This question already has answers here:
Reading file contents on the client-side in javascript in various browsers
(5 answers)
Closed 8 years ago.
I am quite knowledgeable in CSS(3) and HTML(5), but my current project allows me to take things further. However, this is completely unknown terrain for me. In my project I have a textarea in which users can submit some XML, which I then parse with jQuery's $.parseXML method. However, I want to add the ability to upload an XML file.
I suppose the upload button would look like this:
<form name="upload-form" action="???" method="???">
<input type="file" name="upload-field">
</form>
However, I do not know what the action and method ought to look like. Because I am staying on the same page and nothing spectacular is supposed to happen, I am guessing the action-attribute can be left out? The method-attribute might be get, rather than post? (1)
And then what? I don't know how I get the data from the uploaded XML document in my jQuery's parser. (2) Also, how to check for the correct file type? Does this have to happen server-side? (3)
action contains the name of the server side script that will receive the form data. when using post, the browser does not parse the content of the input field into the url.
the server side script, e.g. in php will receive the request, do security checks and save the data.
<form name="upload-form" action="serversidescript.php" method="post">
<input type="file" name="upload-field">
<input type="submit" value="Submit">
</form>
serversidescript.php
<pre>
<?php
print_r($_REQUEST); // shows the array containing the form data
leave the method empty and use the post method. Using post for files is important because it has security features and allows you to upload more data.
since it's uploading a file you need to add the enctype, enctype="multipart/form-data" or it won't work
<form name="upload-form" action="" method="post" enctype="multipart/form-data" >
<input type="file" name="upload-field">
<input type="submit" value="Submit">
</form>
ANd then to parse the file, you need to read the xml file using jquery and then write a parser function depending on what you need to do.
//write the custome parse function
function parse(data){
//code for your parse function goes here.
// to append the file to html, I need the actual structure of the file to show you
}
//this reads the xml file using jquery
$.ajax({
url: 'name.xml', // name of file you want to parse
dataType: "xml",
success: parse, //this calls the parse function
error: function(){alert("Error: Something went wrong");}
});
I have a form where I'm posting different fields and every type of field posted seems to work except the input File type.
I'm using var_dump($_POST); and all the other fields are there but nothing in the input type file.
My form part looks like this:
<form enctype="multipart/form-data" id="ajax-form" action="index2.php" method="POST" data-ajax="true">
and works well for everything else.
If there anything that's different in the input type file?
<input type="text" id="myid" name="myid" value="" /> ..This posts value
<input id="theimage" name="theimage" type="file" /> .. does not post value
Any ideas anyone?
Files are stored in $_FILES, not $_POST
http://php.net/manual/en/reserved.variables.files.php $_FILES variable
http://www.php.net/manual/en/features.file-upload.php Manual on PHP File Uploads.
To handle the file (no error checking):
$ROOT = "/path/to/store/files";
foreach($_FILES as $file => $details)
{ // Move each file from its temp directory to $ROOT
$temp = $details['tmp_name'];
$target = $details['name'];
move_uploaded_file($temp, $ROOT.'/'.$target);
}
See also http://www.php.net/manual/en/function.move-uploaded-file.php for more examples.
Actually if you try Firefox (>13), you can get the posted value. But if you are using Chrome or Safari, you can't get the posted value. I think the it is related with the Browsers.
I'm newbie in PHP.I want to know that,I taking data by html form and a .php file.
like:
<form id="form1" name="form1" method="post" action="show.php">
<strong>Please Enter the Unique id</strong><br/><br/>
Unique id:
<!-- name of this text field is "tel" -->
<input name="id" type="text" id="id" />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</html>
Then,I used show.php file to get the 'id'.like:
$id=$_POST['id'];
Is there any way to take input by php code???
Update:
In "C" we take ant input by this way
scanf("%d",a);
is there any way to do so in PHP.I think now all you may be clear what I'm trying to say??
Thanks
Yasir Adnan.
What you are you trying to get is wrong!
HTML:- It is the communicator between the user and the browser. It displays the contents according to the user input or html code.It gets data from user or from html code.
Php :- It is the communicator between server and the browser. It has the capability of collecting from some where else other than the code like mysql data base and then uses html to display the content!
Here you are asking php to do html work which is not correct!!
the html
<input name="sb_id" type="text" id="sb_id" />
php
$id=$_POST['sb_id'];
Well, you do take the input by your php code. Your variable $id took the value of $_POST['id'] which contains the input of the textfield.
After this step you can work with the variable like any other
$id = $_POST["sb_id"]; ?
Remember that $_POST["field_name"] where field_name must be match the name attribute of your <input /> tag.
the id attribute of input tag is not sent to server inside the $_POST array. It`s typically used in client-side.
You can get data in your PHP code through GET and POST parameters. Those parameters are part of the HTTP request.
The GET parameters are in the url :
http://mywebsite.com/id=3&name=test
Then you get them using:
$id = $_GET['id'];
$name = $_GET['name'];
So you can get input data through this way when people visit the URL, call it in AJAX, or call the URL in another application (like a webservice). But no matter how it's called, it's the same for you on the PHP side.
The POST parameters are in the HTTP request, you can't pass them through the URL. You can do that by using an HTML form, or by creating the HTTP request yourself. If you are using Javascript to call your PHP code (and pass data to it), you can use AJAX to do that for example. You, in your PHP code, can get the variables this way:
$id = $_POST['id'];
$name = $_POST['name'];
If you want console-style I/O, you should probably check JavaScript/AJAX. The second one will allow you to write your own wrapper that will help you to process the input by your server "on air".
The problem is, you still need to use $_POST for AJAX. And, which is more important, it's easier (and cheaper for the server) to validate and process input by JS (and to validate and process it further on the server-side after submit).
And if the question is "how can I get the variable from the needed format?", the answer is: try using regexps/parsing the string.
Oh, btw: there IS scanf() in php, and it's called 'sscanf' ('fscanf' for files).
<form enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit" />
</form>
I tried the above,and found var_dump($_FILES); is always empty.
It only works when you upload text files or images.
UPDATE
I added method="POST" and it works.Why is POST necessary here?
MP3 file uploads should work like any other file upload, there's no discrimination by file type or extension.
Check whether your file is not larger than allowed.
PHP manual on file uploads
PHP manual on file uploads: Common pitfalls
Update: #Adhip Gupta solved it. GET seems to be the default method for a FORM, not POST as I thought. Check here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
This attribute specifies which HTTP method will be used to submit the form data set. Possible (case-insensitive) values are "get" (the default) and "post". See the section on form submission for usage information.
Did you specify the form method to be POST explicitly and try?
First thing to check is how big the files are, and what the max upload size in PHP.ini is set to.
Nothing wrong with your PHP code. And neither PHP nor the webserver know the difference between a MP3 file and other types of content.
Have you checked its not size related?
Do you know that there isn't other things between the browser and the PHP which might be filtering?
Have you tried using a wiretap (e.g. wireshark) to confirm the data is leaving the browser / getting to the server?
C.
Maybe you have missed the MAX_FILE_SIZE which should be included.
<input type="hidden" name="MAX_FILE_SIZE" value="157286400" />
You should also add action="some.php" and method="POST" to <form>
OR using http://www.uploadify.com/ with ajax