Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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....
Posted
Updated 1-Sep-12 2:52am
v5

Have another look at the w3cschool page you got this from. You also need the html form which is the first part of the code.

http://www.w3schools.com/php/php_file_upload.asp[^]
HTML
<html>
<body>

  <form action="upload_file.php" 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>

</body>
</html> 


Good luck!
 
Share this answer
 
Comments
Misbah1 1-Sep-12 1:18am    
The same code from same link i.e.; w3school , I used. both html and php code are written on the same file and the file is saved in 'www' folder of the wamp server. But dn't know what is the problem?
You have the action attribute missing from the form. That's not a problem, if the handler on server side is the same that generated the form. But it looks like you have two separated files. So use the action attribute to make the form send it's data to your php script.
 
Share this answer
 
Comments
Misbah1 1-Sep-12 1:19am    
I haven't give action attribute because both html and php code is written in the same file.
Zoltán Zörgő 1-Sep-12 3:03am    
Ok. How have you tried to debug it? "The file is not uploading" is quite a wide problem description. Is the php script not called; is called, but the uploaded file is not saved? Have you checked the apache or the php log files? Remember, we can not see your screen!
Misbah1 1-Sep-12 4:01am    
php script is called displays messages,.. invalid file...
Zoltán Zörgő 1-Sep-12 5:37am    
Ok, and what about sharing with us the messages displayed? Do you want us to help you or not? We don't see your screen! Copy the the relevant content from your browser window and paste here!
Misbah1 1-Sep-12 8:51am    
I have updated my question . please check it..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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