I am using this form as a file upload form(as part of a larger php script, $pk is sanitized in the actual thing):
<?php
if (isset($_GET["pk"]))
{ $pk = $_GET["pk"];}
echo '<form action="up.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="hidden" name="pk" value="$pk">
<br />
<input type="submit" name="submit" value="Submit" />
</form>';
?>
I am using the following(extremely trimmed) code to handle the upload. Any syntax errors are a result of the tabifier I used.
<?php
if (isset($_GET["pk"])) {
$pk = $_GET["pk"];
}
$con = mysqli_connect("localhost","x","x", "x");
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],
"./" . $_FILES["file"]["name"]);
echo "Stored in: " . "./" . $_FILES["file"]["name"];
$fileQuery = "INSERT INTO FILES VALUES (?, ?)";
if ($fileInsert = $con->prepare($fileQuery)) {
$fileInsert->bind_param("ss", $pk, $_FILES["file"]["name"]);
$fileInsert->execute();
$fileInsert->close();
} else {
print_r($con->error);
}
?>
What I would like to know, is how do I access $pk. Is it already passed to the handling code with the form?
Suggestion: $pk is not set.
So try to use $_REQUEST['pk'] instead of _GET
Also, the single quotes in your echo won't evaluate $pk. Alter
input type="hidden" name="pk" value="$pk"
to
input type="hidden" name="pk" value="' . $pk . '"
Cheers, T.
$_POST['pk']
but you'll see $pk I guess, because you're using single quotes to echo the string.
Related
I am using the following html form code to allow the user to select multiple files for upload.
<form action="uploadFiles.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]" multiple /><br>
<input type="submit" name="submit" value="submit" />
</form>
uploadFiles.php has the following code.
<?php
echo "uploadFiles.php" . "<br>";
print_r($_POST);
if(isset($_POST['submit']))
{
echo "Post submit" . "<br>";
if ($_FILES["file"]["error"][0] > 0)
{
echo "Error: " . $_FILES["file"]["error"][0] . "<br>";
}
else
{
echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>";
echo "Upload: " . $_FILES["file"]["name"][0] . "<br>";
echo "Type: " . $_FILES["file"]["type"][0] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"][0];
}
}
?>
For some reason, isset($_POST['submit']) always returns false. I get the following output
uploadFiles.php
Array( )
You aren't closing the input tags. Particularly, you aren't closing the <input type="file" name="file[]" multiple> tag, so possibly it isn't being added to the form's $_POST array correctly, and is instead being added to the file array. Just a guess.
The problem appears to be related to the size of the files. If the files are 11 MB in size, the post only works with one of them. If I select both $_POST is an empty array. If the files are half that size, the post works with both with the following code.
HTML
<form action="uploadFiles.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]" multiple="multiple"/></br>
<input type="submit" name="submit[]" value="submit" />
</form>
uploadFiles.php
<?php
if(isset($_POST['submit']))
{
if ($_FILES["file[]"]["error"] > 0)
{
echo "Error: " . $_FILES["file[]"]["error"] . "<br>";
}
else
{
$numFilesUploaded=count($_FILES['file']['name']);
echo "No. files uploaded : ".$numFilesUploaded."<br><br>";
for ($inc=0; $inc<$numFilesUploaded; ++$inc){
echo "File " . $inc . ": " . $_FILES["file"]["name"][$inc] . "<br>";
echo "Upload: " . $_FILES["file"]["name"][$inc] . "<br>";
echo "Type: " . $_FILES["file"]["type"][$inc] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][$inc] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"][$inc];
echo "<br><br>";
}
}
}
?>
I have tried numerous attempts at this code, i know the html is correct, but the PHP is tricky
<html>
<body>
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploadfile);
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
?>
<h1>File upload successful!</h1>
<form method="get" action="/megan">
<input type="submit" value="Continue"/>
</form>
</body>
</html>
help me fix this
Hey try this simple code, I think this will help you, and add your code as per your requirement.
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
$dir="var/www/Megan/";
move_uploaded_file($temp,$dir.$name);
?>
To start, your assumption that the HTML is correct is wrong. File upload forms need to be method="POST"
One of your form elements needs to be a file picker:
<input type="file" name="file">
Your opening form tag needs an extra parameter:
<form method="post" enctype="multipart/form-data">
Your PHP should only run if the form has been POSTED, in which case the variable $_POST is set. So put your php code inside a
if($_POST) {
//php here
}
may be your uploading directory is readonly or write protected
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
chmod($uploadfile, 0777);
if($_FILES['file']['error']==0) {
if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile)){
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
} else {
echo "error!!";
}
} else {
echo "An error has occurred.<br/>Error Code: " . $_FILES["file"]["error"];
}
?>
Below is my html code....
<form enctype="multipart/form-data" action="some.php" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
and my some.php code...
print_R($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_POST["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$_POST RESULTS IN Array ( [file] => gcc-mlion.tar [submit] => Submit )
BUT $_FILES gives empty result.
When you try to print File array that time your Spell of "print_r" is wrong.
You write "print_R" instead of "print_r", Php is case sensitive so it's matters a lot.
You're trying to output the value of $_POST['file']['name'];. It will return an undefined index error message.
Change that line to:
echo "Upload: " . $_FILES['file']['name'] . "<br>";
That should fix the issue.
Also, here's how I'd do it:
<pre>
<?php
if(isset($_POST['submit'])) //checking if form was submitted
{
print_r($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0) //checking if error'ed
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES['file']['name'] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
?>
</pre>
<form enctype="multipart/form-data" action="" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this helps!
I'm trying to send two forms, one with enctype="multipart/form-data" for file upload, the other without. my problem is only one of them is working. the submit button wont send two form.
What I've tried:
<?php
echo '<form method="post" action="' . $PHP_SELF . '">';
....
echo '<input type="submit" name="submit" value="Submit"><br>';
echo "</form>";
echo '<form action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit"></form>';
if (isset($_FILES["file"]["name"])){
$file_name = $_FILES['file']['name'];
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES['file']['tmp_name'], '../images/' . $file_name);
}
}
?>
This is how it works in HTML. You can only send one form at a time. The only workaround is to send all your data with just one form.
Just Try With The Following :
<?php
echo '<form method="post" action="' . $PHP_SELF . '">';
....
echo '<input type="submit" name="Submit_Button_One" value="Submit"><br>';
echo '</form>';
echo '<form action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Submit_Button_Two" value="Submit"></form>';
/*First Form Submit*/
if(isset($_POST['Submit_Button_One']))
{
$dataValue = $_POST['data'];
}
/*Second Form Submit*/
if(isset($_POST['Submit_Button_Two']))
{
if(isset($_FILES["file"]["name"])){
$file_name = $_FILES['file']['name'];
if($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES['file']['tmp_name'], '../images/' . $file_name);
}
}
}
?>
I think this may help you to resolve your problem.
Note : Give Different name for submit buttons. Then it's possible to get the datas as you wish from both the forms.
I have this form:
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
And this php script:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"],
"/tmpupload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/tmpupload/" . $_FILES["file"]["name"];
}
?>
My goal is using curl from command line to upload a file.
The problem is to avoid the interaction with the button submit and choose, so the upload should me automatic.
Is there a solution?
To make your form auto submit on selecting the file change your file input to look like
<input type="file" onchange="formname.submit();" name="file" id="file" />
So make sure you give your form a "name" property and change 'formname' in the onchange to be the same as your chosen form name.