PHP txt/doc Upload, and make url to it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Breana
    New Member
    • Aug 2007
    • 117

    PHP txt/doc Upload, and make url to it?

    Ok, i want to make a simple upload where it accepts only txt/doc files rejects other files and saves (tru) files to a folder uploads
    .
    After display a message (upload complete) Link:
    And have it post the url to the txt/doc file they just uploaded.

    so it will pop up like uploads/myfile.txt, so they can link it in the walkthrough form.
    So far i have 2 files, addwalkthrough. php and savewalkthrough .php but i cant get it to echo the file link...

    [PHP]<form action="savewal kthrough.php" method="post"
    enctype="multip art/form-data">
    <label for="file">File name:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    <?php
    if (($_FILES["file"]["type"] == ".txt")
    || ($_FILES["file"]["type"] == ".doc")
    && ($_FILES["file"]["size"] < 20000))
    {
    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("u pload/" . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
    move_uploaded_f ile($_FILES["file"]["tmp_name"],
    "upload/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    }
    ?>[/PHP]

    What did i do wrong it keeps saying invalid file...
    .txt
  • gregerly
    Recognized Expert New Member
    • Sep 2006
    • 192

    #2
    Hi there Breana,

    I think your error is coming in the way you are using the $_FILES['type'] variable from the $_FILES superglobal. Type is actually the type of file you are uploading, not the file extention. If you wanted to check based on the file extention, you could do something like this:

    [PHP]<?
    $fileType=explo de('.',$_FILES['type']);
    $type=$fileType[1];

    //will give you the actual file extention
    echo $type;
    ?>[/PHP]

    If you want to see the different elements you have available to you, you could run the code below:

    [PHP]<?php
    foreach($_FILES as $k => $v){
    foreach ($_FILES[$k] as $kk => $vv){
    echo "$kk => $vv<br />";
    }
    }
    ?>[/PHP]

    So, your code is working properly. Congrats! It may not be doing what you want, but you should be able to tweak the "type" to get the desired result. If you have any other problems post back here.

    In looking for more information, I noticed that w3schools has almost the exact same PHP code that your using. Note the way they are using the "type", it's not a file extention, rather something like "image/gif" or text/plain.

    Greg

    Comment

    • Breana
      New Member
      • Aug 2007
      • 117

      #3
      Lol, i am using a php book my dad baught me :)
      But it dont give fils ext. or how to set them that i see anyway lol.
      I dont get this:

      [PHP]$fileType=explo de('.',$_FILES['type']);
      $type=$fileType[1];
      //will give you the actual file extention
      echo $type;[/PHP]

      I need it to check if its a .txt, .doc and upload it.
      What does that do?
      I just want to know how to get it to detext the wright files in the code above lol.

      Is it .text?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        The $_FILES[$x]['type'] variable stores the MIME type of the uploaded file.
        For text files this would return 'text/plain' and for .doc types it should return 'application/msword'.

        Check out this list of MIME types at w3schools.com

        Comment

        • gregerly
          Recognized Expert New Member
          • Sep 2006
          • 192

          #5
          Originally posted by Breana
          Lol, i am using a php book my dad baught me :)
          But it dont give fils ext. or how to set them that i see anyway lol.
          I dont get this:

          [PHP]$fileType=explo de('.',$_FILES['type']);
          $type=$fileType[1];
          //will give you the actual file extention
          echo $type;[/PHP]

          I need it to check if its a .txt, .doc and upload it.
          What does that do?
          I know this stuff can be confusing. In your original code your saying

          if $_FILES["file"]["type"] == ".txt" then do something. This is incorrect as your checking the file extention, and not the file type. The explode code I mentioned before would allow you to check the file extention. To be more accurate, your upload code should check like this:

          [PHP]<?
          if($_FILES["file"]["type"] == "applicatio n/msword" || $_FILES["file"]["type"] =="text/plain"){
          //do something
          }
          ?>[/PHP]

          You just need to change how your using the files array, and check for the proper type, instead of checking for file extention.

          Post back if you need more help. This can be a very confusing topic, and we are here to help!

          Greg

          Comment

          • Breana
            New Member
            • Aug 2007
            • 117

            #6
            Ok, i see now i used to do it like .txt in html lol.
            It works but no file was uploaded?

            Upload: testgame.txt
            Type: text/plain
            Size: 0.0625 Kb
            Temp file: /tmp/phpKacwZt
            Stored in: uploads/testgame.txt
            Time: 1.1 seconds

            Comment

            • gregerly
              Recognized Expert New Member
              • Sep 2006
              • 192

              #7
              Originally posted by Breana
              Ok, i see now i used to do it like .txt in html lol.
              It works but no file was uploaded?

              Upload: testgame.txt
              Type: text/plain
              Size: 0.0625 Kb
              Temp file: /tmp/phpKacwZt
              Stored in: uploads/testgame.txt
              Time: 1.1 seconds
              gotta love that feeling when something works!

              Congrats, post back here if you need assistance on anything else!

              Greg

              Comment

              • Breana
                New Member
                • Aug 2007
                • 117

                #8
                Uh.. yeah it wprked but no files was put into the uploads/ folder?

                Comment

                • Breana
                  New Member
                  • Aug 2007
                  • 117

                  #9
                  Never mind, it crashes at the tmp/ lol i forgot to ad that folder duh.....

                  UPDATED :)

                  Upload: testgame.txt
                  Type: text/plain
                  Size: 0.0625 Kb
                  Temp file: /tmp/php4a9ydf
                  testgame.txt already exists.

                  Sorry, you already uploaded that file...

                  Comment

                  Working...