TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Back to Basics: File uploading (http://www.talkphp.com/absolute-beginners/2737-back-basics-file-uploading.html)

delayedinsanity 04-29-2008 10:50 PM

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 04-29-2008 10:58 PM

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

Village Idiot 04-29-2008 11:18 PM

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

Quote:

Originally Posted by delayedinsanity (Post 14109)
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 (Post 14108)
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']

delayedinsanity 04-29-2008 11:45 PM

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

TlcAndres 04-30-2008 12:43 AM

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.

ETbyrne 04-30-2008 01:39 AM

How do you get the file extention from $_FILES? $that = $_FILES['input']; doesn't give me anything.

delayedinsanity 04-30-2008 02:07 AM

Quote:

Originally Posted by ETbyrne (Post 14114)
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); 


Wildhoney 04-30-2008 11:34 AM

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'];

bullit 09-29-2008 10:39 AM

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 :)

sarmenhb 09-30-2008 07:58 AM

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>


bullit 09-30-2008 08:37 PM

thanks bud thats most helpful :)

sarmenhb 09-30-2008 09:43 PM

your welcome

bullit 10-03-2008 10:14 AM

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


All times are GMT. The time now is 06:58 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0