+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP file upload problem

  1. #1
    bnrj_sjy is offline x10Hosting Member bnrj_sjy is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    18

    PHP file upload problem

    I am new to php and is trying to construct a file upload script that will only allow pdf file

    i am using the following codes for my form.php

    PHP Code:
    <form action="upload.php" method="post"
    enctype="multipart/form-data">
      <
    h4>Single File Upload<br />
      </
    h4>
      <
    label for="file">Filename:</label>
    <
    input type="file" name="file" id="file" />
    <
    br /><br />

    <
    input type="submit" name="submit" value="Submit" />
    </
    form
    and the following for the upload.php
    PHP Code:
    <?php
    if (($_FILES["file"]["type"] == "application/pdf")
    && (
    $_FILES["file"]["size"] < 200000))
      {
      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";
      }
    ?>
    when i run it on localhost it shows the file uploaded successfully

    but when i run it on server it gives the message Invalid File

    please help me regarding it

  2. #2
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Re: PHP file upload problem

    Are you sure you uploaded the same file both times? I've never encountered this error, however since I'm the only one who uses file uploads on my website, I took off all of the file limits. Try taking off the first limit:
    Code:
    if (($_FILES["file"]["type"] == "application/pdf")
    && ($_FILES["file"]["size"] < 200000)){
    }
    
    and...
    
    else{
    print "Invalid file";
    }
      {
    because that's the error message you are getting. If you continue to get an error message generated by your program, delete file limit that follows. If you still want to keep your file limits, double check you have the right file types and/or sizes. Good luck!

  3. #3
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: PHP file upload problem

    I tried that code. It is ok and working fine.

    check what is the maximum upload size in the server.

    One more thing if POST size exceeds server limit then $_POST and $_FILES arrays becomes empty.
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP file upload problem

    Add debugging code to see what the server thinks certain values are ......

    PHP Code:
    <?php
    if (($_FILES["file"]["type"] == "application/pdf")
    && (
    $_FILES["file"]["size"] < 200000))
      {
      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";
     
      
    ##************   DEBUGGING CODE:  ************##
     
      
    echo  "<br />File type is : " $_FILES["file"]["type"] . "<br />File size is: " $_FILES["file"]["size"]  ;
     
     
      }
    ?>
    Also, you can test the max upload size by testing:

    PHP Code:

    $max 
    ini_get'upload_max_filesize' ) ;

    echo 
    "Max is $max
    I show it to be 32M on Chopin.
    Last edited by descalzo; 10-31-2009 at 01:39 PM.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. Server Time-Zone To Local Time-Zone
    By rockee in forum Tutorials
    Replies: 4
    Last Post: 06-02-2008, 09:58 AM
  2. help!!
    By retro-bliss in forum Free Hosting
    Replies: 25
    Last Post: 12-07-2007, 01:12 PM
  3. Replies: 8
    Last Post: 12-03-2007, 04:12 PM
  4. Uploading file problem
    By HyDr@ in forum Free Hosting
    Replies: 5
    Last Post: 08-28-2006, 12:54 PM
  5. Converting a .RMVB file
    By SEŅOR in forum Off Topic
    Replies: 7
    Last Post: 06-19-2006, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers