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[]" />
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 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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting ‘undefined index’ error while trying to use $_FILE in PHP
I have made a php upload form for images. It uses session variables to determine the upload directory. There are two options for the uploads, the slider dir or the side dir so I have an if statement that determines the directory. If i remove this from the form then the whole thing works ok however with it in, $_FILES seems to not be declared and returns as an undefined index error.
The code can be found is as follows:
upload.php
<?php
include("resize-class.php");
$allowedExt = array('jpg', 'jpeg', 'JPG', 'JPEG');
$tmps = explode(".", $_FILES['file']['name']);
$extension = end($tmps);
session_start();
if ($_POST['dir'] == 'side'){
$dirent = $_SESSION['sideDir'];
}
else if($_POST['dir'] == 'slider'){
$dirent = $_SESSION['sliderDIR'];
}
else{
die();
}
echo $_POST['dir'];
print_r($_FILES);
if (($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 4000000000)&& in_array($extension, $allowedExt)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
echo 'here';
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . " <br />\n";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> \n";
echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br />\n";
}
if (file_exists($dirent. $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . "already exists";
} else {
$fName = $_FILES["file"]["name"];
$tmpname = $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $dirent . $_FILES["file"]["name"]);
$number = FileCounter($dirent);
echo "Number of images in DIR: " . $number. " <br />\n ";
$number +1;
$resizeObj = new resize($dirent.$fName);
$resizeObj -> resizeImage(250, 150, 'crop');
$resizeObj -> saveImage($dirent.$number.".jpg", 100);
unlink ($dirent.$_FILES["file"]["name"]);
}
} else {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
function FileCounter($dir) {
$counter = 0;
$iterator = new DirectoryIterator(dirname($dir));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if ($fileinfo->getExtension() == "jpg") {
$counter++;
echo $counter . "\n";
}
}
}
return $counter;
}
?>
HTML FORM:
<form action="includes/upload.php" method="post">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<div class="styled-select">
<label for="dir"> Upload to:</label>
<select size="2" name="dir" multiple="yes" id="dir">
<option value="side" >Side Images</option>
<option value="slider" >Slider Images</option>
</select>
</div>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
I am guessing that there is either a really stupid error in my code that I am overlooking as I have been staring at it for an hour now or that there is something that I do not know about $_FILES and $_POST. (or possibly I have coded the form like a moron!).
Your form is missing
enctype="multipart/form-data"
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