Jump to content

I would like to read a dir of images and save the imagefile name to mysql


Go to solution Solved by jcbones,

Recommended Posts

I am having trouble trying to save my image file names to mysql after reading a dir. I am trying to use the file below.

Any help would be appreciated. I was originallly getting the files to list but they we not showing up in the database.

Now the page shows up blank.

<?php
require("config_0.php");

// Connect to server and select database.
mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");


$files = glob("images/*.jpg"); 
for ($i=1; $i<count($files); $i++) 
	{ 	
		$num = $files[$i];
		$sql="INSERT INTO images (url) VALUES ('$num')";

			if (!mysql_query($sql))
			  {
			  die('Error: ' . mysql_error());
			  }
		echo '<img src="'.$num.'" alt="random image">'."  ";
	}
?>

If you turn on php error checking you might see your errors.  If you are getting a blank page you may have errors.  You might also read the manual about these functions.  Note the special RED highlighted paragraphs that tell you not to use them.

 

BTW - your logic on the loop is flawed.  You're missing the last file with this code.  Also - you are saving a filename but you do not necessarily have the path to the files.  If you do a glob for "*.*" you get the files in the directory where the script is run from.  But once you store them in your table you have no reference to where the files are located.

In addition are missing the mysql_query() so not saving them to database

$sql=mysql_query("INSERT INTO images (url) VALUES ('$num')");

if (!$sql) {
    die('Error: ' . mysql_error());
}

Okay, I added error checking and the mysql_query and now I' getting a listing of the file names and the pictures show up.

I am still not getting anything in the mysql database. No error messages are showing up.

I think the problem in in the sql INSERT statement.

I've listed the program below.

<?php
error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');
require("config_0.php");

// Connect to server and select database.
mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");


$files = glob("image/*.jpg"); 
for ($i=1; $i<count($files); $i++) 
	{ 	
		$num = $files[$i];
    echo "$files[$i]";
    echo "<br/>";
    
    $sql=mysql_query("INSERT INTO images (url) VALUES ('$num')");

if (!$sql) {
    die('Error: ' . mysql_error());
}
	
		echo '<img src="'.$num.'" alt="random image">'."  ";
}	
?>

Since $files is an array, $i should start at 0, not 1 in the for() loop. You're skipping the first image.

 

Please also note that the mysql extension is deprecated, meaning it will no longer be available in php in the future. If you want to write code that will work on future versions of php without having to rewrite all of your queries, it's highly suggested you use the PDO or mysqli extension.

I use phpMyAdmin to view the database, it doesn't put anything in the columns except id which is generated. img_id, image, url are all zeros.

Any help would be appreciated and or thoughts to correct this.

  • Solution

Change the url column to a varchar(30).  The image might also need to be a varchar, although I don't know what you are using it for.

FYI int is for integers, which are whole numbers 0-9.  This is why you are getting all 0's.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.