Jump to content

Readfile Problem


imperium2335

Recommended Posts

Hi, I have something where a previous directory handle is active, then i have another directory handle start which is totaly not connected with the other one. But the second one loads a file that is not in any order, it is meant to get the files alphabetically like the first handler does no problem, here is the problem code:

////// BEGIN THUMBNAIL AND GALLERY CREATION...

$imagenum++ ;

$galleryname = $galleryname . $galnum . ".html" ; // Build full file name for gallery.

$thumbfileread = opendir($thumbimages) ; // Read the file name of the thumb images.
$readthumb = readdir($thumbfileread) ;

echo "$readthumb" ;
$galleryhandle = fopen($galleryname,w) ; // Prepare gallerys for writing.

 

I could send the whole code but its massive. Thanks in advance.

Link to comment
Share on other sites

Hi, thanks, here is all the code:

<?PHP

if (!file_exists("php/dbconnect.php"))
{
die("Database settings not found, administrator intervention required.") ;
}
else
{
require("php/dbconnect.php") ; //Must connect to the database.
}

$galtitle = "Square" ;

$galleryname = "square-cakes-" ;

$words = "cake cakes square rectangle oblong geometric math mathamatical line straight long short" ;

$fname = "cake- cakes- square- rectangle- oblong- geometric- math- mathamatical- line- straight- long- short-" ;

$idnumber = 93 ; // One higher than the record count in the base. When adding new librarys make sure this is correct!

$galnum = 1 ; // Start at the first gallery.

$filebox = NULL ;

$thumbimages = "cake-images/cake-thumbs/square-thumbnails/" ; // Location of thumb images.

$galleryout = "/" ; // Where the gallery pages will be placed, the root.

$dir = "cake-images/square-cakes" ;//Directory containing the images.

$imghome = "cake-images/square-cakes" ;

$outdir = "cake-photo-pages/square-cake-gallery" ; // Where the enlargment pages will be put.

$thumburllink = "http://www.cake-photos.com/cake-photo-pages/square-cake-gallery/" ;

$existingids = array() ; // Empty array needed later.

$openzone = @opendir($dir) ;

while($currentfile = readdir($openzone))
{

if($currentfile != "." && $currentfile != ".." && !is_dir($currentfile))
{

	$filecount++ ;

}
}	
	// Pagination data set...

$imagespergal = 9 ;
$galpages = ceil($filecount / $imagespergal) ; //Number of gallery pages needed, 9 thumbs per page. Coming from imagecount at the top of the script.
for($i=0; $i < $galpages; $i++)
{

	$pagelinks .= '<a href="' . "$galleryname" . "$galnum" . '">' . "$galnum" . '</a> ' ; // Build our gallery page links...

}
////////////////////////////////////////////

rewind($openzone) ;

