2 Pages V   1 2 >  
Reply to this topic Start new topic

 [PHP] - [Uploading files] - [making]
making
post Oct 12 2004, 04:42 PM
Post #1


Young Padawan
*

Group: Members
Posts: 1
Joined: 12-October 04
From: Palestine
Member No.: 333





Welcome everyone hiya.gif
here you'll learn how to upload files using PHP in a very simple way.

first put the following code in one file (upload.php)
create a directory called (images) or whatever you want "don't forget to change ( $upload_dir = "images/"; ).




CODE
<html>

<head>
<title>Upload File</title>
</head>

<form action="upload.php" method="post" enctype="multipart/form-data">
Browse a File to Upload:<br>
<input type="file" name="filetoupload"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<input type="Submit" value="Upload File">
</form>

<?php
/*
Author:  Mohammed Ahmed(M@king)
E-mail:  m at maaking dot com
WWW   :  http://www.maaking.com
*/
//**********************************************************************//
//  $_FILES['filetoupload']  is the value of                            //
// file field from the form. <input type="file" name="filetoupload">    //
//**********************************************************************//

// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "images/";   //change to whatever you want.
// files less than 1MB
$size_bytes = 1048576; //bytes  will be uploaded
$limit_file_type = "no"; //Do you want to limit the types of files uploaded. (yes/no)
// specify file types.
$allowed_file_type = array('image/gif',
                          'image/pjpeg',
                          'image/jpeg',
                          'image/png',
                          'image/jpg');

         //check if the directory exist or not.
         if (!is_dir("$upload_dir")) {
     die ("The directory <b>($upload_dir)</b> doesn't exist");
         }
         //check if the directory is writable.
         if (!is_writeable("$upload_dir")){
            die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
         }

//Check first if a file has been selected
//is_uploaded_file('filename') returns true if
//a file was uploaded via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{//begin of is_uploaded_file

        //Get the Size of the File
        $size = $_FILES['filetoupload']['size'];
        //Make sure that $size is less than 1MB (1000000 bytes)
        if ($size > $size_bytes)
        {
            echo "File Too Large. File must be <b>$size_bytes</b> bytes.";
            exit();
        }
             //check file type
        if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type)))
        {
            echo"wrong file type";
            exit();
        }

        // $filename will hold the value of the file name submetted from the form.
        $filename =  $_FILES['filetoupload']['name'];
        // Check if file is Already EXISTS.
        if(file_exists($upload_dir.$filename)){
            echo "Oops! The file named <b>$filename </b>already exists";
            exit();
        }

        //Move the File to the Directory of your choice
        //move_uploaded_file('filename','destination') Moves afile to a new location.
        if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {

           //tell the user that the file has been uploaded and make him alink too;).
           echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded!";
           exit();

        }
        else
        {
            //Print error
            echo "There was a problem moving your file";
            exit();
        }
}//end of is_uploaded_file


?>







what we used?

-The Super Global Variable $_FILES is used in PHP 4.x.x.

$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.

****************
-Functions used

1- (!is_dir("$upload_dir"))
checks if the directory exist or not.

2- (!is_writeable("$upload_dir"))
checks if the directory is writable.

3- (is_uploaded_file($_FILES['filetoupload']['tmp_name'])
Checks first if a file has been selected

4- (!in_array($_FILES['filetoupload']['type'],$allowed_file_type))
checks file type eg. "gif,jpg etc."

5- (file_exists($upload_dir.$filename))
Checks if file is Already EXISTS.

6- (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename))
Moves the File to the Directory of your choice eg. "images"

- IS it a very simple and powerful WAY???.
- Enjoy hiya.gif
M@king



Go to the top of the page
 
Jaymz
post Oct 12 2004, 04:49 PM
Post #2


Retired P2L Staff
****

Group: Members
Posts: 4,104
Joined: 25-September 04
Member No.: 21



Very good script smile.gif A word to people using this, make sure you chmod your directory to 77 so that php can write to it... I made that mistake and couldn't for the life of me figure out why it wasn't working...

Very good keep em coming smile.gif
Go to the top of the page
 
Gio
post Oct 13 2004, 06:57 PM
Post #3


Jedi In Training
**

Group: Members
Posts: 317
Joined: 25-September 04
Member No.: 20



Glad I am not the only making php tutorials on the forum.
Go to the top of the page
 
youngcity
post May 3 2005, 05:45 PM
Post #4


Young Padawan
*

Group: Members
Posts: 3
Joined: 3-May 05
Member No.: 5,684



how do you chmod a folder to 777?
Go to the top of the page
 
PsYcHo
post May 3 2005, 07:25 PM
Post #5


Young Padawan
*

Group: Members
Posts: 10
Joined: 16-October 04
Member No.: 393



you can chmod via ftp, or in cpanel... in ftp, right click the folder, and go to attributes or chmod, whichever yours has...
Go to the top of the page
 
Sinny
post May 12 2005, 07:57 PM
Post #6


Young Padawan
*

Group: Members
Posts: 144
Joined: 4-May 05
Member No.: 5,739



Hi, sorry for the slight bump, I just wanted to query you. I am a complete php newb, and I've been looking for a php script that let's users upload files without ftp access on a forum, is that what this script is? Sorry to be such a newb sad.gif
Go to the top of the page
 
adam123
post May 13 2005, 01:51 PM
Post #7


Retired P2L Staff
****

Group: Members
Posts: 2,306
Joined: 25-October 04
From: London, UK
Member No.: 538



QUOTE(Sinny @ May 13 2005, 01:57 AM)
Hi, sorry for the slight bump, I just wanted to query you. I am a complete php newb, and I've been looking for a php script that let's users upload files without ftp access on a forum, is that what this script is? Sorry to be such a newb sad.gif

Yes, 'tis what this is.
Go to the top of the page
 
runedeath
post May 13 2005, 03:13 PM
Post #8


Young Padawan
*

Group: Members
Posts: 8
Joined: 6-February 05
Member No.: 2,757



HOW TO CHMOD 777:

Either go to your cpanel and click on the folder to CHMOD then choose the permissions..

or...

Go to your ftp browser and right click on the file you wish to CHMOD and click either on * : properties / CHMOD / attributs and set it's permissions... very easy..!! this is basics all!!!!!! if u cant do this then dont bother learning PHP! lol
Go to the top of the page
 
Sinny
post May 13 2005, 04:19 PM
Post #9


Young Padawan
*

Group: Members
Posts: 144
Joined: 4-May 05
Member No.: 5,739



That's awesome, thankyou!! victory.gif
Is there a way to have it restricted? For example a log-in page on the index and it takes you to uploader? If anyone knows it would be greatly appreciated, thanks!! victory.gif
Go to the top of the page
 
TommyE
post Jun 8 2005, 08:33 PM
Post #10


Young Padawan
*

Group: Members
Posts: 55
Joined: 23-May 05
From: Glade Valley, North Carolina, USA
Member No.: 6,572



That is a script. Sure you can find a tut here on P2L smile.gif
Go to the top of the page
 
solitude
post Jul 14 2005, 05:44 PM
Post #11


Young Padawan
*

Group: Members
Posts: 18
Joined: 14-July 05
From: Texas, USA
Member No.: 8,580



i'm reely new at this i made a forum on phpbb u know forums.cjb.net

how would i use this tutorials there or where can i find tutorials or it
i still dont understand how to put ur banner or logo on the skin
please help me some1

thanxx
Go to the top of the page
 
pixelfinity
post Aug 1 2005, 10:45 AM
Post #12


Young Padawan
*

Group: Members
Posts: 14
Joined: 9-June 05
From: Probably at work
Member No.: 7,189



Great script (upload PHP)

is there a way (or could you submit) a progress bar graphic ???

Many thanks
Go to the top of the page
 
JoshT
post Aug 2 2005, 05:49 AM
Post #13


Young Padawan
*

Group: Members
Posts: 1
Joined: 2-August 05
Member No.: 9,363



Hey, how would i make it so that it will upload files to a mysql database?
Thanks
Josh
Go to the top of the page
 
ecrader
post Aug 11 2005, 03:32 PM
Post #14


Young Padawan
*

Group: Members
Posts: 1
Joined: 11-August 05
Member No.: 9,746



Hi,

I think I'm missing something. Uploaded the script and it seems to be working fine. But, when I go to my FTP site to retreave the files, I don't have permission to download them. It says they are owned by 'nobody'.

How do I get these files?

Thanks for your help!!!
Emily
Go to the top of the page
 
Addict
post Aug 15 2005, 02:30 PM
Post #15


Young Padawan
*

Group: Members
Posts: 1
Joined: 15-August 05
Member No.: 9,889



Thanks for this great tutorial.

I was just wondering if there was a way you could upload mutiple files?
Go to the top of the page
 
Firebird
post Oct 20 2005, 02:30 AM
Post #16


Young Padawan
*

Group: Members
Posts: 16
Joined: 20-October 05
Member No.: 12,239



Wow, cool script
Go to the top of the page
 
b00g3R
post Dec 6 2005, 06:00 AM
Post #17


Young Padawan
*

Group: Members
Posts: 79
Joined: 3-December 05
From: Chicago, IL, USA
Member No.: 13,867



My question is for that script.. is there a way to make it where it will create a folder automatically with the username? like say i have a couple ppl that register my site.. and say some of the names are 1. myname 2. myothername 3. imcool ... now i want it where when they register eitehr a folder will automatically be made with that users login name and then when they go to upload a file it will be put into there login names folder.. ?? does anyone know how to?
Go to the top of the page
 
Moezzie
post Dec 26 2005, 06:06 AM
Post #18


Young Padawan
*

Group: Members
Posts: 1
Joined: 26-December 05
Member No.: 14,557



very nice tutorial. l realy like it.
Is there any way to get the uploaded in a directory displayed so that people can download what's ben uploaded to?
havent found anything of that type yet.

Keep up the good work!
Go to the top of the page
 
Nad
post Jan 3 2006, 12:04 PM
Post #19


Young Padawan
*

Group: Members
Posts: 7
Joined: 27-December 04
From: England, The United Kingdom
Member No.: 1,668



nice tutorial! also, thanks for pointing out what it all means because i have never been able to get my head around php and file uploading

QUOTE(b00g3R @ Dec 6 2005, 11:00 AM) *
My question is for that script.. is there a way to make it where it will create a folder automatically with the username? like say i have a couple ppl that register my site.. and say some of the names are 1. myname 2. myothername 3. imcool ... now i want it where when they register eitehr a folder will automatically be made with that users login name and then when they go to upload a file it will be put into there login names folder.. ?? does anyone know how to?


i have always wondered how to do this aswell. anyone know how?

edit:
in your registration file, after the user has been registered, input:

CODE
mkdir("/uploads/" .$username. "/", 0777);


where $username is the var of the users' name inputted into the form/db.
this will make a folder with the username in the uploads folder and will be chmodded 777.
i have tried this and it works of, now you can just edit the upload script move paths to like "uploads/" .$_SESSION['username']. " or "uploads/" .$username. " or summa.

fixed error in code

This post has been edited by Nad: Jan 3 2006, 04:42 PM
Go to the top of the page
 
softmania
post Jan 26 2009, 10:22 AM
Post #20


Young Padawan
*

Group: Members
Posts: 19
Joined: 21-June 05
Member No.: 7,657



what if i want to upload mp3 format file?
Go to the top of the page
 

2 Pages V   1 2 >
Reply to this topic Start new topic
Forum Options & Statistics
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 31st July 2010 - 02:35 PM
Pixel2Life Home Advanced Search Get Started Credit Corner Get Started Credit Corner Forum Options The P2L Staff Top Overall Posters Top Today Posters Today Active Topics