Get the $_POST varaible along with the associated $_FILES - php

I have a form.When a user clicks on add a file. Two input fields are appended. One with the input name of the file to be uploaded and another with input field for the file itself
<form>
<label>File 1</label>
<input type="text" name="fileName[]"/>
<input type="file" name="file[]"/>
<label>File 2</label>
<input type="text" name="fileName[]"/>
<input type="file" name="file[]"/>
.
.
.
.
<input type="submit" value="submit"/>
How do i get the corresponding file name and the file together for insertion
Since the text field will be stored in $_POST variable and file in $_FILES

First your form needs to have the attribute enctype="multipart/form-data" like so:
<form type="post" enctype="multipart/form-data">
On submit: you can get both $_FILES and $_POST arrays.
To see what you have, try (if submitted):
echo '<pre>';
print_r($_POST);
print_r($_FILES);
echo '</pre>';
If your file field name attribute was "upload_pic", then your $_FILES array should look like:
Array
(
[upload_pic] => Array
(
[name] => name_of_file
[type] => file_type
[tmp_name] => temporary_name
[error] => 4
[size] => 0
)
)
I believe that gives you any info you need to insert to DB. (E.g. $filename = $_FILES['upload_pic']['name']), add it to the $_POST array and carry on with your insert query.
EDIT:
Re-reading your question, I see:
When a user clicks on add a file. Two input fields are appended. One
with the input name of the file to be uploaded and another with input
field for the file itself...
I was wondering why you need to manually construct a separate text input for the "fileName", since you can obtain it from the $_FILES array (as explained above).

Store what is in $_POST and what is in $_FILES in variables. then you need to open de file with fopen and fwrite the to the file the variable you retrieved from $_POST. Don't forget fclose the file when you are done.

Related

How to set php variable as value of input in html form for those input with type "file"

we can set PHP variable as the value of input in HTML form like this
<input type="number" name="price" value="<?php echo $editprice;?>" >
but this doesn't work for input with type file.
I try it this way
<?php
$sqlch15="SELECT image1 FROM pc where id=$idtoe";
$resultch15= mysqli_query($db, $sqlch15);
while ($row = mysqli_fetch_array($resultch15))
{
$editimg1 = "<img src='images/" .$row['image1']."'>";
}
?>
<input type="file" name="image1" value = "<?php echo $editimg1;?>">
but it doesn't work what is my mistake help me.
The value property contains the complete path of the file.
The value property of the input:file element is read-only, because of security reasons.
if you press that button you upload a picture into the /tmp folder of the server (or another folder designed for that)
You will create a Array variable called $_FILES['image']
[_FILES] => Array
(
[image1] => Array
(
[name] => example.png
[type] => image/png
[tmp_name] => /tmp/phpq0JHjV // that is the upload folder
[error] => 0
[size] => 10847
)
)
So the only thing that would make a little sense is to fill in the name of the file
<input type="file" name="image1" value="<?=$_FILES['image']['name'];?>">
But as I said it makes no much sense because the info about the path to this file on the client is missing.

How to carry file name to php post method

I am trying to have my code carry the name of a file that the user uploads (with below code) and after uploading, print the string "Your file thefilename.txt has been uploaded" to the screen, but the line prints with no value to thefilename.txt the line is just: "Your file has been uploaded", I tested it with a text input, and it carried the data, but not the filename, any help?
index.php:
<form action="uxV637__.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_to_upload" id="file_to_upload"><br>
<input type="submit" value="Upload"><br>
</form>
uxV637__.php:
<?php
$fname_ = $_POST['file_to_upload'];
echo "Your file ".$fname_." has been uploaded...";
echo "Text is: ".$_POST['text'];
$ip = $_SERVER['REMOTE_ADDR'];
$fname = substr(md5(microtime()),rand(0,26),5);
move_uploaded_file($_FILES["file_to_upload"]["tmp_name"], "uploaded_files/".$fname.".txt.safe");
?>
You want to use the superglobal $_FILES array, specifically, the name key of the array corresponding to your file field.
For example, in your code, the file input field is named file_to_upload, so you would access the file name using $_FILES['file_to_upload']['name'], like this:
echo "Your file " . $_FILES['file_to_upload']['name'] . " has been uploaded...";
See the explanation of the $_FILES array in the documentation.
You should use $_FILES reserved variable when you deal with file uploads, not $_POST.
You just have to replace this:
$fname_ = $_POST['file_to_upload'];
with:
$fname_ = $_FILES['file_to_upload']['name'];
Check the $_FILES reserved variable documentation:
http://php.net/manual/en/reserved.variables.files.php
$('form').on('submit', function(){
var name = $('input[type=file]').val()
$('#inputHidden').value(name)
})
Just grab in in the submit event with jquery and append it as a value in a input type hidden

