473,378 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

<
Expand|Select|Wrap|Line Numbers
  1. ?php
  2. $target = "upload/";
  3. $target = $target . basename( $_FILES['uploaded']['name']) ;
  4. $ok=1;
  5.  
  6. //NOT CURRENTLY USED
  7. //This is our size condition
  8. //if ($uploaded_size > 350000)
  9. //{
  10. //echo "Your file is too large.<br>";
  11. //$ok=0;
  12. //}
  13.  
  14. //This is our limit file type condition
  15. if (!($uploaded_type =="application/gzip" || $uploaded_type =="application/x-gzip"|| $uploaded_type =="application/x-bz2" || $uploaded_type =="application/x-bzip" || $uploaded_type =="application/bzip"))
  16. {
  17. echo "File MUST be gziped or bziped<br>";
  18. $ok=0;
  19. }
  20.  
  21. //Here we check that $ok was not set to 0 by an error
  22. if ($ok==0)
  23. {
  24. Echo "Sorry your file was not uploaded";
  25. }
  26.  
  27. //If everything is ok we try to upload it
  28. else
  29. {
  30. if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
  31. {
  32. echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
  33. }
  34. else
  35. {
  36. echo "Sorry, there was a problem uploading your file.";
  37. }
  38. }
  39. ?> 
Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" action="upload.php" method="POST">
  2. Please choose a file: <input name="uploaded" type="file" /><br />
  3. <input type="submit" value="Upload" />
  4. </form> 
PS: The finished work will most likely be CC-BY
Nov 21 '08 #1
9 1602
nathj
938 Expert 512MB
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
Nov 21 '08 #2
Markus
6,050 Expert 4TB
What does "PS: The finished work will most likely be CC-BY" mean?
Nov 21 '08 #3
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.
Nov 21 '08 #4
Markus
6,050 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <select name="uploadtype">
  2.   <option value="kart">Kart</option>
  3.   <option value="track">Track</option>
  4. </select>
  5.  
That should be in your html form.

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

Expand|Select|Wrap|Line Numbers
  1. $type = $_POST['uploadtype'];
  2.  
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.

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

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.
Nov 21 '08 #5
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
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4.   <meta
  5.  content="text/html; charset=ISO-8859-1"
  6.  http-equiv="content-type">
  7.   <title>upload form</title>
  8. </head>
  9. <body>
  10. <form enctype="multipart/form-data"
  11.  action="upload.php" method="post">What
  12. type of
  13. add-on are you uploading:&nbsp;
  14.   <select name="uploadtype">
  15.   <option value="addons/karts/">Kart</option>
  16.   <option value="addons/tracks/">Track</option>
  17.   </select>
  18.   <br>
  19. Is the add-on's license compatible with Debain: <input
  20.  name="license" value="Yes" type="radio">Yes
  21.   <input name="your question2"
  22.  value="No" type="radio">No<br>
  23. Who is the
  24. author: <input name="author:"
  25.  size="50" maxlength="100" type="text"><br>
  26.   <br>
  27. What is the name of the track/kart:<input
  28.  name="name:" size="50" maxlength="100" type="text"><br>
  29.   <br>
  30. Please
  31. choose a file: <input name="uploaded"
  32.  type="file"><br>
  33.   <input value="Upload" type="submit"></form>
  34. <br>
  35. </body>
  36. </html>
  37.  
My PHP code
Expand|Select|Wrap|Line Numbers
  1. /*This file is under a CC-BY license, it has beeen coded by jpenguin with help from
  2. som freindly people at the bytes.com forum*/
  3. <?php
  4. $allow = $_POST['license'];
  5. $type = $_POST['uploadtype'];
  6. $target = $type . basename( $_FILES['uploaded']['name']) ;
  7. $ok=1;
  8.  
  9. //NOT CURRENTLY USED
  10. //This is our size condition
  11. //if ($uploaded_size > 350000)
  12. //{
  13. //echo "Your file is too large.<br>";
  14. //$ok=0;
  15. //}
  16.  
  17. //This is our limit file type condition
  18. if (!($uploaded_type =="application/gzip" || $uploaded_type =="application/x-gzip"|| $uploaded_type =="application/x-bz2" || $uploaded_type =="application/x-bzip" || $uploaded_type =="application/bzip"))
  19. {
  20. echo "File MUST be gziped or bziped<br>";
  21. $ok=0;
  22. }
  23. //license check
  24. if ($allow =="no")
  25. {
  26. echo "All add-ons MUST have a Debain campatable license"
  27. $ok=0;
  28. }
  29. //Here we check that $ok was not set to 0 by an error
  30. if ($ok==0)
  31. {
  32. Echo "Sorry your file was not uploaded";
  33. }
  34.  
  35. //If everything is ok we try to upload it
  36. else
  37. {
  38. if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
  39. {
  40. echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
  41. }
  42. else
  43. {
  44. echo "Sorry, there was a problem uploading your file.";
  45. }
  46. }
  47. ?> 
