How to carry file name to php post method - php

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

Related

PHP (json_encode, implode) store data in a txt file

I'm trying to store data in a .txt file..
The data is already appear on my HTML page but I couldn't know how to post them in a txt file or store them in a session.
In main page:
<?php
echo implode('<br/>', $res->email);
echo json_encode($res->password);
?>'
I want to do something like below:
<?php
$login = "
EMAIL : $_POST['$res->email'];
PASSWORD: $_POST['$res->password']; ";
$path = "login.txt";
$fp = fopen($path, "a");
fwrite($fp,$login);
fclose($fp);
?>
So this $_POST['$res->email']; doesn't work with me I get in the login.txt:
EMAIL : json_encode(Array)
PASSWORD: implode('<br/>', Array)
Neither function calls nor $_POST['$res->email'] would work in string/interpolation context.
While unversed, you should assemble your text data line by line:
$login = " EMAIL : "; # string literal
$login .= implode('<br/>', $res->email); # append function/expression result
$login .= CRLF; # linebreak
$login .= " PASSWORD: "; # string literal
$login .= json_encode($res->password); # append function/expression result
$login .= CRLF; # linebreak
And instead of the oldschool fopen/fwrite, just use file_put_contents with FILE_APPEND flag.
When you use post data you recieve it in your php file. You dont send post data from a php file. With that in mind you manipulate this data with php in the following way:
If is data you recieved from post:
echo $_POST['field'];
This will show the message stored on the field variable among the posted data. But check that the field will be always a string (even though the contents may not be so)
If you want to acces dynamically a field just have in mind that it should be a string for example:
$email = "example#gmail.com";
echo $_POST[$email]
This will NOT return the posted email, but will return the contents from a variable inside Post called "example#gmail.com". Which is the same as :
echo $_POST["example#gmail.com"];
But making now a correct example. if you have this html in your webpage
<form action="/yourphp.php" method="post" target="_blank">
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
you will be able to recover the data from the input field named "email"
echo $_POST['email'];
and this will return the email inside the input.
After you have this clear, you can manipulate the data in different ways to send them to a file, but usually you will have to instantiate a handler, open a file, write content, save and close the file, all depending on your handler.

php form action handler and save it

