Solved

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

Posted on 2008-06-16
5
2,336 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:ResortCompanies
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:qaelan
ID: 21798939
You might try removing the spaces between

< ?php

and

? > 

At the start and end of the file. They should be <?php and ?>, respectively
0
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 21799017
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

0
 
LVL 29

Accepted Solution

by:
fibo earned 500 total points
ID: 21800367
Some typos and errors with ' and ".
Code is easier to grasp and debug when indented.

If doing lots of development, you shoud consider using commercial PHP IDEs like Zend or NuCoder: they would help you spotting at once these typos.
They offer trial versions that you can use for free for 2 or 4 weeks: this will help you to determine if the time you save is worth the money. They both have a "personal" version at $100-120.

<?php
$location="/var/www/website.com/html/imagearchive/";
 
$db = mysql_pconnect("localhost");
mysql_select_db("database",$db);
 
$sql="select person.name as my_name, media.id as my_id, media.data as my_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->my_name . '.' . $row->my_id . '.jpg';
  echo "$filename :";
  $file=fopen($filename,'w');
  if (fwrite($file,$row->my_data)) {
    echo "OK<br />";
    $counter++;
  } else echo 'fail **<br>';
}
 
echo "<h2>$counter images exported</h2>";
?>

Open in new window

0
 

Author Closing Comment

by:ResortCompanies
ID: 31467823
Thanks, worked like a charm.
0
 
LVL 29

Expert Comment

by:fibo
ID: 21805535
Glad it worked, thx for the points
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question