TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Need help getting info into database (http://www.talkphp.com/absolute-beginners/4855-need-help-getting-info-into-database.html)

Flexxall 08-16-2009 03:22 PM

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)

jasonberresford 08-18-2009 12:06 PM

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.

rguy84 08-21-2009 09:35 PM

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

Flexxall 08-21-2009 11:55 PM

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 />");
}

?>


jasonberresford 08-24-2009 12:32 PM

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";



codefreek 08-24-2009 01:22 PM

ModNote: please read the following link.
http://www.talkphp.com/lounge/4563-p...e-talkphp.html
Thankyou,
-CF


All times are GMT. The time now is 11:07 PM.

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