i am trying to make a page for form action handler using php
the the problem is that i want the action to be saved on that page
here's my form on index.php
<form method="get" onSubmit="return val()" action="han.php">
<label>Name:</label>
<input type="text" id="name" name="name" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
and here's my php code on han.php
<?php
$name = $_GET['name'];
echo "Your Name is:".$name;
?>
i need that name to be saved on han.php file like this every time i submit a name:
Your Name is: Brian
Your Name is: Jack
Your Name is: Daniel
Your Name is: Bob
What you can do is to create another file for saved names for example names.txt then in your han.php you could write this to save the name each time the form is submitted:
<?php
$name = $_GET['name'];
$file = fopen("names.txt","a");
echo fwrite($file,"$name, \n");
fclose($file);
return header('Location: index.php'); // to redirect to index.php page
?>
You can save them in the current session
<?php
session_start();
if(!isset($_SESSION['names']))
$_SESSION['names'] = array();
if(isset($_GET['name']))
array_push($_SESSION['names'], $_GET['name']);
if(isset($_SESSION['names']))
foreach($_SESSION['names'] as $name)
echo $name."<br>";
?>
It is not fully clear, what you mean by save, but here are some possible solutions:
1. Every name schould be saved "for ever" (big amount of names expected):
Use a Database like MySQL (see here)
2. The current name should be saved for a period of time:
Use session variables for this case
if(!isset($_SESSION['name']) {
$_SESSION['name'] = $_GET['name'];
$_SESSION['name'] = date();
}
you can also check befor this, if a period of time has expired and overwrite the name variable.
3. One or more names should be saved for all calls of the script (from any user):
You can use a file and save the names in this file:
create and write to file
read from file OR get whole file (file_get_contents)

How to populate input type file value from database in PHP? [duplicate]

This question already has an answer here:
Echo the value inside html input type=file
(1 answer)
Closed 8 days ago.
I am writing the code for editing a form that contains an input file field. I am getting all the values pulled from database for different field types but the file type input does not show its value.
I have a code that looks like this:
<input class="picturebox" id="logo" name="userfile" value="<?php echo $discount_details->picture_name;?>" />
But actually in rendered view value attribute is null for userfile field.
How do I load the value of input type when someone is editing the form and does not want to alter the picture entered earlier by him upon edit.
you can give the value attribute to input file type
if you want to show the file content while updating form you can show it in separate tag
like:
<input type="file" /> <span><?php echo $row[column_name]?></span>
here you should consider one thing
if the use is selected new file to upload you can update the column else the use not selected any thing just updated other content without file you should update the column name with old file name.
$file = $_FILES['file']['name'];
if($file!="") {
move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
} else {
$file = $oldfile;
}
You can just make value field empty and show your old image at just up of that input field(or below).then check after submitting form, if $_POST['userfile'] is empty don't update table.picture_name.
The simple trick is that ; give an id to the tag
<input type="file" name="file" /> <span name="old" value="<?=$row[column_name]?>"><?php echo $row[column_name]?></span>
Then in PHP make it like this:
$oldfile = $_POST['old'];
$file = $_FILES['file']['name'];
if($file!="") {
move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
} else {
$file = $oldfile;
}
you can write this way
Step 1: Fetch your image in a variable. like this $pimg = $result['pimage'];
Step 2: write html code for add file. <input type="file" name=""image">
Step3: in PHP fetch the file if image upload by user. like this $pimage = $_FILES['images']['name'];
Step 4: now check if the user uploaded the file or not.
if(empty(file name)){
if yes then update image it.
}else{
if no then priviously uploaded image use it.
}

PHP upload file to web server from form. error message

I am trying to upload a file from a php form.
I have verified the target location with my ISP as being "/home/hulamyxr/public_html/POD/"
I get the below error when executing the page:
Warning: move_uploaded_file(/home/hulamyxr/public_html/POD/ 1511.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/hulamyxr/public_html/hauliers/include/capturelocal2.php on line 124
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpyp3ERS' to '/home/hulamyxr/public_html/POD/ 1511.pdf' in /home/hulamyxr/public_html/hauliers/include/capturelocal2.php on line 124
POD Successfully uploaded for delivery 1511. filename: :
My Form Code
<form enctype="multipart/form-data" method="post" action="capturelocal2.php">
<input type=file size=6 name=ref1pod id=ref1pod>
</form>
My PHP Code to upload the file
$ref1 = $_POST[ref1]; //this is the name I want the file to be
$ref1pod = $_POST[ref1pod]; // this is the name of the input field in the form
move_uploaded_file($_FILES["ref1pod"]["tmp_name"],
"/home/hulamyxr/public_html/POD/ " . ($ref1.".pdf"));
Any assistance will be greatly appreciated.
Thanks and Regards,
Ryan Smith
There is an error in your code:
You need to change your move_uploaded_file funciton. There is an extra space i think which is causing the problem:
move_uploaded_file($_FILES["ref1pod"]["tmp_name"],"/home/hulamyxr/public_html/POD/" .($ref1.".pdf"));
Also i am not sure where is the
$ref1 = $_POST[ref1]; //this is the name I want the file to be
$ref1pod = $_POST[ref1pod];
coming from .There is no such values in your form. Did you upload only the form with upload only. Also be sure to put quotes around attribute values in your form and post value.
Is ref1 and ref1pod are constants. If you din't put quotes PHP will take it as constants. If they are not constants change to:
$ref1 = $_POST['ref1']; //this is the name I want the file to be
$ref1pod = $_POST['ref1pod'];
Also in your form, put quotes:
<form enctype="multipart/form-data" method="post" action="capturelocal2.php">
<input type="file" size="6" name="ref1pod" id="ref1pod"/>
</form>
Be sure you set permissions to your upload folder .
Hope this helps you :)
Check folder names, they should be case sensitive, and also check if POD folder has 777 rights(CHMOD)
Agreed with Phil, remove the space between string and file name
"/home/hulamyxr/public_html/POD/ " . ($ref1.".pdf"));
^
|
and you can also try the following :
$ref1 = $_POST[ref1];
$file_name = $_SERVER['DOCUMENT_ROOT'] . '/POD/' . $ref1 . '.pdf';
move_uploaded_file($_FILES['ref1pod']['tmp_name'], $file_name);
Please try following code.
<?php
if(isset($_REQUEST['upload'])) {
$filename = $_FILES['ref1pod']['tmp_name'];
if (file_exists($_SERVER['DOCUMENT_ROOT']."/POD/".$_FILES["ref1pod"]["name"]))
{
echo $_FILES["ref1pod"]["name"] . " Already Exists. ";
}
else {
$path = $_SERVER['DOCUMENT_ROOT']."/POD/".$_FILES['ref1pod']['name'];
move_uploaded_file($filename,$path);
}
}
?>
<form enctype="multipart/form-data" method="post" action="">
<input type=file size=6 name=ref1pod id=ref1pod>
<input type="submit" name="upload" value="upload" />
</form>
http://patelmilap.wordpress.com/2012/01/30/php-file-upload/

How to get Value from form (symfony 1.4)

I've made complex form which is valid in action. I'd like to get value from this input to save file on server.
<input type="text"
name="produkty[pForm][1][caption]"
id="produkty_pForm_1_caption" />
I've tried something like that:
$this->form=new ProduktyForm();
if ($request->isMethod(sfRequest::POST))
{
$this->form->bind($request->getParameter('produkty'),$request->getFiles('produkty'));
if ( $this->form->isValid())
{
$file=$this->form->getValue('produkty[pForm][1][src]');
$filename='u';
$extension = $file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);
}
}
But it doesn't work.
'produkty' is the name of your form. Are you using a subform to capture an array of possible files to be input?
You could get the values of the entire form by doing this.
$form_vals = $this->form->getValues();
Then you could could see what variables you have by your output.
You'll probably be able to get the input this way.
$caption = $form_vals['pForm'][1]['caption'];
this is working fine but how to fetch the values from file attributes . i cant get the values from file input
my main form name is slide and subform is mslide
here is the my code
$this->multiSlideForm->bind($request->getParameter('slide'), $request->getFiles('slide'));
$form_vals = $this->multiSlideForm->getValues();
echo $form_vals['mslide'][0]['slide_name']; //working
echo $this->multiSlideForm->getValue('[mslide][0][file_name]')->getOriginalName(); //not working

Categories