Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page Need to Know about Upload Form
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
simson's Avatar
Member
simson is on a distinguished road
 
Posts: 46
Join Date: Sep 2011
Location: INDIA
Question Need to Know about Upload Form - 09-13-2011, 11:44 AM

Hello, Friends
I need to know if visitor upload there files through web forms on my site then how to save uploaded files on my server and what code is used for it. I am using HTML form

I want to give a simple form for uploading jpg, gif & png files only, i am not good in programming so please help me out.

Thanks in Advance


Human is a Self Destruction Machine

Last edited by simson; 09-13-2011 at 02:14 PM.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
CSS_Man's Avatar
Senior Member
CSS_Man is on a distinguished road
 
Posts: 2,912
Join Date: Jun 2010
Location: Earth
Default 09-13-2011, 07:30 PM

You can't use html to handle files on a server. You need to use php do this. Take trip to google and you can very easily find your answer.



Feel free to PM me for help
If someone has helped you please spread some karma
Reply With Quote
(#3 (permalink))
Old
Member
Passionless is on a distinguished road
 
Posts: 48
Join Date: Sep 2011
Default 09-15-2011, 09:06 AM

Hi,
File uploads are enabled (check here to see all the features that are enabled.)

Below is an example of an upload form and the server side code. Play around with it. Put it in some test directory then create the 'uploads' directory for the uploads to work.

Btw. You should check if there is a file with the same name on the upload dir, and many other things like mimetypes if you want to create thumbnails etc.

Good luck.

PHP Code:
<?php
// Do we process an upload...
if (isset($_REQUEST['upload']) && isset($_FILES['file']))
{
    
// Get the extension (cheap solution. no preg_*)
    
$extension explode('.'$_FILES['file']['name']);
    
$extension $extension[count($extension)-1];

    if (
in_array(strtolower($extension), array('jpg','png','gif')))
    {
        
// Do we have the file and can we read it?
        
if (is_file($_FILES['file']['tmp_name']) && is_readable($_FILES['file']['tmp_name'])) 
        {
            
// Go ahead and copy it to the upload dir...
            
if (move_uploaded_file($_FILES['file']['tmp_name'], "./uploads/" $_FILES['file']['name'])) {
                die (
'Seems ok');
            } else {
                die (
'Unknown error processing upload');
            }
        } else {
            die (
"Can\'t read uploaded file!");
        }
    } else {
        die (
"Not a valid type lol");
    }
} else {
// We better display the form
    
echo <<<FORM
<form method="post" action="upload.php?upload" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" name="submit" />
</form>
FORM;
}
?>
Reply With Quote
(#4 (permalink))
Old
simson's Avatar
Member
simson is on a distinguished road
 
Posts: 46
Join Date: Sep 2011
Location: INDIA
Default 09-16-2011, 04:15 AM

Thanks passionless you take out some time for my post. Do i need MySQL database for handling files on my server. Pls help me.


Human is a Self Destruction Machine
Reply With Quote
(#5 (permalink))
Old
Member
Passionless is on a distinguished road
 
Posts: 48
Join Date: Sep 2011
Default 09-16-2011, 04:49 AM

Hi,
If you want to just store them you won't need a DB to keep track of them. Of course, if you want to display them in some way (ie. a image-board or similar) you must keep track of them in some way. The easiest way is to store the file name, or some sort of ID, into the 'message' table.

What are you planning to do?
Reply With Quote
(#6 (permalink))
Old
simson's Avatar
Member
simson is on a distinguished road
 
Posts: 46
Join Date: Sep 2011
Location: INDIA
Default 09-16-2011, 05:09 AM

I am creating Wallpapers Website & i just want that visitors upload there jpeg,gif or png files on my server. How is it possible? i create folder 'uploads' on the root folder & create upload page with the code that you mention above but when i test it, it doesn't upload my image file on the 'uploads' folder. What mistake i am doing?


Human is a Self Destruction Machine
Reply With Quote
(#7 (permalink))
Old
Member
Passionless is on a distinguished road
 
Posts: 48
Join Date: Sep 2011
Default 09-16-2011, 05:43 AM

Hi,
It's happens that the 'uploads' folder must be located on the same directory of the script, not in the root directory. That means you directory structure should look like this:
public_html/
upload.php
uploads/
The script must be named "upload.php", because the form's 'action' attribute points to it. Otherwise change that attribute.

Check if it works now.
Reply With Quote
(#8 (permalink))
Old
simson's Avatar
Member
simson is on a distinguished road
 
Posts: 46
Join Date: Sep 2011
Location: INDIA
Default 09-16-2011, 06:44 AM

Hey Buddy... MANY MANY THANKS it works perfect KARMA FOR YOU but 1 more thing to know you have given this line of code if (move_uploaded_file($_FILES['file']['tmp_name'], "./uploads/" . $_FILES['file']['name'])) {
die ('Seems ok'); instead of this i want to give customize page is this possible?


Human is a Self Destruction Machine
Reply With Quote
(#9 (permalink))
Old
Member
Passionless is on a distinguished road
 
Posts: 48
Join Date: Sep 2011
Default 09-16-2011, 07:16 AM

Hi,
Well you can embed HTML code into the script but a more flexible way is to include HTML templates passing to them some variables like error messages and such.

So you can add...
include_once 'template.tpl.php';
...instead of...
die('Seems ok');
Then you 'template.tpl.php' file can contain, among all commom HTML elements (it's essentially a HTML page) some PHP blocks for dynamic contents:
<div class="box message">
<?php
echo 'You file: ', $_FILES['file']['name'], ' was succesfully uploaded!';
?>
</div>
That's extremely minimalist but point is the included template/script can access any variable you pass to it. So you can build a common template for the whole site and depending on context show different contents, like the upload form or a message page.

Good luck.

Last edited by Passionless; 09-16-2011 at 07:20 AM. Reason: $filename variable doesn't exists lol
Reply With Quote
(#10 (permalink))
Old
Junior Member
Paars is on a distinguished road
 
Posts: 25
Join Date: Aug 2011
Location: Netherlands, Leeuwarden
Default 09-16-2011, 07:38 AM

Quote:
Originally Posted by Passionless View Post
Hi,
Well you can embed HTML code into the script but a more flexible way is to include HTML templates passing to them some variables like error messages and such.

So you can add...
include_once 'template.tpl.php';
...instead of...
die('Seems ok');
Then you 'template.tpl.php' file can contain, among all commom HTML elements (it's essentially a HTML page) some PHP blocks for dynamic contents:
<div class="box message">
<?php
echo 'You file: ', $_FILES['file']['name'], ' was succesfully uploaded!';
?>
</div>
That's extremely minimalist but point is the included template/script can access any variable you pass to it. So you can build a common template for the whole site and depending on context show different contents, like the upload form or a message page.

Good luck.
I guess thats pretty much how I do it, but correct me if Im wrong.

my index.php pretty much includes everything including a general div box.
I (re)direct everything in this way-> <a href=index.php?p=variable
as example variable might be login.php, so would be: index.php?p=login.php

<div class=blaat>
//in this case $page will be login.php, which will be included into your div class
$page = $_Get["p"];
if($page != "")
include_once($page);
else
// do nothing or show random page, w/e.

</div>

if thats what you meant simson, might be an ok sample for you?
Reply With Quote
Reply

Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com