TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-29-2008, 10:50 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Back to Basics: File uploading

I've come to the end of the first module for my web site, and I think I've learned an awful lot in the last month since I picked up PHP again. All the basics, a lot about security, form handling, sessions, and most of all OOP (I lurv OOP). But now that I'm moving on to the next module, I feel like an infant again. Pulling out all my books, got the manual open in four different tabs, and here we go...

I was wondering if anybody could point me in the direction of any bookmarks they have for dealing with files and file uploads. I don't want pre-built classes for this, I've looked at a couple, and while they're really powerful, they're also bloated beyond what I want. So I'm looking for good tuts, resources and information.

On that note, while I'm playing around with a basic form and the $_FILES superglobal to get comfortable with it, I was curious -- is there any way to check a file for any information before it's uploaded? For example, to make sure if I want a 30kb or less file, they aren't trying to upload a 2mb file? I know how to check this once the file is uploaded, and it's easy to throw em back an error, but it seems like a waste to let somebody upload a large file only to be caught with "um, you're dumb, go back and read the instructions.". I won't word it like that on my production server, just the test one, but you get the idea.
-m
delayedinsanity is offline  
Reply With Quote
Old 04-29-2008, 10:58 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Smaller question, not worth a new thread - I'm guessing to store an image in a MySQL database you would probably use something like base64_encode() or a blob data field. I'm thinking though that it's probably a hell of a lot faster to store files physically on the server and just store their location in the database. Less data in the database makes it quicker to access, and not having to encode or decode an image on retrieval would also add to the speed factor. Any opinions? When would storing an image in the database be recommendable?
-m
delayedinsanity is offline  
Reply With Quote
Old 04-29-2008, 11:18 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I don't have a tutorial for uploading, but I can address your more specific questions.

Quote:
Originally Posted by delayedinsanity View Post
I'm guessing to store an image in a MySQL database you would probably use something like base64_encode() or a blob data field. I'm thinking though that it's probably a hell of a lot faster to store files physically on the server and just store their location in the database. Less data in the database makes it quicker to access, and not having to encode or decode an image on retrieval would also add to the speed factor. Any opinions? When would storing an image in the database be recommendable?
-m
Don't store files in the database, there is a reason it is called a database and your file server is called a file server. If you need to keep track of things, keep their location in the database.

Quote:
Originally Posted by delayedinsanity View Post
On that note, while I'm playing around with a basic form and the $_FILES superglobal to get comfortable with it, I was curious -- is there any way to check a file for any information before it's uploaded? For example, to make sure if I want a 30kb or less file, they aren't trying to upload a 2mb file? I know how to check this once the file is uploaded, and it's easy to throw em back an error, but it seems like a waste to let somebody upload a large file only to be caught with "um, you're dumb, go back and read the instructions.". I won't word it like that on my production server, just the test one, but you get the idea.
-m
Yes, $HTTP_POST_FILES['userfile']['size']
__________________

Village Idiot is offline  
Reply With Quote
Old 04-29-2008, 11:45 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

For the purposes I intend to incorporate, storing them outside of the database is definitely the way to go. I'm sure somebody stores files in their database however, I was just wondering if anybody had any insights as to when that'd be more feasible.

HTTP_POST_FILES is just PHP 4's $_FILES if I'm correct and isn't used at all on my installation. Either way, I was hoping to check the file size before the upload, not after. Thank you though.
-m
delayedinsanity is offline  
Reply With Quote
Old 04-30-2008, 12:43 AM   #5 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

The cons of database storage

1. The obvious increase in overhead, making your application slower.

2. Database storage can be expensive

3. your code will be slighty more complex. (comment and structure clearly and it's not an issue)

As for benefits of Database storage think of it this way

1. You've essentially one very BIG file to back up, but one none the less. This single file contains every other file which has been uploaded. It's cleaner and simpler then having to back up several files.

2. Databases like MySQL support server clustering so you can just connect to ONE database and have 10 machines at your disposal. Retrieving the file info is a task of simply querying it and then decoding it (You are using base64?). Correct me if I'm wrong on this part though - I read about the server clustering in passing.

3. What do companies most often spend more money taking care of? their databases. Think about it.

When you have the machine power to carry it out I'd go with database storage, if you've got the raw ammount of storage and not so much CPU then a simple file server type server is good.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 04-30-2008, 01:39 AM   #6 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

How do you get the file extention from $_FILES? $that = $_FILES['input']; doesn't give me anything.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 04-30-2008, 02:07 AM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
How do you get the file extention from $_FILES? $that = $_FILES['input']; doesn't give me anything.
PHP Code:
$szExt end(explode("."$_FILES['file']['name']));
// or
$szExt pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); 
delayedinsanity is offline  
Reply With Quote
Old 04-30-2008, 11:34 AM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

