0

Sir I have following codes

<?php
 if (isset($_POST['submit']))
 {

 $allowedExts = array("gif", "jpeg", "jpg", "png");
 $temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
 || ($_FILES["file"]["type"] == "image/png"))
 && ($_FILES["file"]["size"] < 20000)
 && 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"] / 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";
   }
   ?> <img src='upload/<?php echo $_FILES["file"]["name"];?>'> <?php
   }
 ?> 
 <html>
 <body onLoad="form1.username.focus()">
 <div id="container">
 <div id="header"> 
 <img src="../images/32kdmconfig.png" height="40" width="40">Signup</div>
 <div id="content"> <form name="form1" action="<?php $_PHP_SELF ?>" method="post">
 <input type="file" name="file" id="file"><br>
 <input type="submit"  name="submit" value="Submit" id="submitphoto" >
 </form>
 </div> 
 </body>
 </html>

when I select file and press submit button then this error message appeares

Undefined index: file in C:\wamp\www\firstproject\admin\profile.php on line 6

and line 6 is

$temp = explode(".", $_FILES["file"]["name"]);

What I am doing wrong?

3
Contributors
2
Replies
12
Views
1 Year
Discussion Span
Last Post by hielo
0

Before that line check if the $_FILES array exists: if the form is submitted without appending a file, or if the upload fails (file too big, for example), then you will get that error.

Also, you can use pathinfo() to get the extension:

$ext = pathinfo($_FILES['file']['name'])['extension'];

Docs: http://php.net/manual/en/function.pathinfo.php

0

You need to fix your <form> tag to include enctype="multipart/form-data" otherwise the $_FILES array will remain empty. Also, you forgot to use echo for the action attribute. Try:

<form name="form1" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">...</form>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.