SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    May 2014
    Posts
    7
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    php upload file not working

    Hi all,

    I'm a newbie here. I've tried to run a simple file upload. The problem is the browser keep on showing the php script when i press submit button. Anyway below are two files i'm working on

    1. test.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>
    2. upload_file.php
    <?php
    if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 10240) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }

    ?>
    By the way, i'm using XAMPP in windows 7. Any help would be much appreciated.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Hi @marie.jane ; and welcome to the forum.

    There were errors in your script which I have removed.

    Try this:

    test.html
    Code:
    <!doctype html>
    <html>
    <head>
     <title>Test upload</title>
    </head>
    <body>
      <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <div>
          <label for="file" filename </label>
          <input type="file" name="file" id="file" />
          <br>
         <input type="submit" name="submit" value="submit" />
      </div>    
    </form>
    </body>
    </html>
    upload_file.php
    Code:
    <?php
    if ($_FILES["file"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    
    
    } else {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: "   . $_FILES["file"]["type"] . "<br>";
      echo "Size: "   .($_FILES["file"]["size"] / 10240) . " kB<br>";
      echo "Stored: " . $_FILES["file"]["tmp_name"];
      
    }
    Last edited by John_Betong; May 27, 2014 at 23:53. Reason: formatting

  3. #3
    SitePoint Member
    Join Date
    May 2014
    Posts
    7
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi @John_Betong,

    thanks for your reply. I have tried to use your code, however the upload_file.php code is not being executed. Instead, raw php code is displayed on the browser. see below photo.
    Untitled.jpg

    I sure that both html form and php file is saved at c:/xampp/htdocs

    Please could it be any other problem?

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Hi @marie.jane ;

    I tested the script both on the localhost and also uploaded the files here:

    http://www.johns-jokes.com/downloads...jane/test.html

    Assuming the test.html displays, allows browsing, file selection then I think the upload_file.php is corrupt.

    Your screen dump is still not available - "Attachments Pending Approval"

    Have you tried directly calling the upload_file.php from the browser? It should display something similar:

    Code:
    Notice:  Undefined index: file in /home/john/htdocs/test/upload_file.php on line 2
    
    Notice:  Undefined index: file in /home/john/htdocs/test/upload_file.php on line 8
    Upload: 
    
    Notice:  Undefined index: file in /home/john/htdocs/test/upload_file.php on line 9
    Type: 
    
    Notice:  Undefined index: file in /home/john/htdocs/test/upload_file.php on line 10
    Size: 0 kB
    
    Notice:  Undefined index: file in /home/john/htdocs/test/upload_file.php on line 11
    Stored:
    If there is a problem with the file I suggest copying and pasting the contents into a new Notepad text and overwriting upload_file.php. This would eliminate any BOM file conflicts,

  5. #5
    SitePoint Member
    Join Date
    May 2014
    Posts
    7
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi @John_Betong,

    The tested link from your link is working. but it's not working when i'm tested it locally

    I've tried calling upload_file.php from the browser and it's similar to your reply. I've also overwriting the existing upload_file.php using new notepad tect. unfortunately the problem still persist. anyway, i attached another screen dump
    Untitled.jpg

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Hi @marie.jane ;

    Php files will not run on Windows.

    Either Wamp or Xampp server must be installed. Both are free, I prefer Xampp because the installation is easier.
    Last edited by John_Betong; May 28, 2014 at 03:20. Reason: spelling: not my fortay

  7. #7
    SitePoint Member
    Join Date
    May 2014
    Posts
    7
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yes, i'm currently using Xampp. I can execute simple php using echo or print statement. it's just that the problem occurs when i'm trying to make a form handling where the browser only shows raw php code instead of handling the data

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    The screen dumps supplied show the files being called from the browser. HTML files will display OK but not php files.

    Have you tried calling the files from your localhost server?

    Http://localhost/test.HTML

    Please note that the above should all be lowercase. This tablet has a mind of its own

  9. #9
    SitePoint Member
    Join Date
    May 2014
    Posts
    7
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ahha..

    now i manage to find the problem. I've tried calling from the local host server and guess what?? it works!!

    Many thanks for your guidance..

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    I am pleased that it is working ok and many thanks for letting us know.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •