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 08-16-2009, 03:22 PM   #1 (permalink)
The Visitor
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Flexxall is on a distinguished road
Default Need help getting info into database

I have a file that is reading the files in a directory. I also want the file to upload the data to the database table. I can get it to send one file (even though its wrong) to the database. I am sure its a problem with my variables but am I even going about this the correct way ? I also just thought about this part but if the files already exist in the DB then i do not want them written again. Heres what I have so far.
Code:
<?php
$path = "./";
$files = "*.gif";
$name = '$filename';
$size = 'size';
$type = 'type';
        
$sfiles = glob($path.$files);
asort($sfiles);

foreach ($sfiles as $filename) {
   echo "<br>\n", "$filename " . filesize($filename);
}
$content = file_get_contents($filename);
if ($conn = mysqli_connect('localhost', 'root', 'xxx', 'xxx')) {
			$content = mysqli_real_escape_string($conn, $content);
			$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";

			if (mysqli_query($conn, $sql)) {
				$uploadOk = true;
				$imageId = mysqli_insert_id($conn);
			} else {
				echo "Error: Could not save the data to mysql database. Please try again.";
			}

			mysqli_close($conn);
		} else {
			echo "Error: Could not connect to mysql database. Please try again.";
		}
?>
The table strcture is
Name
Size
Type
Content <- mediumblob binary (not sure if that is correct either)
Flexxall is offline  
Reply With Quote
Old 08-18-2009, 12:06 PM   #2 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 11
Thanks: 1
jasonberresford is on a distinguished road
Default

I'm not sure if there is another file calling this file ... But if there isn't ...

Your Foreach closes before you start the rest of your code(No loop). At least that's what I see first off.
jasonberresford is offline  
Reply With Quote
Old 08-21-2009, 09:35 PM   #3 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

I have been told by several people that it is better to just save the files in a directory and save paths to a db
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 08-21-2009, 11:55 PM   #4 (permalink)
The Visitor
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Flexxall is on a distinguished road
Default

Would you have any suggestions on how to do that ? I really am a noob with this. I have managed to get a bit further with a different script. Hers what I have for this version
Code:
<?php

mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('pets');

$Dir = '.';

// Get a list of the files
if ($handle = opendir($Dir))
{
      while ($file = readdir($handle))
      {
            $extension = strtolower(substr($file, -3));
            if ($extension == 'gif')
            {
                  // Load the file contents into the database
                  mysql_query("INSERT INTO image (name,size,type,content) VALUES ('$file', '$size($filename)', '$type', '" . addslashes(file_get_contents($Dir . "/" . $file)) . "');") or die(mysql_error());
            }
      }
}
else
{
      die("Could not open directory: $Dir<br />");
}

?>
Flexxall is offline  
Reply With Quote
Old 08-24-2009, 12:32 PM   #5 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 11
Thanks: 1
jasonberresford is on a distinguished road
Default

I use something like this .. also note I create a thumbnail, and give a random start to the filename.

Nothing fancy but it works .. then you just echo out the image in the database, and store what you like in it ..

In this case I track who uploaded it, what type of file it is, the filename, and the game it belongs to, along with comments on the image itself.


PHP Code:
 if (!empty($_FILES['userfile'])) {
$random rand();
$uploaddir 'files/';
$type 'picture';
$comments mysql_real_escape_string($_POST['comments']);


$uploadfile $uploaddir $random.basename($_FILES['userfile']['name']);
$uploadfile str_replace(" """$uploadfile);
$filename $random.basename($_FILES['userfile']['name']);
if (
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) {
$filename str_replace(" """$filename);

exec("/usr/bin/convert -thumbnail x200 files/$filename /var/www/www/files/thumb-$filename");
mysql_query("INSERT INTO image (filename,type,userid,gameid,comments) VALUES ('$filename','$type','$userid','$gameid','$comments')");

}
} else {
   echo 
"Possible file upload attack!\n";


Last edited by jasonberresford : 08-24-2009 at 01:25 PM.
jasonberresford is offline  
Reply With Quote
Old 08-24-2009, 01:22 PM   #6 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

ModNote: please read the following link.
Prettifying Pasted Code on TalkPHP
Thankyou,
-CF
codefreek is offline  
Reply With Quote
The Following User Says Thank You to codefreek For This Useful Post:
jasonberresford (08-24-2009)
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Adding Images to a database from a folder Rendair Advanced PHP Programming 3 01-13-2008 07:40 PM
Important Database Structure Question! AnthonyOS MySQL & Databases 5 12-20-2007 03:26 PM


All times are GMT. The time now is 11:43 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