Click here to Skip to main content
15,886,199 members

Problem to Upload file in php

Misbah1 asked:

Open original thread
Hi,

I am using following code to upload and save file to server in php. But file is not uploading.. there is no error in my code.. kindly guide me where i m wrong???
PHP
//file upload
<?php
//*******************************************
//           U P L O A D  F I L E
//*******************************************

$allowedExts ="ppt";

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

if (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"],
              "myphp/MyPP/" . $_FILES["file"]["name"]);
           echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
    }
  }
else
  {
      echo "Invalid file";
  }

?>
.....

html code

HTML
<html>
<head>
</head>
<body>
<form  method="post" enctype="multipart/form-data">

Select Presentation  
<input type="hidden" name="upload" value="1" />
<input type ="file" id="file" />
<input type="submit" value="Upload" />


</form>
</body>
</html>


//*********************
// output
//*********************
when i load the php page... following messages are displayed...


( ! ) Notice: Undefined index: file in C:\wamp\www\fileUpload.php on line 9
Call Stack
# Time Memory Function Location
1 0.0005 375624 {main}( ) ..\fileUpload.php: 0


( ! ) Warning: in_array() expects parameter 2 to be array, string given in C:\wamp\www\fileUpload.php on line 11
Call Stack
# Time Memory Function Location
1 0.0005 375624 {main}( ) ..\fileUpload.php: 0
2 0.0006 376008 in_array ( ) ..\fileUpload.php:11
Invalid file

and on upload event this behavior does not change....
Tags: HTML, PHP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900