Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2008, 04:00 PM   PM User | #1
Moodle
New Coder

 
Join Date: Jul 2008
Location: Newcastle, UK
Posts: 92
Thanks: 3
Thanked 7 Times in 5 Posts
Moodle is an unknown quantity at this point
User Upload System (and more!)

Hello, I have a request for a free script in where users use a simple form to upload their stuff to a page in my site.
They should be able to do the following:
  • Enter a title for the upload
  • Enter a description for the upload
  • Enter the catergories & tags
  • Upload their item & post a screenshot
  • Uploads are only available to logged in users - so there should be a log in system to accompany the uploader
  • Instant showage on site
  • The catergory must lead to the correct page. So if it is a picture, it will go to the picture page, a movie, the movie page.

Please help me.
FYI: I dont want to be pointed towards Mihalism Multi Host or anything like that. If you give me that, you mustn't have read my post thoroughly.

Thanks very much, Moodle =-)
Moodle is offline   Reply With Quote
Old 07-21-2008, 04:49 PM   PM User | #2
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.

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>
This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.


the actual file upload is very simple:

Code:
   <?php
    $target = "upload/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $ok=1;
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    }
    else {
    echo "Sorry, there was a problem uploading your file.";
    }
    ?>
This very small piece of code will upload files sent to it by your HTML form.

1. The first line $target = "upload/"; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file. So for example, if your file was at www.yours.com/files/upload.php then it would upload files to www.yours.com/files/upload/yourfile.gif. Be sure you remember to create this folder!

2. We are not using $ok=1; at the moment but we will later in the tutorial.

3. We then move the uploaded file to where it belongs using move_uploaded_file (). This places it in the directory we specified at the beginning of our script. If this fails the user is given an error message, otherwise they are told that the file has been uploaded.


Code:
   if ($uploaded_size > 350000)
    {
    echo "Your file is too large.<br>";
    $ok=0;
    }
Assuming that you didn't change the form field in our HTML form (so it is still named uploaded), this will check to see the size of the file. If the file is larger than 350k, they are given a file too large error, and we set $ok to equal 0.

You can change this line to be a larger or smaller size if you wish by changing 350000 to a different number. Or if you don't care about file size, just leave these lines out.



Code:
 if ($uploaded_type =="text/php")
    {
    echo "No PHP files<br>";
    $ok=0;
    }
The code above checks to be sure the user is not uploading a PHP file to your site. If they do upload a PHP file, they are given an error, and $ok is set to 0.

Code:
    if (!($uploaded_type=="image/gif")) {
    echo "You may only upload GIF files.<br>";
    $ok=0;
    }
In our second example we only allow users to upload .gif files, and all other types are given an error before setting $ok to 0. You can use these basic examples to allow or deny any specific file types.






--------Put it all together----------

Code:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

//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 =="text/php")
{
echo "No PHP files<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";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
masterofollies is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:51 AM.


Advertisement
Log in to turn off these ads.