Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML PHP
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???
//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>
<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 31-Aug-12 2:41am
Misbah1536
Edited 1-Sep-12 2:52am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
<body>
 
  <form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    
 
    <input type="submit" name="submit" value="Submit" />
  </form>
 
</body>
</html> 
 
Good luck!
  Permalink  
Comments
Misbah1 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Misbah1 at 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ő at 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 at 1-Sep-12 4:01am
   
php script is called displays messages,.. invalid file...
Zoltán Zörgő at 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 at 1-Sep-12 8:51am
   
I have updated my question . please check it..
Zoltán Zörgő at 1-Sep-12 12:00pm
   
Try a var_dump($_POST); at the beginning of the string, and post here what you see. And be sure not to have space between type and the = sign, and don't forget the name property - that's bad!
<input type="file" name="file" id="file" />
Misbah1 at 3-Sep-12 0:43am
   
Thank you so much.. file is uploaded successfully :)
Thanks a lot... :)

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



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100