I'm trying to require a file upload in a form, but cannot get it to work. Any ideas? I'd rather echo the php error on the page vs. a javascript popup. Thanks for taking a look:
<?php
// initialize $file1
$file1 = $_POST['file1'];
// check upload of $file1
if ($file1 == '' ) {
$error = "Please upload an image";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Require upload of an file</title>
</head>
<body>
<?php if ($error) {echo $error;} ?>
<br /><br /><br />
<form id="form1" name="form1" method="post" action="nextpage.php">
<input type="file" size="8" name="file1" />
<input name="Submit" type="Submit" />
</form>
</body>
</html>
See this tutorial it have all that you need.
To sum up:
Use enctype="multipart/form-data" and method="POST" in the <form> tag.
In PHP use $_FILES['uploadedfile']['name'] to read original name ("uploadedfile" is the name of your file input - "file1" in your example).
In PHP use $_FILES['uploadedfile']['tmp_name'] to read server side temp file name.
In PHP use $_FILES['uploadedfile']['error'] to get the error (if any) see there for possible codes.
Also see the PHP manual for more info.
In your exemple use this form instead:
<form id="form1" name="form1" method="post" action="nextpage.php" enctype="multipart/form-data">
<input type="file" size="8" name="file1" />
<input name="Submit" type="Submit" />
</form>
In "nextpage.php":
//Use $_FILES['file1'] to check the file upload...
print_r($_FILES['file1']);
Related
I am trying to upload a blob data type into a mysql database using phpmyadmin. I am using WAMP as a localhost server, but the problem is this error...
Warning: mysqli_query(): MySQL server has gone away in C:\
and this:
Warning: mysqli_query(): Error reading result set's header in C:\
The Exact Problem is that when I fill all of the required fields, then click submit, it does not inserting the data into the database and giving those errors.
The HTML code is named as index5.html and the phpcode is named as upload.php. I created a database named upload and a table named uploadfile in phpmyadmin localhost...
Any kind of help is Deeply appreciated, Thanks in Advance masters!
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtmll-
transitional.dtd">
<html xmins="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>RIP</title>
</head>
<body>
<h1> Upload Form </h1>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="text" name="uname" id="uname"
placeholder="Username" /><br /><br />
<input type="email" name="email" id="email"
placeholder="email" /><br /><br />
<input type="file" name="image" id="image" /><br /><br />
<input type="submit" name="submit" value="submit"
id="submit" />
</form>
</body>
</html>
Php code
<?php
$con=mysqli_connect("localhost","root","","upload") or
die("database not connected ");
if(isset($_POST['submit']))
{
if((empty($_POST['uname'])) || (empty ($_POST['email'])))
{
?>
<script> window.alert("Enter name & email please ..
");</script>
<?php
}
else
{
$uname=$_POST['uname'];
$email=$_POST['email'];
$image=addslashes($_FILES['image']['tmp_name']);
$imagename=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image=base64_encode($image);
$query=mysqli_query($con,"insert into
uploadfile(username,email,image,imagename)values
('$uname','$email','$image','$imagename')")
or die("Query Error ");
?>
<script> window.alert("Record saved ");
</script>
<?php
}
}
?>
I think you have to check the server side settings (the post max size, maximum execution time), depending on the size of the files you are uploading, and the database structure, the field image must be a blob.
Can someone please explain this code, i am following a very good book that was recommended to me and I have typed the code exactly as it is in the book. it displays the code instead of out, I am not sure what is wrong, the code is from a book called php solutions
<?php
// set the max upload size in bytes
$max = 51200;
if(isset($_POST['upload'])){
// define the path to the upload folder
$destination = 'C:\upload_test';
// move the file to the uplaod folder and rename it
move_uploaded_file($_FILES['image']
['tmp_name'], $destination.$_FILES['image']['setara']);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mult</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form action="" method="post" enctype="mutipar/form-data" id="uploadImage">
<p>
<label for="image">Upload image:</label>
<input type="hidden"name="MAX_FILE_SIZE" value="<?php echo $max; ?>">
<input type="file" name="image" id="image">
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload">
</p>
</form>
</body>
</html>
This is surely because you are writing PHP code in .html or .htm extension file try putting the code with .php extension file. It will resolve error.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined index: file
Hi I'm learning right now how to upload images to database, but I'm getting this error/notice
Notice: Undefined index: image in C:\xampp\htdocs\Pildibaas\index.php on line 19
Here is my index.php whole code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image upload</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
<?php
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("databaseimage") or die (mysql_error());
?>
</body>
</html>
Line 19 (this line gives error) cut out from index.php:
echo $file = $_FILES['image']['tmp_name'];
From google found that i need to change premission of tmp folder, but it allready shoud have all premissions it needs.
In tutorial he dont get this error
thank you
echo $file = $_FILES['image']['tmp_name'];
should be
if(isset($_FILES['image'])){
echo $_FILES['image']['tmp_name'];
}
This checks first if $_FILES['image'] is set. If not, this wil not be run. Therefore, you will not have an error that it is out of index.
Because you first have to submit the form before $_FILES['image'] will be set...
Also, the input tag is self closing, so your form will not be:
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
but:
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" /> <input type="submit" value="Upload" />
</form>
echo $file = $_FILES['image']['tmp_name'];
should be
echo $_FILES['image']['tmp_name'];
or
if(!empty($_FILES) && isset($_FILES['image'])){
echo $_FILES['image']['tmp_name'];
}
you can also use
print_r($_FILES);
I have the following page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
Click Browse and choose a file from your PC<br /><br />
<form method="POST" action="_URL_" enctype="multipart/form-data" name="IMGform">
<input type="file" name="image_upload"><br /><br />
<input class="button" type="Submit" value="Change Image">
<input type="hidden" name="add_image" value="true">
</form>
</body>
It simply produces nothing when I add a file to upload.
var_dump($_POST) only produces:
array(1) { ["add_image"]=> string(4) "true" }
Files are available through $_FILES, not $_POST.
See the documentation for further information.
In PHP when files are uploaded, they are accessible via the $_FILES superglobal, rather than in the $_POST superglobal. See POST Method Uploads
I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)