I am trying to create a form where a user can upload images. I'm using php for validation of this file to see whether it is an image file or not but I am getting an error
"Undefined index file.."
I can't understand what's wrong.. Please help
HTML code..
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP code...
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
else
{
echo "Invalid file";
}
?>
There were two things wrong with your handler.
1) There was a missing closing brace above your last else condition
2) The following line contained characters (dots in tmp...) that didn't belong:
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
Which was changed to:
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
Reformatted code, tested
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "<br>";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "<br>";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "<br>";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
echo "<br>";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
// error line for you to compare the error
// move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
} // this was the missing closing brace
else
{
echo "Invalid file";
}
?>
Added bonus: I added a few echo "<br>"; to seperate the fields on successful upload.
Which will appear like this:
Upload: image_test.jpg
Type: image/jpeg
Size: 26.16015625 Kb
Temp file: /tmp/phpifKd7I
Stored in: users/image_test.jpg
instead of on one line.
Related
I'm trying to allow users to upload videos to my website however I can't see where I have gone wrong. I am just trying to get them to upload to a folder first before also adding the file name to my database.
This is my form:
<form action='videoUpload.php' method='post' enctype="multipart/form-data">
<input type='hidden' name='id' value='<?php echo $row['videoID'];?>'>
<p><label>Title</label><br />
<input type='text' name='videoTitle' required value='<?php if(isset($error)){ echo $_POST['videoTitle'];}?>'></p>
<p><label>Image</label><br />
<input type="file" name='video' id="video" required value='<?php if(isset($error)){ echo $_POST['video'];}?>'></p>
<p><input type='submit' name='submit' value='Submit'></p>
and this is my php page:
<?php
require_once('../../../includes/config.php');
if(isset($_POST["submit"])) {
//collect form data
extract($_POST);
$allowedExts = array("ogg", "mp4", "wma");
$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
if ((($_FILES["video"]["type"] == "video/mp4")
|| ($_FILES["video"]["type"] == "video/ogg")
|| ($_FILES["video"]["type"] == "video/wma")
&& ($_FILES["video"]["size"] < 50000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["video"]["error"] > 0)
{
echo "Return Code: " . $_FILES["video"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["video"]["name"] . "<br />";
echo "Type: " . $_FILES["video"]["type"] . "<br />";
echo "Size: " . ($_FILES["video"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["video"]["tmp_name"] . "<br />";
if (file_exists("../videos/" . $_FILES["video"]["name"]))
{
echo $_FILES["video"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["video"]["tmp_name"],
"upload/" . $_FILES["video"]["name"]);
echo "Stored in: " . "../videos/" . $_FILES["video"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
Maybe it's something really simple that I'm but can't see where I've gone wrong
there are a ) missing on if
if(isset($_POST["submit"])) {
//collect form data
extract($_POST);
$allowedExts = array("ogg", "mp4", "wma");
$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
if ((($_FILES["video"]["type"] == "video/mp4")
|| ($_FILES["video"]["type"] == "video/ogg")
|| ($_FILES["video"]["type"] == "video/wma")
&& ($_FILES["video"]["size"] < 50000000)
&& in_array($extension, $allowedExts))){ //comma missing
if ($_FILES["video"]["error"] > 0)
{
echo "Return Code: " . $_FILES["video"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["video"]["name"] . "<br />";
echo "Type: " . $_FILES["video"]["type"] . "<br />";
echo "Size: " . ($_FILES["video"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["video"]["tmp_name"] . "<br />";
if (file_exists("../videos/" . $_FILES["video"]["name"]))
{
echo $_FILES["video"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["video"]["tmp_name"],
"upload/" . $_FILES["video"]["name"]);
echo "Stored in: " . "../videos/" . $_FILES["video"]["name"];
}
}
}else{
echo "Invalid file";
}
}
I'm a beginner in php and now doing a project in php. I want to upload images(maximum four image files only)
.I used the following code to upload images.
<?php if(isset($_POST['submit'])) {
$count=count($_FILES["images"]["name"]);
for($i=0;$i<$count;$i++) { if ((($_FILES["images"]["type"][$i] == "image/gif") || ($_FILES["images"]["type"][$i] == "image/jpeg") || ($_FILES["images"]["type"][$i] == "image/pjpeg")) && ($_FILES["images"]["size"][$i] < 100000)) {
if ($_FILES["images"]["error"][$i] > 0) { echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />"; } else { echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />"; echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />"; echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("public/images/".$_FILES["images"]["name"][$i] )) { echo "<b>".$_FILES["images"]["name"][$i] . " already exists. </b>"; } else {
move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/". $_FILES["images"]["name"][$i] );
echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />"; ?> Uploaded File:<br> <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path Invalid" > <?php } } }else { echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." , file size::: ".$_FILES["images"]["size"][$i] ; } } }?>
First: Please, learn to indent!
Your count is bad. You are counting the $_FILES['images']['name'] size, but you need to count how many $_FILES['images'] there are. So change your code like this (note the new position of [$i] keys):
<?php
if (isset($_POST['submit'])) {
$count = count($_FILES["images"]);
for ($i = 0; $i < $count; $i++) {
if ((($_FILES["images"][$i]["type"] == "image/gif") || ($_FILES["images"][$i]["type"] == "image/jpeg") || ($_FILES["images"][$i]["type"] == "image/pjpeg")) && ($_FILES["images"][$i]["size"] < 100000)) {
if ($_FILES["images"][$i]["error"] > 0) {
echo "File Error : " . $_FILES["images"][$i]["error"] . "<br />";
} else {
echo "Upload File Name: " . $_FILES["images"][$i]["name"] . "<br />";
echo "File Type: " . $_FILES["images"][$i]["type"] . "<br />";
echo "File Size: " . ($_FILES["images"][$i]["size"] / 1024) . " Kb<br />";
if (file_exists("public/images/" . $_FILES["images"][$i]["name"])) {
echo "<b>" . $_FILES["images"][$i]["name"] . " already exists. </b>";
} else {
move_uploaded_file($_FILES["images"][$i]["tmp_name"], "public/images/" . $_FILES["images"][$i]["name"]);
echo "Stored in: " . "public/images/" . $_FILES["images"][$i]["name"] . "<br />";
?> Uploaded File:
<br>
<img src="public/images/
<?php echo $_FILES["images"][$i]["name"]; ?>" alt="Image path Invalid" >
<?php
}
}
} else {
echo "Invalid file detail ::<br> file type ::" . $_FILES["images"][$i]["type"] . " , file size::: " . $_FILES["images"][$i]["size"];
}
}
}?>
This code works assuming your html is something like this:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 20000000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
i am a newbie at php and studying pdf upload . can somebody have an idea what is wrong with my code . i have been researching . thank you so much
<form action="<?php echo $_SERVER["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>
here is my form please somebody help me i am kind of confuse right now .
The problem is, you are posting on the same file which actually generates the HTML.
So when you first generate your HTML, you don't have any posted values, so the script throws this error.
try this :
<?php
if(isset($_FILES['file']){
upload_file();
}else{
?>
<form action="<?php echo $_SERVER["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>
<?php
}
function upload_file(){
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 20000000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
I have this form:
<form name="commentform" id="commentform" action="comment.php" method="post"
enctype="multipart/form-data">
Your Name:
<textarea maxlength="60" rows="1" cols="62" class="margin" name="name"
id="name"> </textarea> <br><br>
Submit Picture
<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>
This is the PHP to validate the picture (from W3Schools.com):
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I am the submitting the form to the same page, so the PHP is executed as soon as the webpage loads. How can I make it load as soon as the form is submitted? Also, this script does not seem to be working.
You need to check if your form is submitted before you process the file upload:
if ( isset($_POST['pic'])) {
//save file here.
}
EDIT: It looks like your not referring to the right POST variable - you have a file element called 'pic' in your form but you are referring to $_POST['file'] in your PHP code which will not exist.
Also: If you are starting out with PHP, (IMHO) W3Schools.com is the worse place you can be - I've seen really bad examples of how code should NOT be written in there..
<?php
if( isset( $_POST( 'submit' ) ) ){ // Check form is submitted or not
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
Add This To the Top of your page:
<?php $action = $_GET['action']; ?>
Your New Form:
<form name="commentform" id="commentform" action="comment.php?action=go" method="post" enctype="multipart/form-data">
Your Name: <textarea maxlength="60" rows="1" cols="62" class="margin" name="name" id="name"> </textarea> <br><br>
Submit Picture<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>
And the action script:
<?php
if (isset($action) && $action == 'go'){
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) {
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}else{
echo "Invalid file";
}
}
?>
I'm building a php upload script for images, and it refuses to run at all.
First off, I've got a simple post form like this:
<form action="variables.php" method="post" enctype="multipart/form-data" >
<p>Event title : <input type="text" name="name" required></p>
<p>Description : <input type="text" name="description" required></p>
<input type="file" name="file" id="file"/>
<input type="submit" value="submit">
</form>
This then feeds the submitted fields to "variables.php", which looks like this:
<?php
require("myfunctions.php");
$title = $_POST['name'];
$description =$_POST['description'];
$img = $_FILES["file"];
imgput($img);
generator($title, $description);
?>
"imgput" and "generator" are functions from "myfunctions.php", but the problem isn't in "genrator", so here's what "myfunctions.php" looks like:
<?php
function imgput($img) {
if ((($img["type"] == "image/gif")
|| ($img["type"] == "image/jpeg")
|| ($img["type"] == "image/pjpeg"))
&& ($img["size"] < 500000))
{
if ($img["error"] > 0)
{
echo "Return Code: " . $img["error"] . "<br />";
}
else
{
echo "Upload: " .$img["name"] . "<br />";
echo "Type: " . $img["type"] . "<br />";
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $img["tmp_name"] . "<br />";
if (file_exists("../uploads/" . $img["name"]))
{
echo $img["name"] . " already exists. ";
}
else
{
move_uploaded_file($img["tmp_name"],
"../uploads/" . "event1.jpg");
echo "Stored in: " . "../uploads/event1.jpg";
}
}
}
else
{
echo "Invalid file";
}
}
?>
Any help would be great. I tried running test echos right after "imgput" begins, but it won't even run it.
You're missing a ( here:
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
^^^ no matching ( available
Most likely you wanted this:
echo "Size: " . ($img["size"] / 1024) . " Kb<br />";
If you had error_reporting turned on, you'd have seen the syntax error:
PHP Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/marc/test.php on line 16