while($firstfile = readdir($openzone))
{

if($firstfile != '.' && $firstfile != '..' && !is_dir($dir.'/'.$firstfile))
{

$word_pool = explode(" ",$words) ;
$name_pool = explode(" ",$fname) ;
$wordcnt = count($word_pool) ;
$namecnt = count($name_pool) ;

$length = rand(2, 5) ;

//Create file name...

for($i = 0; $i < $length; $i++)
{

	$w = rand(0, $namecnt - 1) ;
	$filename .= "$name_pool[$w] " ;
}

$filename = explode(" ", $filename) ; //Blow the words in the title apart...
$filename = array_unique($filename) ; //Delete any duplicate words found...
$filename = implode("",$filename) ; //Put the words back together again in the var...
$filename = substr($filename, 0, -1) ; //Delete the last character, a space in this case.
$filename = $filename . ".html" ;

$filebox .= $filename . " " ; // Add the proposed file name to the list of ones that exist.
$filearray = explode(" ",$filebox) ;

$names = count($filearray) ;

//if($filebox = NULL)
//{

$filehandle = fopen($outdir . "/" . $filename,'w') or die ("Error creating file") ;

//	}

//////////////////////////

echo "$filename<br />" ; 

$length = rand(2, 5) ;

for($i = 0; $i < $length; $i++)
{

	$w = rand(0, $wordcnt - 1) ;
	$title .= "$word_pool[$w] " ;
}

$title = explode(" ", $title) ; //Blow the words in the title apart...
$title = array_unique($title) ; //Delete any duplicate words found...
$title = implode(" ",$title) ; //Put the words back together again in the var...
$title = ucwords(strtolower($title)) ; //Every first letter to uppercase
$title = substr($title, 0, -1) ; //Delete the last character, a space in this case.

$length = rand(3, $wordcnt) ;

for($i = 0; $i < $length; $i++)
{
	$w = rand(0, $wordcnt - 1) ;
	$keywords .= "$word_pool[$w] " ;

}

$keywords = explode(" ", $keywords) ; //Blow the words in the title apart...
$keywords = array_unique($keywords) ; //Delete any duplicate words found...
$keywords = implode(" ",$keywords) ; //Put the words back together again in the var...
$keywords = substr($keywords, 0, -1) ; //Delete the last character, a space in this case.

$length = rand(2, 4) ;

for($i = 0; $i < $length; $i++)
{

	$w = rand(0, $wordcnt - 1) ;
	$headtag .= "$word_pool[$w] " ;

}

$headtag = explode(" ", $headtag) ; //Blow the words in the title apart...
$headtag = array_unique($headtag) ; //Delete any duplicate words found...
$headtag = implode(" ",$headtag) ; //Put the words back together again in the var...
$headtag = ucwords(strtolower($headtag)) ;
$headtag = substr($headtag, 0, -1) ; //Delete the last character, a space in this case.

	$length = rand(2, 4) ;

for($i = 0; $i < $length; $i++)
{

	$w = rand(0, $wordcnt - 1) ;
	$alttaga .= "$word_pool[$w] " ;

}

$alttaga = explode(" ", $alttaga) ; //Blow the words in the title apart...
$alttaga = array_unique($alttaga) ; //Delete any duplicate words found...
$alttaga = implode(" ",$alttaga) ; //Put the words back together again in the var...
$alttaga = substr($alttaga, 0, -1) ; //Delete the last character, a space in this case.

	$length = rand(2, 4) ;

for($i = 0; $i < $length; $i++)
{

	$w = rand(0, $wordcnt - 1) ;
	$alttagb .= "$word_pool[$w] " ;

}

$alttagb = explode(" ", $alttagb) ; //Blow the words in the title apart...
$alttagb = array_unique($alttagb) ; //Delete any duplicate words found...
$alttagb = implode(" ",$alttagb) ; //Put the words back together again in the var...
$alttagb = substr($alttagb, 0, -1) ; //Delete the last character, a space in this case.

	$firstimage = $imghome . '/' . $firstfile ;

	$secondimage = $imghome . '/' . readdir($openzone) ;

	$idnumber++ ;

$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>' . "$title" . '</title>
<meta name="keywords" content="' . "$keywords" . '" />' . '
<meta name="description" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="http://www.cake-photos.com/cakes-css.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?PHP $currentimage =' . "$idnumber" . ' ?>
<div id="page"><?PHP include "/home/caketph/public_html/includes/headnav.html" ?>
<div id="head"></div>

<div id="search">
<?PHP include "/home/caketph/public_html/includes/header-search.html"?>
</div>

<div id="sidebar">
<?PHP include "/home/caketph/public_html/includes/sidenav.html" ?>
</div>
<div id="main">' ;

$secondwidth = getimagesize($secondimage) ;

$firstwidth = getimagesize($firstimage) ;

if($secondimage[0] != NULL && $secondwidth[0] < $firstwidth[0])
{
$data .= '<img src="' . "http://www.cake-photos.com/$secondimage" . '" class="ralign" alt="' . "$alttaga" . '"/>' ;
}
elseif($secondwidth[0] != $firstwidth[0])
{

list($secondimage, $firstimage) = array($firstimage, $secondimage);
$data .= '<img src="' . "http://www.cake-photos.com/$secondimage" . '" class="ralign" alt="' . "$alttaga" . '" />' ;

}

$data .= '<h1>' . "$headtag" . '</h1>
<h2></h2>
<table width="350">
  <tr>
    <td>Cake ID</td>
    <td>' . $idnumber . '</td>
  </tr>
  <tr>
    <td>Decorator:</td>
    <td>Awaiting Info</td>
  </tr>
  <tr>
    <td>Type:</td>
    <td>Awaiting Info</td>
  </tr>
  <tr>
    <td>Flavor:</td>
    <td>Awaiting Info</td>
  </tr>
  <tr>
    <td>Tiers:</td>
    <td>Awaiting Info</td>
  </tr>
  <tr>
    <td>Serves:</td>
    <td>Awaiting Info</td>
  </tr>
  <tr>
    <td>Price:</td>
    <td>Awaiting Info</td>
  </tr>
</table>
<p>This cake is awaiting a description, please check back soon.</p>
<p>Is this your cake? Please <a href="http://www.cake-photos.com/contact.html">let us know</a> so we can give you credit and write a description for you!</p>
<img src="' . "http://www.cake-photos.com/$firstimage" . '" alt="' . "$alttagb" . '" />
<form action="<?php echo $_SERVER[' . "'PHP_SELF'" . ']; ?>" method="get">

<p><input class="rate" name="rating" type="text" id="rating" size="2" maxlength="2" /></p>
<p><input class="button" type="submit" name="button" id="button" value="Vote" /></p>
</form>  

</div>
</div>

<div id="rightzone">
<?PHP include "/home/caketph/public_html/php/random.php" ; ?>

</div>

<div id="footer">
<?PHP include "/home/caketph/public_html/includes/footer.html" ; ?>


</div>


</body>
</html>' ;

fwrite($filehandle,$data) ;
fclose($filehandle) ;

////// BEGIN THUMBNAIL AND GALLERY CREATION...

$imagenum++ ;

$galleryname = $galleryname . $galnum . ".html" ; // Build full file name for gallery.

$thumbfileread = opendir($thumbimages) ; // Read the file name of the thumb images.
while($readthumb = readdir($thumbfileread))
{
	echo "$readthumb" ;
}



echo "$readthumb" ;
$galleryhandle = fopen($galleryname,w) ; // Prepare gallerys for writing.



if($imagenum < 9)
{

	$thumbset .= '<div id="thumb">
<a href="'. "$thumburllink" . "$filename" . '"><img src="' . "$thumbimages" . "$readthumb" . '" alt="" /></a>
<p>Description coming soon.</p>
</div>' ;
echo "<br />image number is $imagenum, thumbnail data set is:<br />$thumbset" ;

}
else // We now have 9 images on that page (in the data set)...
{
echo "The image number is now $imagenum, building gallery<br />" ;
$gallerybuild = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>' . "$galtitle " . 'Cake Photo Gallery</title>
<meta name="keywords" content="' . "$keywords" . '" />' . '
<meta name="description" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="http://www.cake-photos.com/cakes-css.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="page"><?PHP include "/home/caketph/public_html/includes/headnav.html" ?>
<div id="head"></div>

<div id="search">
<?PHP include "/home/caketph/public_html/includes/header-search.html"?>
</div>

<div id="sidebar">
<?PHP include "/home/caketph/public_html/includes/sidenav.html" ?>
</div>
<div id="main">' . 
"$thumbset" . 
'<div id="galnav"><p class="central">' . "$pagelinks" . '</p></div></div>' . 
'<div id="rightzone">
<?PHP include "/home/caketph/public_html/php/random.php" ; ?>
</div>

<div id="footer">
<?PHP include "/home/caketph/public_html/includes/footer.html" ; ?>
</div>
</div>

</body>
</html>' ;

fwrite($galleryhandle,$gallerybuild) ; // Puke out the data set into the file, and close it...
$imagenum = 0 ; //Reset our image counter...
$galnum++ ; // And move onto the next gallery!
$thumbset = NULL ; // Wipe all previous thumbnails...

}

///////////////////////////////////////////////////////	

////// ADD IMAGE RECORD ID TO THE DATABASE AND THUMB/PAGE ADDRESS...

$pageurl = $outdir . "/" . $filename ;
$thumbaddy = $thumburllink . $readthumb ;
$query = "SELECT image_id FROM image_bank" ;

$result = mysql_query($query) ;
$i = 0 ;
while($row = mysql_fetch_assoc($result))
{

	$i++ ;
	$dbimgid = $row['image_id'] ;
	$existingids[$i] = $dbimgid ;
	//array_push($existingids,$dbimgid) ;
	//echo "$existingids[$i],$dbimgid<br />" ;

	//$existingids .= $dbimgid . " " ;

}

if(in_array($idnumber, $existingids))//IF an ID already exists...
{

$query = "UPDATE image_bank SET image_id='$idnumber', thumb_url='$thumbaddy', url='$pageurl' WHERE image_id='$idnumber'" ;

mysql_query($query) ;
echo "Updating base $idnumber.<br />" ;

}
else//Add the new record...
{

$query = "INSERT INTO image_bank (image_id, thumb_url, url) VALUES ('$idnumber', '$thumbaddy', '$pageurl')" ;

mysql_query($query) ;
echo "Inserting into base $idnumber.<br />" ;

}

///////////////////////////////////////////////////////



$title = NULL ; //Reset everything for the next file...
$keywords = NULL ;
$headtag = NULL ;
$alttaga = NULL ;
$alttagb = NULL ;
$filename = NULL ;
echo "Outputted $idnumber<br />" ;
exit() ;
}



}