Changing value inside array to be editable input field

have a small problem. In this part of code:
<?php
$data = [
"eCheckDetails"=>[
"paymentsReceived"=>$history["transactionSummary"]["eCheckTotal"],
"revenueReported"=>$history["transactionSummary"]["eCheckTotal"],
"fundsDeposited"=>$history["transactionSummary"]["eCheckTotal"],
"accountAdjustment"=>0.00],
"paymentCardDetails"=>[
"paymentsReceived"=> $history["transactionSummary"]["paymentCardTotal"],
"revenueReported"=> $history["transactionSummary"]["paymentCardTotal"],
"fundsDeposited"=> $history["transactionSummary"]["paymentCardTotal"],
"accountAdjustment"=>0]
];
data " $history [...][...]"
is taken from another file or database (its not really important from where)
Point is, that this data is sometimes incorrect, and needs to be changed manually. And this is my question. How to make this fields (where $history [..] [..] is) editable, to be
<input type="text">
(with small button ACCEPT or smg somewhere aside) with default value hidden under $history[..][..].
I tried to do it, but its inside array and didnt have any luck. Maybe someone knows?
Best regards
You can use named keys in your HTML attributes, for example <input ... name="history[transactionSummary][eCheckTotal]">. Submitting this back to the server will fill your array.
<?php
$form = <<<EOS
<form method="post" action="">
<input type="text" value="" name="history[transactionSummary][eCheckTotal]">
</form>
EOS;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
var_dump($_POST);
} else {
echo $form;
}
The content of the $_POST superglobal will be:
Array
(
[history] => Array
(
[transactionSummary] => Array
(
[eCheckTotal] => dsdsa
)
)
)

How Can I Post inpputed Array Data in PHP?

I've already tried this stuff
$item=$_POST($val['item_id'])
And
$item=$_POST[$val['item_id']]
Any Idea on how to Post my inputted data ?
$_POST isn't a function, it is a special PHP array that reflects the data submitted from a form. So, the second line you got there can work only if the $val['item_id'] has a valid post name key. You should always first check if that key actually exists in the $_POST data array by using isset function like this:
if (isset($_POST[$val['item_id']]) {
$item = $_POST[$val['item_id']];
}
To debug and see all $_POST data, use this code:
<pre><?php
print_r($_POST);
?></pre>
1) form.html
Make sure your form uses POST method.
<form action="submit.php" method="post">
<input name="say" value="Hi">
<input name="to" value="Mom">
<input type="submit" value="Submit">
</form>
2) submit.php
var_export($_POST);
Will result in:
array (
'say' => 'Hi',
'to' => 'Mom',
)
$_POST is not a function, but an array superglobal, which means you can access submitted data thus:
print $_POST['field_name']

Why does image upload fail php's is_uploaded_file check?

I have a html form that allows image uploads, but the image uploaded now fails the "is_uploaded_file" check for some reason.
So why does the upload fail the is_uploaded_file check?
HTML:
<form enctype="multipart/form-data" id="RecipeAddForm" method="post"
action="/recipes/add" accept-charset="utf-8">
<!--- Omitted Markup -->
<input type="file" name="data[Recipe][image]" value="" id="RecipeImage" />
<!--- Omitted Markup -->
</form>
PHP:
// returns false
echo is_uploaded_file($file['tmp_name'])?'true':'false';
I did a dump on the $file or $_FILES array:
Array
(
[name] => add or remove.jpg
[type] => image/jpeg
[tmp_name] => E:\\xampp\\tmp\\phpB9CB.tmp
[error] => 0
[size] => 71869
)
File size is not too large, and the error was 0. So why does it fail the is_uploaded_file check?
Might be a problem with windows, since it's case sensitive and will not match if the path is different. Try using realpath($file['tmp_name'])
try with
if (is_uploaded_file($_FILES['data[Recipe][image]']['tmp_name']))
remember $_FILES is reserve PHP superglobal variable ..so always write in capital
u can retrieve the correct path of file using
$filename = basename($_FILES['userfile']['name']);
NOTE: why u using array in the name attribute ( name="data[Recipe][image]" ) ?
if there is no specific reason then alwayz make simple code
<input type="file" name="RecipeImage" value="" id="RecipeImage" />
and check simply
if (is_uploaded_file($_FILES['RecipeImage']['tmp_name']))
Remember KISS
After you have used the uploaded image (move_uploaded_file) the function is_uploaded_file always returns false.

Categories