PS–I am using NVU to create my HTML form. a screenshot of the 'preview' is here
Nov 21 '08 #6
Markus
6,050 Expert 4TB
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.
Nov 22 '08 #7
Your write, I already have APACHE, I just need to get PHP; then I'll test the code.
Nov 22 '08 #8
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.tbz", the text file should be named "mytrack.txt" and it should go in the same directory as the uploaded .tbz

My HTML code-
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4.   <meta content="text/html; charset=ISO-8859-1"
  5.  http-equiv="content-type">
  6.   <title>upload form</title>
  7. </head>
  8. <body>
  9. <form enctype="multipart/form-data" action="upload.php" method="post">What
  10. type of
  11. add-on are you uploading:&nbsp;
  12.   <select name="uploadtype">
  13.   <option value="karts/">Kart</option>
  14.   <option value="tracks/">Track</option>
  15.   </select>
  16.   <br>
  17. Is the add-on's license compatible with Debain: 
  18.   <select name="license">
  19.   <option value="no">no</option>
  20.   <option value="yes">yes</option>
  21.   </select>
  22.   <br>
  23.   <br>
  24.  Who is the
  25.  author: <input name="author" type="text"><br>
  26.    <br>
  27.  What is the name of the track/kart:<input name="title" type="text"><br>
  28.    <br>  <br>
  29.   <br>
  30. Please
  31. choose the archive for the kart/track: <input name="uploaded"
  32.  type="file"><br>
  33.   <input value="Upload" type="submit">
  34.   <br>
  35. </form>
  36. </body>
  37. </html>
  38.  
Parts of my PHP code-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $author=$_POST['author'];
  3. $title=$_POST['title']; 
  4. $allow = $_POST['license'];
  5. $type = $_POST['uploadtype'];
  6. $target = $type . basename( $_FILES['uploaded']['name']) ;
  7. $ok=1;
  8.  
  9. //find the file extention
  10. function findexts ($filename)
  11. {
  12. $filename = strtolower($filename) ;
  13. $exts = split("[/\\.]", $filename) ;
  14. $n = count($exts)-1;
  15. $exts = $exts[$n];
  16. return $exts;
  17. }
  18. $ext = findexts ($_FILES['uploaded']['name']) ;
  19.  
Expand|Select|Wrap|Line Numbers
  1.     if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
  2.     {
  3.         echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,<br>it will be in the add--on manager shortly";
  4.         //The collects the athor and the addon name
  5.         //and stores them in a text file
  6.         $File = (($target)".txt");            //This line is causing the problem
  7.         $Handle = fopen($File, 'w');
  8.         fwrite($Handle, $author);
  9.         fwrite($Handle, "\n");
  10.         fwrite($Handle, $title);
  11.         fclose($Handle); 
  12.             if ($type =="karts/")
  13.             {
  14.                 //The next line is server specific
  15.                 header( 'Location: http://josh-dyes-computer.local/~jpenguin/stk/Kart upload.html' ) ;
  16.             }
  17.             if ($type =="tracks/")
  18.             {
  19.                 //The next line is server specific
  20.                 header( 'Location: http://josh-dyes-computer.local/~jpenguin/stk/Track upload.html' ) ;
  21.             }
  22.     }
  23.     else
  24.     {
  25.         echo "Sorry, there was a problem uploading your file.";
  26.     }
  27. }
  28. ?>
Nov 23 '08 #9
nevermind, I got it!

Thanks for all your help
Nov 24 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tihon | last post by:
Hello! I again need your help, just can't understand whats going on. Got this upload pictures form and it's having problem handling large files (~1.5 - 2 MB). Everything works fine if i just...
1
by: PeterB | last post by:
Hi! I'm using Pure ASP File Upload (http://www.asp101.com/articles/jacob/scriptupload.asp) to upload a file from a client to a server. I am testing both on a local IIS and a remote server. The...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
7
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file"...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
9
by: Steve Poe | last post by:
I work for an animal hospital trying to use PHP to store an animal's dental x-rays to a file server. I can browse for the xray on the local desktop computer then click "Upload Image". This...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
5
by: camphor | last post by:
hi, I have found an upload script in hotscripts and have implemented it into the website, I followed the installation steps to 'give write permissions to php on the upload folder (which is...
24
by: owz2008 | last post by:
This has probably been covered before but could not find a similar thread. Basically I have created a form which can be viewed at www.icomworks.co.uk/canvaspayform.html I want to submit the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.