PHP and HTML Undefined Index on file upload - php

Here is my HTML Code
<HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>
<H1>Upload a File</H1>
<FORM METHOD="POST" ACTION="PHP3.php">
<strong>File to Upload:</strong><br>
<INPUT TYPE="file" NAME="txt1" SIZE="50">
<P><INPUT TYPE="submit" NAME="submit" VALUE="Upload File"></P>
</FORM>
</BODY>
</HTML>
And here is my PHP Code
if ($_FILES['txt1'] != '')
{
mkdir("C:/xampp/CIS64/"); //Creates the CIS64 directory
$filename = "C:/xampp/CIS64/"; //Location of where the file will be
copy($_FILES['txt1']['tmp_name'], $filename.$_FILES['txt1']['name']) or die("Couldn't copy the file."); //Copies the uploaded file to the CIS64 directory
}
else
{
die("No input file specified"); //If the file doesn't open, close the program.
}
For some reason I get the error: "Undefined index: txt1 in C:\xampp\htdocs\PHP3.php on line 11"
It was working before and all of a sudden it stopped working. What is wrong with my code?

Check with
if (!empty($_FILES['txt1']))
because at the time when form is not posted there would be nothing like $_POST['txt1']
and <FORM METHOD="POST" ACTION="PHP3.php"> should be <FORM METHOD="POST" ACTION="PHP3.php" enctype="multipart-formdata"> for file uploading.

form must have attribute 'enctype=multipart/form-data'
Example: <form action=PHP3.php method=post enctype=multipart/form-data>
add strings to your "PHP3.php" such as var_dump($_POST); and var_dump($_FILES); and check what is inside - helps you to debug!
Location to save I set using $location = $_SERVER['DOCUMENT_ROOT'] . '/myuploaddir/' . $filename;

Related

Trouble accessing HTML form data using PHP

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"];
?>

php upload image get undefined index for $_FILE['file'], also submit button is not detected as isset

I have have 2 files. one for image upload. one for evaluating.
1st file:
<html>
<head><title>Upload File</title></head>
<body>
<h2>Upload File</h2>
<form method="post" action="Assignment7_backend.php" enctype="multipart/formĀ­data"><input type='hidden' name='MAX_FILE_SIZE' value='50000'>
Your file: <input type="file" name="image">
<br/>
<input type="submit" value="Send it!">
</form>
</body>
</html>
2nd file:
<html>
<head><title>Upload File</title></head>
<body>
<h2>Upload File now</h2>
<?php
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
if($_FILES['image']['error'] != UPLOAD_ERR_OK) {
print "<p>File not uploaded successfully!</p>";
print "<p><a href='Assignment7_upload.php'>Try Uploading Again</a> </p>" ;
} else {
move_uploaded_file($_FILES['image']['tmp_name'], "./images/" . $_FILES['image']['name'] )
or die("can't move file");
print "<p>Success!</p>";
}
} else {
print("<p>File is not uploaded</p>");
print("<p>".$_FILES['image']['error']."</p>");
if (!isset($_POST['submit'])) {
print("<p>cannot even detect submit</p>");
}
}
?>
</body>
</html>
I was hoping to upload the file, but after I choose the file and submit, it shows error: $_FILE['image'] index is undefined. also, $_POST['submit'] is not set. what is wrong with my code?
The enctype you posted here is wrong. So the form fails to submit any image/file.
I don't know if this is a typo or you actually have it wrong in your code.
The correct one is: enctype="multipart/form-data"

HTML form for file upload won't return file name

I have a simple HTML file with a form to upload a file and PHP code to process the uploaded file. I've tried a bunch of input types and they all work except for input type="file". Nothing gets displayed. The code is attached.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload:
<input type="file" name="test.f" id="test.f">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
The processing php code is:
<?php
echo "Hello world";
echo $_FILES["test.f"];
?>
you should call the file by name if it does not work please change the file name in your form
name="test"
$_FILES["test"]['name'];

File Upload php

So I am trying to upload files to the database through php here is the code
HTML:
<!doctype html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="tag"/>
<input type="file" name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
php:
<?php
$target = "files/";
$target = $target . basename( $_FILES['file']['name']);
$name=$_POST['tag'];
$file=($_FILES['file']['name']);
mysql_connect("localhost","Bluesir9","Bluesir9","website");
mysql_query("Insert into files values('$name','$file');");
if(move_uploaded_file($_FILES['file']['tmp_name'],$target))
{
echo "The File ".basename($_FILES['file']['name'])."has been uploaded";
}
else
{
echo "Sorry there was an error";
}
?>
when I check the target folder the files are there, but I cant find the files on the database returns an empty result set when I query it through phpMyAdmin.
What am I doing wrong?
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert query.
Please check syntax of insert query
mysql_query("Insert into files values('$name','$file')");

Upload mp3 to Server PHP

I am trying to upload a small mp3 through a simple html form using PHP.
Here is my html:
<html>
<head></head>
<body>
<form action="upload_mp3.php" method="post" enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit"/>
</form>
</body>
</html>
Here is my php:
<?php
$mp3 = ($_FILES['mp3']['name']);
$target = "mp3/";
$target = $target . basename( $_FILES['mp3']['name']);
if(move_uploaded_file($_FILES['mp3']['tmp_name'], $target))
{ header("Status: 200"); }
else
{ echo "no";}
?>
Does anything look wrong with the code. Also could it be that my temp file isn't writable? If so can I get some instructions on how to make it writable.
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="upload_mp3.php" method="POST" enctype="multipart/form-data">

Categories