Link to home
Start Free TrialLog in
Avatar of ResortCompanies
ResortCompanies

asked on

Need to export all jpeg files (blob entries) to filesystem

We have an old Linux box setup years ago as a PHP/MySQL based webserver.  We're upgrading it to a new server, and need to get all the images (jpeg type) that are stored in the sql database out to the filesystem.  I found a PHP script to do just that, but I'm having problems.  I think my problems are that this server is running PHP 4.3.2.  We have no expertise in Linux, or PHP, so we're trying to extract this info without putting effort into upgrading the system first.  I know this has got to be a simple thing to do.  Any ideas?  Oh, the way it's to name the file, is to take and combine the info in 2 of the fields in the table, and append .jpg on the end of it.

I have attached my code.  What's happing when I run it, is that it appears to be bypassing the PHP engine because it simply shows me the PHP code in my browser.  I've tested PHP with the phpinfo() function and that works fine.

Thanks for your help.
< ?php
$location="/var/www/website.com/html/imagearchive/";
 
$db = mysql_pconnect("localhost");
mysql_select_db("database",$db);
 
$sql="select person.name, media.id, media.data from media LEFT JOIN person ON (person.id=media.l_table_name_id)";
 
$rs = mysql_query($sql,$db);
 
echo "<h2>EXPORTING INTO $location</h2>";
$counter=0;
while ($row=mysql_fetch_object($rs)) {
$filename=$location.$row->person.name"."media.id..jpg;
$file=fopen($filename,w');
if (fwrite($file,$row->image)) {
echo $filename.<br />;
$counter++;
}
}
 
echo <h2>$counter images exported</h2>;
? >

Open in new window

Avatar of qaelan
qaelan

You might try removing the spaces between

< ?php

and

? > 

At the start and end of the file. They should be <?php and ?>, respectively
Try this code. The php being output to the screen is caused by the space between the ? and the > sign at the start and end.

There are also a few php syntax errors, which I corrected below.

Let me know if I did not code it right.


<?php
$location="/var/www/website.com/html/imagearchive/";
 
$db = mysql_pconnect("localhost");
mysql_select_db("database",$db);
 
$sql="select person.name, media.id, media.data from media LEFT JOIN person ON (person.id=media.l_table_name_id)";
$rs = mysql_query($sql,$db);
 
echo "<h2>EXPORTING INTO ".$location."</h2>";
 
if ( $rs && mysql_num_rows($rs) > 0 )
{
	$counter=0;
	for ( $i=0; $i < mysql_num_rows($rs); $i++ )
	{
		$name = mysql_result($result,$i,0);
		$id = mysql_result($result,$i,1);
		$data = mysql_result($result,$i,2);
		$filename = $location.$name.$id.".jpg";
		$file=fopen($filename,'w');
		if (fwrite($file,$data)) 
		{
			echo $filename."<br />";
			$counter++;
		}
	}
}
else
{
echo "No files found";
}
 
echo "<h2>".$counter." images exported</h2>";
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ResortCompanies

ASKER

Thanks, worked like a charm.
Glad it worked, thx for the points