?>

Link to comment
Share on other sites

You weren't kidding when you said it's big.  :o

 

You said it's only the one part giving you problems, right?

 

Replace this part of the code:

 

////// BEGIN THUMBNAIL AND GALLERY CREATION...

$imagenum++ ;

$galleryname = $galleryname . $galnum . ".html" ; // Build full file name for gallery.

$thumbfileread = opendir($thumbimages) ; // Read the file name of the thumb images.
while($readthumb = readdir($thumbfileread))
{
	echo "$readthumb" ;
}

 

With this:

 

////// BEGIN THUMBNAIL AND GALLERY CREATION...

$imagenum++ ;

$galleryname = $galleryname . $galnum . ".html" ; // Build full file name for gallery.

$thumbfileread = opendir($thumbimages) ; // Read the file name of the thumb images.
$newarray = array();
while($readthumb = readdir($thumbfileread))
array_push($newarray, $readthumb);
sort($newarray);

foreach ($newarray as $n)
	echo $n;

 

Battery is going to die any second, so I can't check it. But should fix it. :)

Link to comment
Share on other sites

Thanks, ill give the array idea a try. I've always used the filenames as they came off the readdir thing. But an array makes more sense cos afterall its just names im dealing with here.

 

Yea this is my first massive php thing I've ever made, still cant believe i made it lol. I'm still a noob i think, so far spent about 3 weeks in deep php.

 

Thanks.

Link to comment
Share on other sites

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.