Jump to content

upload directory contents into mysql database ?


flexxall

Recommended Posts

So I was able to return the data ok with this

<?php
$path = "./";
$files = "*.gif";
        
$sfiles = glob($path.$files);
asort($sfiles);

foreach ($sfiles as $filename) {
   echo "<br>\n", "$filename " . filesize($filename);
}
?> 

 

I could use assistance assigning the information variables that can then be uploaded to the database. My table consists of this so far and Im not sure if this is the proper way to store the data.

  • name varchar(100) latin1_swedish_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext
  • size int(11) Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext
  • type varchar(20) latin1_swedish_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext
  • content mediumblob BINARY Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext

Made som eprogress but need help with variables

Heres what I have now that kind of works

<?php
$path = "./";
$files = "*.gif";
$name = '$filename'; //Variable here
$size = 'size';          //Variable here
$type = 'type';        //Variable here
        
$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.";
	}
?> 

 

But this adds a file to the table with $filename 0 size and no type. I am having defining the variable correctly

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.