I have a problem that i just quite do not understand at all. I have this uploading script that always return Notice: Undefined index: uploadPDF in xxxxx
I've made sure that the form has the enctype="multipart/form-data" <form action="" method="POST" enctype="multipart/form-data">
The field also has the same name that i ask for in the code <input name="uploadPDF" size="100" type="file" title=""/>
When i try to echo $_POST['uploadPDF'] i actually get the filename in question. But when i try to var_dump the following $_FILES['uploadPDF']['name'] i get the undefined index error.
I really cant see what the problems is. I'm running on a inhouse IIS server.
Debug information:
This is the "debug" i try to do:
echo $_POST['uploadPDF']."<br />";
$filename = $_FILES['uploadPDF']['name'];
var_dump($filename);
echo "<br />";
var_dump($_FILES);
This is the output i get:
TEST PDF PORTAL V3.pdf
Notice: Undefined index: uploadPDF in C:\inetpub\myfolder\V4\admin\addRoutine.php on line 29
NULL
array(0) { }
When you upload file, you should use
$_FILES['file_name'] not $_POST['file_name'] that is because, the file information is stored in the $_FILES arrays, since you have named your input type to 'file'
So, I would suggest
Changing
echo $_POST['uploadPDF'];
to
echo $_FILES['uploadPDF'];
Your form as you wrote it has no action specified.
( <form action="" method="POST" enctype="multipart/form-data"> )
You need the asign "path_to_yourform.php" as your form action.
You better write it like this:
echo $_POST['uploadPDF']."<br />";
$filename = $_FILES['uploadPDF']['name'];
echo var_dump($filename)."<br />";
Well, this is quite embarrassing, one of the other guys working on this had left a <form action="" method="post"> in one of the included files in the project. Since this form tag was before my form tag $_FILES did not catch the index because of the missing enctype in the first form tag!
Related
Marking the question as duplicate is futile, cause I've referred all such questions on SO and none of them did provide a solution.
<?php
if(isset($_POST['submit'])){
$name=$_FILES['filedoc']['name'];
$temp_name=$_FILES['filedoc']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$file=file_get_contents($temp_name);
}
}
else echo"Please upload file";
echo "<form action=\"";echo htmlentities($_SERVER["PHP_SELF"]);echo "\" method=\"post\">
<h2>New Paste</h2>
<label> Upload File? <input type = \"file\" name = \"filedoc\"/></label><br><span class=\"error\">";echo $fileErr;echo"</span><br>
<input id=\"button\"class=\"red\" type =\"submit\" class=\"red\" name=\"submit\" value = \"Paste\"/><br><span class=\"error\">";echo $submitErr;echo "</span>
</form>";
}
?>
Form is being displayed correctly. So there are no errors in the second part. But then, I get this error ( ! ) Notice: Undefined index: filedoc in path of the file
form attribute enctype="multipart/form-data" is missing
The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form
Without the requirement above, the file upload will not work.
I'm using this code to process uploaded files:
mkdir("files/" . $id, 0700);
$path = "files/" . $id;
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
foreach($_FILES['attachments']['name'] as $f => $name)
{
if($_FILES['attachments']['error'][$f] == 4)
{
continue;
}
if($_FILES['attachments']['error'][$f] == 0)
{
if(move_uploaded_file($_FILES["attachments"]["tmp_name"][$f], $path.$name))
{
$count++;
}
}
}
}
$id is a random number taken from the database. Besides, I'm using this markup:
<input type="file" id="attachments" name="attachments[]" multiple="multiple" accept="*">
While the exact same code had worked brilliantly before, it now throws numbers of errors I can't really deduce:
1: mkdir(): File exists in ... on line ... (<-- now, it doesn't for granted!)
2: Undefined index: attachments in ... on line ... (well, it's defined also using form method post!)
3: Invalid argument supplied for foreach() in ... on line ... (which is quite clear as the above stated errors do prevent the foreach from doing its job correctly)
Yes, I made sure that I'm actually using POST. I also tried changing the file input's name from attachments to any other, however, scenario remains the same.
Adding enctype="multipart/form-data" has done it.
1] Check for the folder rights 0777. Weather you are able to create directory or not
2] After posting form. Make sure your form has enctype = multipart/form-data tag.
In you file please check with
echo "<pre>";
print_r($_FILES);
exit;
If you are getting any data or not? If getting data then move ahead.
First check if your $id really contain something, secondly your form should have attribute of enctype = multipart/form-data for using input type file.
<form action="test.php" method="post" enctype="multipart/form-data">
Now in your case you will get the array of files you before your perform any work, see print_r of attachments:
echo "<pre>";
print_r($_FILES);
exit;
I wrote a simple php script that basically echos the values put into a form on the page, later, I will have this write to a DB but I was working on troubleshooting it before I did that since I keep getting this warning.
Does anyone know why I am getting it? If I just fill in the fields and submit the form, the script works fine and the warning disappears.
PHP Function:
function quickEntry()
{
$subTitle = $_POST['subTitle'];
$subDetails = $_POST['details']; //This is line 13 in my code
echo "$subTitle";
echo "<br>$subDetails";
}
HTML / PHP Code:
<form method="post" action="">
<hr>
<h1>Quick Entry:<p></h1>
Subject Title:<br> <input type="text" name="subTitle"><br><br>
Subject Details: <p><textarea name="details" placeholder="Enter Details here..."></textarea><p><br>
<input type="submit" name="QuickEntrySubmit" value="Submit Quick Entry" /><br>
</form>
<?php
if (isset($_POST['QuickEntrySubmit']))
{
quickEntry();
}
?>
I know that I could disable warnings and I wouldn't see this, but I really just want to know why php is throwing the warning so I can fix the syntax appropriately and keep my code clean going forward. Full warning is:
Notice: Undefined index: details in C:\xampp\htdocs\test1.php on line 13
Thanks!
The reason why you are getting that error is because you are not checking whether the 'subTitle' and 'details' inputs have values in them.
Your code should work well like this:
function quickEntry(){
$subTitle = isset($_POST['subTitle'])? $_POST['subTitle']: null;
$subDetails = isset($_POST['details'])? $_POST['details']: null ; //This is line 13 in my code
if(!is_null($subTitle) && !is_null($subDetails)){
echo "$subTitle";
echo "<br>$subDetails";
} else{
//blah blah blah
}
You get the warnings because the $_POST variables with the indexes that you're checking for ($_POST['subTitle'] & $_POST['details']) aren't set, so the variables are empty as you reference something that isn't there.
You should do a check to ensure they are set first before trying to assign them to a variable:
$subTitle = (isset($_POST['subTitle']) && !empty($_POST['subTitle'])) ? $_POST['subTitle'] : null;
$subDetails = (isset($_POST['details']) && !empty($_POST['details'])) ? $_POST['details'] : null;
The above code will check to ensure the post index isset() and isn't empty() before assigning it to the variables.
NOTE
The variables are set when you submit the form because you are actually posting the data to the script.
You see the input attribute name? When you submit the form, they are the relevant $_POST indexes.
Just remember to ensure that the values aren't empty if you intend to print them to the markup.
I am learning PHP from a book called PHP and MySQL web development. I am a newbie to PHP , I am pretty well versed with C and HTML, I find syntax of PHP to be pretty same of C.
Look at the code:
<html>
<form action = "processorder.php" method = "post">
<p>tires:<input type=”text” name=”tireqty” size=”3” maxlength=”3” /></p>
<input type=”submit” value=”Submit Order” /></td>
</form>
</html>
This is the HTML code now I will type in the php code and save it as processorder.php
<?php
$tireqty = $_POST[‘tireqty’];
echo "<p>Your order is as follows: </p>";
$tireqty." tires<br />";
?>
After doing this , i go to the html page where I have to enter the value for tires and click submit after doing this it redirects to me mu processorder php page where my raw php code gets displayed and not the proper output which is:
Your order is as follows:
2 tires
But i get a different o/p which is as shown below:
$tireqty = $_POST[‘tireqty’];
echo "<p>Your order is as follows: </p>";
$tireqty." tires<br />";
what is going on here what is wrong??? and i also get an error message is processorder script saying that:
parse error: undefined index 'tireqty' in line something and undefined var tireqty
Hey guys i dont know whats wrong but cant post comments that button is not working or my browser is not enabling me to write comments, I am answering to all the calls here:
Yeah I am using XAMPP what is wrong in that, what should i do?
I copied your codes and yes there are no errors in it but when i go to html page and click submit the browser opens a dialog box saying that "I have chosen to open process.php" and what should it do with it so i select open with gedit because that is the only default option available there, and it redirects to my php script page :(:(:(
WHat is this it is so tough, Is PHP really that difficult or am i working on a poor platform and learning from a bad book?
Is there any good book on php
<?php
$tireqty = $_POST[‘tireqty’];
echo "<p>Your order is as follows: </p>";
$tireqty." tires<br />";
?>
You closed your echo tags in the first 2nd row already, therefor you need to add another echo tag to the 3rde line.
<?php
$tireqty = $_POST[‘tireqty’];
echo "<p>Your order is as follows: </p>";
echo $tireqty." tires<br />";
?>
Always make sure that your variables have been set before you use them:
<?php
if(isset($_POST['tireqty'])){
$tireqty = $_POST['tireqty'];
echo "<p>Your order is as follows: <br />";
echo $tireqty." tires.</p>";
}
?>
Also, your HTML had a random closing table column tag in it:
<html>
<form action = "processorder.php" method = "post">
<p>tires:<input type="text" name="tireqty" size="3" maxlength="3" /></p>
<input type="submit" value="Submit Order" />
</form>
</html>
Sounds like your PHP environment is not working as it should. Do you have a web server with PHP running or XAMP (http://www.apachefriends.org/en/xampp.html)?
Always use
if ( isset($_POST['var']) ) { ... }
Same with $_GET, $_SESSION etc. You variable tireqty isn't initialized until form is submitted, that's why throws errors
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/