I'm creating a page when user can enter how many files he want to upload then that page is rendered again with a form, after submission to another page the files are uploaded.
But i'm receiving an error [if i upload 1 file, else the problem is userfile[limit-1]]
Notice: Undefined index: userfile0 in E:\wamp\www\uploader\uploader.php on line 10
I'm pasting here my code
index.php
<html>
<?php
if (isset($_POST['num']) && !empty($_POST['num']))
{
?>
<form name="uploader" action="uploader.php" method="post">
<table>
<tr><td>Title</td><td>Select File</td><td>Description</td></tr>
<input type="text" name="number" value="<?php echo $_POST['num']; ?>"/>
<?php
for ($i=0;$i<$_POST['num'];$i++)
echo '<tr><td><input type="text" name="title'.$i.'"/></td>
<td><input type="file" id="userfile'.$i.'" name="userfile'.$i.'" size="30"></td>
<td><textarea name="desc'.$i.'" rows="4"></textarea></td></tr>';
?>
</table>
<input type="submit" name="b" value="Submit"/>
</form>
<?php
}
else
{
?>
<form name="form" method="post">
How many files to upload ? <input type="text" name="num" value="1"/>
<input type="submit" value="Submit"/>
</form>
<?php
}
?>
</html>
uploader.php
<?php
if (isset($_POST['number']))
{
for ($slot = 0; $slot < $_POST['number']; $slot++)
{
$title = $_POST["title$slot"];
$desc = $_POST["desc$slot"];
if (move_uploaded_file($_FILES["userfile$slot"]['tmp_name'],$_FILES["userfile$slot"]['name']))
echo $name.' file Uploaded !';
else
echo ' file not Uploaded ! ';
}
}
?>
Edit
SQL code removed
You need an enctype="multipart/form-data" on your <form> tag, otherwise it cannot upload files/PHP cannot read it.
You should also check the existance of a file before trying to upload it.
if (isset($_FILES['userfile'.$slot])) ...
Related
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>
All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
So, I have a form with two input elements: one to upload a file, and the other, a hidden input. Here it is:
<form action="upload.php" method="POST">
upload: <input enctype="multipart/form-data" name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
on my "upload.php" page (which is a different page), I get the undefined index error. Here's the code for that:
<?php
session_start();
if(isset($_POST["eup"])){
$fERR=false;
if(isset($_FILES["pp"])){ // undefined index error comes if this IF is removed...
$aExts = array("png");
$temp = explode(".", $_FILES["pp"]["name"]);
$tEXT = end($temp);
if ((($_FILES["pp"]["type"]=="image/x-png")
|| ($_FILES["pp"]["type"]=="image/png"))
&& ($_FILES["pp"]["size"]<300000)
&& in_array($tEXT, $aExts)){
if ($_FILES["pp"]["error"]>0){
$ppERR=true;
}
else{
// handle file upload here
}
}
else{
$fERR=true;
}
}
else{
$fERR=true;
}
if($fERR==true){
echo "ERROR";
}else{
echo "GOOD TO GO";
}
}
echo "<br />".ini_get("file_uploads");
?>
I've looked at a bunch of other posts and websites discussing this topic, but none of the solutions worked for me.
by the way, the output of that php is:
ERROR
1
<form action="upload.php" method="POST" enctype="multipart/form-data">
upload: <input name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
I have a single profile page that I want to upload a photo on as a separate action. I have the first form submitting to the page successfully, it is when I submit the photo form that the page returns blank with an empty message.
HTML
<form method="post" action="profile.php" id="main">
<input name="txtFirstName" type="text" value="<?php echo $sFirstName; ?>">
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<form method="post" action="profile.php" id="photo_upload" enctype="multipart/form-data">
<img src="<?php echo($sPath); ?>" height='100' width='100' id="imgProfile" />
<br>
<input type="file" name="fUpload" id="fUpload">
<br>
<input type="submit" name="btnUploadPhoto" value="Upload" class="cancel"/>
</form>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['btnSubmit']) {
$sFirstName = $_POST['txtFirstName'];
}
else if ($_POST['btnUploadPhoto']) {
// DO MY MOVE LOGIC
}
}
When the "btnSubmit" is called the page loads and the textbox gets the name that was entered. When the "btnUploadPhoto" is called I get this on the screen and nothing else:
{error: '', msg: '' }
How do I get the page to reload with the original form?
As you are uploading a file, you will want to check if a file has been submitted in your PHP code. Here is the code to do this:
isset($_FILES['fUpload'])