If only PHP would allow for us to access array items concurrent to the function call. That would get rid of the second argument in that example. I'd love to see PHP introduce it.

php Code:
pathinfo($_FILES['file']['name'])['ext'];
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
iflashlord (05-03-2009)
Old 09-29-2008, 10:39 AM   #9 (permalink)
The Wanderer
 
bullit's Avatar
 
Join Date: Jan 2008
Location: Leeds, West Yorkshire
Posts: 9
Thanks: 6
bullit is on a distinguished road
Default

sorry to ressurect an old thread but I am having a similar issue, i have spent all morning looking for tutorials to deal with file uploads (.doc / .pdf in particular) i am wanting to store the location of the uploaded file in the database along with other information as these files are being uploaded with the intention of letting users download them to be read at there leisure, however all tutorials I have found have been storing the actual file in the database. Could anyone point me in the right direction please?

TIA folks :)
__________________
who is general failure? and why is he reading my hard drive?
Send a message via MSN to bullit
bullit is offline  
Reply With Quote
Old 09-30-2008, 07:58 AM   #10 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

thats an easy one
here is the code
lets suppose your upload directory is /upload/
and your database structure is
Code:
filepath - varchar(100)
plus additional columbs if needed
the code would look something like this

Code:
<?php

if(isset($_POST['submit'])) {

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";

//query
$query=mysql_query("insert into tbl_yourtable values(null, '$target_path'");
} else{
    echo "There was an error uploading the file, please try again!";
}










}

?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Choose a file to upload: <input name="uploadfile" type="file" /><br />
<input type="submit" value="upload" />
</form>
__________________
no signature set
sarmenhb is offline  
Reply With Quote
The Following User Says Thank You to sarmenhb For This Useful Post:
bullit (09-30-2008)
Old 09-30-2008, 08:37 PM   #11 (permalink)
The Wanderer
 
bullit's Avatar
 
Join Date: Jan 2008
Location: Leeds, West Yorkshire
Posts: 9
Thanks: 6
bullit is on a distinguished road
Default

thanks bud thats most helpful :)
__________________
who is general failure? and why is he reading my hard drive?
Send a message via MSN to bullit
bullit is offline  
Reply With Quote
Old 09-30-2008, 09:43 PM   #12 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

your welcome
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-03-2008, 10:14 AM   #13 (permalink)
The Wanderer
 
bullit's Avatar
 
Join Date: Jan 2008
Location: Leeds, West Yorkshire
Posts: 9
Thanks: 6
bullit is on a distinguished road
Default

hey again, sorry to be a pain but i cant seem to get my upload script working here is the script and related form:
Code:
    <?php
if(isset($_POST['save']))
{
    $title   = $_POST['title'];
      //$date = date(“Y-m-d”);
    //    $content = $_POST['content'];
    $target_path = "downloads/";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp'], $target_path)) 
    {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
    
        include '../inc/config.php';
        include '../inc/opendb.php';
    
        $query = "INSERT INTO news (title, created_at, filepath) VALUES ('$title', '$date', '$target_path')";
        mysql_query($query) or die('Error ,query failed');
        include '../inc/closedb.php';
    
        echo "<span class='bold_add'>Article '$title' added</span>";
    } else {
        echo "There was an error uploading the file, please try again!";
    }
    
}
?>
<form method="post" enctype="multipart/form-data">
  <table width="312" border="0" cellpadding="0" cellspacing="0" class="box" align="center">
    <tr> 
      <td width="40" class="white">Title</td>
      <td width="272"><input name="title" type="text" class="text" id="title" /></td>
    </tr>
    <tr> 
      <td width="40" valign="top" class="white">News</td>
      <td><input type="file" class="text" name="uploadedfile" /></td>
    </tr>
    <tr> 
      <td width="40">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td colspan="2" align="right" ><input name="save" type="submit" class="btn" id="save" value="Save Article"></td>
    </tr>
  </table>
</form>
basically I keep getting the error message regardless of what I try, I dont suppose anyone could tell me where I'm going wrong??

TIA yorkie
__________________
who is general failure? and why is he reading my hard drive?
Send a message via MSN to bullit
bullit is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design