Upload Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpenguin
    New Member
    • Aug 2007
    • 41

    Upload Script

    Hi, I'm creating a upload form for an open-source games track and kart repos. Basically I'm trying to code a upload form that allows user to select, from a pop-up menu, if they are uploading a kart or a track; then they select the file (File MUST be gziped or bziped). If they choose that the file is a kart, the file will be uploaded in ./karts,; if a track, it will be uploaded in ./tracks.
    So I did some searching on the internet, and coded a basic outline for part of my upload form.

    <
    Code:
    ?php
    $target = "upload/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $ok=1;
    
    //NOT CURRENTLY USED
    //This is our size condition
    //if ($uploaded_size > 350000)
    //{
    //echo "Your file is too large.<br>";
    //$ok=0;
    //}
    
    //This is our limit file type condition
    if (!($uploaded_type =="application/gzip" || $uploaded_type =="application/x-gzip"|| $uploaded_type =="application/x-bz2" || $uploaded_type =="application/x-bzip" || $uploaded_type =="application/bzip"))
    {
    echo "File MUST be gziped or bziped<br>";
    $ok=0;
    }
    
    //Here we check that $ok was not set to 0 by an error
    if ($ok==0)
    {
    Echo "Sorry your file was not uploaded";
    }
    
    //If everything is ok we try to upload it
    else
    {
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
    }
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    }
    ?>
    Code:
    <form enctype="multipart/form-data" action="upload.php" method="POST">
    Please choose a file: <input name="uploaded" type="file" /><br />
    <input type="submit" value="Upload" />
    </form>
    PS: The finished work will most likely be CC-BY
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    jpenguin,

    Well, that's lovely. Is there a problem with this script, a question you need help on or are you just showing us that you can copy scripts you found on-line?

    If there is some trouble with it I'd be happy to help but right now I'd be guessing.

    If you need help please provide the following:

    1. Any error messages you are getting including the code that the message references
    2. What the code should do
    3. What the code does not do that it should do
    4. What the code does do that is should not do

    That information will lead to a more helpful less sarcastic answer then mine.

    Cheers
    nathj

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      What does "PS: The finished work will most likely be CC-BY" mean?

      Comment

      • jpenguin
        New Member
        • Aug 2007
        • 41

        #4
        1. I'm sorry I wasn't very clear; I', trying to learn basic PHP and thought this would be a good project to learn on. I've been doing some reading online, but don't get how I can create a drop-down menu suited for my use, because I've found so many complete different examples online.
        1.1 Right now I have the file type checking, what I need in it is a place where the user can select "kart or track"

        2. CC-BY is a license, I want the final script to be under. CC is like GPL but more liberal, BY is what the choose to stand for attribution; so, I'm only mentioning it so you know that you will get credit if I use any help you give.

        We don't even have a web-server picked out, I think it is going to be a Unix based server.

        Once I get the menu figured out I will add more to it- like a screen-shot uploader, and a name field, maybe even mysql integration; but that's all for later.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by jpenguin
          1. I'm sorry I wasn't very clear; I', trying to learn basic PHP and thought this would be a good project to learn on. I've been doing some reading online, but don't get how I can create a drop-down menu suited for my use, because I've found so many complete different examples online.
          1.1 Right now I have the file type checking, what I need in it is a place where the user can select "kart or track"
          So you need to add a drop down menu?

          Do you have the drop-down menu already created in your HTML form?
          If not, here's how your drop down should look:
          Code:
          <select name="uploadtype">
            <option value="kart">Kart</option>
            <option value="track">Track</option>
          </select>
          That should be in your html form.

          Then you will receive the value of the drop down form by doing this server-side:

          Code:
          $type = $_POST['uploadtype'];
          Originally posted by jpenguin
          2. CC-BY is a license, I want the final script to be under. CC is like GPL but more liberal, BY is what the choose to stand for attribution; so, I'm only mentioning it so you know that you will get credit if I use any help you give.
          Oh that's cool, our names on your product. Aha. You don't have to do that.

          Originally posted by jpenguin
          We don't even have a web-server picked out, I think it is going to be a Unix based server.
          Sounding good.

          Originally posted by jpenguin
          Once I get the menu figured out I will add more to it- like a screen-shot uploader, and a name field, maybe even mysql integration; but that's all for later.
          Database is the way forward, my friend, and we'll be here to help.

          Markus.

          Comment

          • jpenguin
            New Member
            • Aug 2007
            • 41

            #6
            Thanks for all your help., I think I've got something that will work. Of coarse I have no way of testing it yet, so for all I know it could crash and burn.
            I'll have to wait for replies from the project developers to see where we should store info like names (after all they have to be able to write C++ code that can retrieve it).

            So now I want to know what the best way to display info specific to karts or tracks; If they select "kart"- I want to display one additional upload field, if they select "track"- two additional upload field should be displayed.

            Here is my current HTML code
            Code:
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <html>
            <head>
              <meta
             content="text/html; charset=ISO-8859-1"
             http-equiv="content-type">
              <title>upload form</title>
            </head>
            <body>
            <form enctype="multipart/form-data"
             action="upload.php" method="post">What
            type of
            add-on are you uploading:&nbsp;
              <select name="uploadtype">
              <option value="addons/karts/">Kart</option>
              <option value="addons/tracks/">Track</option>
              </select>
              <br>
            Is the add-on's license compatible with Debain: <input
             name="license" value="Yes" type="radio">Yes
              <input name="your question2"
             value="No" type="radio">No<br>
            Who is the
            author: <input name="author:"
             size="50" maxlength="100" type="text"><br>
              <br>
            What is the name of the track/kart:<input
             name="name:" size="50" maxlength="100" type="text"><br>
              <br>
            Please
            choose a file: <input name="uploaded"
             type="file"><br>
              <input value="Upload" type="submit"></form>
            <br>
            </body>
            </html>
            My PHP code
            Code:
            /*This file is under a CC-BY license, it has beeen coded by jpenguin with help from
            som freindly people at the bytes.com forum*/
            <?php
            $allow = $_POST['license'];
            $type = $_POST['uploadtype'];
            $target = $type . basename( $_FILES['uploaded']['name']) ;
            $ok=1;
             
            //NOT CURRENTLY USED
            //This is our size condition
            //if ($uploaded_size > 350000)
            //{
            //echo "Your file is too large.<br>";
            //$ok=0;
            //}
             
            //This is our limit file type condition
            if (!($uploaded_type =="application/gzip" || $uploaded_type =="application/x-gzip"|| $uploaded_type =="application/x-bz2" || $uploaded_type =="application/x-bzip" || $uploaded_type =="application/bzip"))
            {
            echo "File MUST be gziped or bziped<br>";
            $ok=0;
            }
            //license check
            if ($allow =="no")
            {
            echo "All add-ons MUST have a Debain campatable license"
            $ok=0;
            }
            //Here we check that $ok was not set to 0 by an error
            if ($ok==0)
            {
            Echo "Sorry your file was not uploaded";
            }
             
            //If everything is ok we try to upload it
            else
            {
            if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
            {
            echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
            }
            else
            {
            echo "Sorry, there was a problem uploading your file.";
            }
            }
            ?>
            PS–I am using NVU to create my HTML form. a screenshot of the 'preview' is here

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Have you not tested your code?!

              If you're only looking for a test server, why not setup a localhost. Every developer in their right minds has one.

              What's a localhost, you ask? A localhost, is a webserver that is on your local machine.

              Googe for xampp or easyphp for very easily installed localhosts.

              --

              The 'select one -> one upload field, select 2 -> two upload fields' thing, can be accomplished with Javascript. Head over to that forum and ask your question there.

              Comment

              • jpenguin
                New Member
                • Aug 2007
                • 41

                #8
                Your write, I already have APACHE, I just need to get PHP; then I'll test the code.

                Comment

                • jpenguin
                  New Member
                  • Aug 2007
                  • 41

                  #9
                  So I'm just about done. I am trying to save some info ther enters in a txt file, I actually have a function for that, but I can't get the name the way I want it nor save it in the right spot; if the user uploads "mytrack.tb z", the text file should be named "mytrack.tx t" and it should go in the same directory as the uploaded .tbz

                  My HTML code-
                  Code:
                  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                  <html>
                  <head>
                    <meta content="text/html; charset=ISO-8859-1"
                   http-equiv="content-type">
                    <title>upload form</title>
                  </head>
                  <body>
                  <form enctype="multipart/form-data" action="upload.php" method="post">What
                  type of
                  add-on are you uploading:&nbsp;
                    <select name="uploadtype">
                    <option value="karts/">Kart</option>
                    <option value="tracks/">Track</option>
                    </select>
                    <br>
                  Is the add-on's license compatible with Debain: 
                    <select name="license">
                    <option value="no">no</option>
                    <option value="yes">yes</option>
                    </select>
                    <br>
                    <br>
                   Who is the
                   author: <input name="author" type="text"><br>
                     <br>
                   What is the name of the track/kart:<input name="title" type="text"><br>
                     <br>  <br>
                    <br>
                  Please
                  choose the archive for the kart/track: <input name="uploaded"
                   type="file"><br>
                    <input value="Upload" type="submit">
                    <br>
                  </form>
                  </body>
                  </html>
                  Parts of my PHP code-
                  Code:
                  <?php
                  $author=$_POST['author'];
                  $title=$_POST['title']; 
                  $allow = $_POST['license'];
                  $type = $_POST['uploadtype'];
                  $target = $type . basename( $_FILES['uploaded']['name']) ;
                  $ok=1;
                  
                  //find the file extention
                  function findexts ($filename)
                  {
                  $filename = strtolower($filename) ;
                  $exts = split("[/\\.]", $filename) ;
                  $n = count($exts)-1;
                  $exts = $exts[$n];
                  return $exts;
                  }
                  $ext = findexts ($_FILES['uploaded']['name']) ;
                  Code:
                  	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
                  	{
                  		echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
                  		//The collects the athor and the addon name
                  		//and stores them in a text file
                  		$File = (($target)".txt");			//This line is causing the problem
                  		$Handle = fopen($File, 'w');
                  		fwrite($Handle, $author);
                  		fwrite($Handle, "\n");
                  		fwrite($Handle, $title);
                  		fclose($Handle); 
                  			if ($type =="karts/")
                  			{
                  				//The next line is server specific
                  				header( 'Location: http://josh-dyes-computer.local/~jpenguin/stk/Kart upload.html' ) ;
                  			}
                  			if ($type =="tracks/")
                  			{
                  				//The next line is server specific
                  				header( 'Location: http://josh-dyes-computer.local/~jpenguin/stk/Track upload.html' ) ;
                  			}
                  	}
                  	else
                  	{
                  		echo "Sorry, there was a problem uploading your file.";
                  	}
                  }
                  ?>

                  Comment

                  • jpenguin
                    New Member
                    • Aug 2007
                    • 41

                    #10
                    nevermind, I got it!

                    Thanks for all your help

                    Comment

                    Working...