Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Galeria i problem z filesize() + filemtime()
Kshyhoo
post 9.01.2010, 17:57:36
Post #1





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Witam. mam skrypt galerii, który działa poprawnie jedynie, gdy umieszczony jest w folderze z grafikami. Jeżeli zagłębiam się - działa (wyświetla grafikę, nie pobiera daty i rozmiaru), ponadto wywala błędy:
Cytat
Warning: filemtime() [function.filemtime]: stat failed for vertical.gif in ... galeria.php on line 56
Warning: filesize() [function.filesize]: stat failed for vertical.gif in ... galeria.php on line 57

  1. $thumbdir = "./"; // działa ok
  2. $thumbdir = "./grafika"; // wywala błędy
  3.  
  4. while ( $file = readdir($handle) ) {
  5. if ( isValid($file,$sfxs) ) {
  6. $tmp = array();
  7. $tmp[0] = $file;
  8. $tmp[1] = filemtime($file);
  9. $tmp[2] = filesize($file);
  10. array_push($images,$tmp);
  11. // miniaturki
  12. if ( !file_exists($thumbdir.$slash.$file) ) {
  13. createThumb($file);
  14. }
  15. }
  16. }

Przeszukałem Google, również to forum - niby znalazłem sporo materiału, ale nie potrafiłem sobie poradzić...


--------------------
Go to the top of the page
+Quote Post
erix
post 9.01.2010, 18:22:34
Post #2





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Wychodzi na to, że problem jest ze ścieżkami.

Pokaż faktyczną strukturę drzewa katalogowego. Poza tym, to nie jest pełny kod.


--------------------

ZCE :: Pisząc PW załączaj LINK DO TEMATU i TYLKO w sprawach moderacji :: jakiś błąd - a TREŚĆ BŁĘDU? :: nie ponaglaj z odpowiedzią via PW!
Go to the top of the page
+Quote Post
Kshyhoo
post 9.01.2010, 18:26:52
Post #3





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




"problem ze ścieżkami" taką informację już wugooglowałem. Dlaczego więc wyświetlają się grafiki? Cała galeria:
CODE
<?

// maximum width of images (height will be scaled accordingly)
$maxw = 100;
// directory to place thumbnail images
$thumbdir = "./"; // wyświetla się dobrze
// $thumbdir = "./gfx"; // wyświetla się z błędami
// number of columns in table
$cols = 5;
// number of rows in table
$rows = 5;
// default sorting method ["date","size","name"]
$df_sortby = "name";
// default ordering (ascending or descending) ["ASC","DESC"]
$df_ascdsc = "ASC";

/* INITIALISATION */
// useful shortcut
$url = $_SERVER["PHP_SELF"]."?";

// start point and ordering
$start = ( isset($_GET["start"]) ) ? $_GET["start"] : 0;
$sortby = ( isset($_GET["sortby"]) ) ? $_GET["sortby"] : $df_sortby;
$ascdsc = ( isset($_GET["ascdsc"]) ) ? $_GET["ascdsc"] : $df_ascdsc;

// allowed image types
$sfxs = array(".jpg",".jpeg",".png",".gif");

// get the current directory path:
$curdir = dirname(stripslashes($_SERVER["PATH_TRANSLATED"]));

// get folder delimiter for this system:
$slash = ( strstr($curdir,"\\") ) ? "\\" : "/";

$curdir = $thumbdir;

/* READ DIRECTORY */
// create array to hold filenames and information
// $images[n][0] = name
// $images[n][1] = date
// $images[n][2] = size
$images = array();

@ $handle = opendir($curdir);
if ( !$handle ) {
error("could not open directory!");
exit;
}

while ( $file = readdir($handle) ) {
if ( isValid($file,$sfxs) ) {
$tmp = array();
$tmp[0] = $file;
///

///
$tmp[1] = filemtime($file);
$tmp[2] = filesize($file);
array_push($images,$tmp);
// do we need to create a thumbnail?
if ( !file_exists($thumbdir.$slash.$file) ) {
createThumb($file);
}
}
}
// now sort the dir:
usort($images,"sortme");

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html>
<head>
<title>Galeria</title>
<style>
body, td, input, select {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 0.8em;
}
a {
font-weight: bold;
}
.img {
font-size: 0.7em;
border: 1px solid #666666;
text-align: center;
vertical-align: bottom;
padding: 5px 2px 5px 2px;
}
</style>
</head>

<body>

<?
/* CREATE TABLE */
$k = $start;
$n = count($images);
$ord = "&sortby=".$sortby."&ascdsc=".$ascdsc;
if ( $start > 0 ) {
$pn = $start - $rows*$cols;
$prev = '<a href="'.$url.$ord.'&start='.$pn.'">poprzednia</a>';
} else {
$prev = "poprzednia";
}
if ( $start + $rows*$cols < $n) {
$nn = $start + $rows*$cols;
$next = '<a href="'.$url.$ord.'&start='.$nn.'">następna</a>';
} else {
$next = "następna";
}
if ( $n-$k < $rows*$cols ) {
$rows = ceil(($n-$k)/$cols);
}
?>

<table border="0" align="center" width="<?=$cols*$maxw*1.4?>">
<tr>
<td colspan="<?=$cols?>" align="center">
<form name="f" action="<?=$_SERVER["PHP_SELF"]?>" method="GET">
<input name="start" type="hidden" value="<?=$start?>">
sortuj
<select name="sortby">
<option value="-1">-- sortuj --</option>
<option value="date"<? if ($sortby=="date") echo " SELECTED"?>>data</option>
<option value="size"<? if ($sortby=="size") echo " SELECTED"?>>rozmiar</option>
<option value="name"<? if ($sortby=="name") echo " SELECTED"?>>nazwa</option>
</select>
<input name="ascdsc" value="ASC" type="radio"<? if ($ascdsc=="ASC") echo " CHECKED"?>>rosnąco
<input name="ascdsc" value="DESC" type="radio"<? if ($ascdsc=="DESC") echo " CHECKED"?>>malejąco
<input type="submit" value="sortuj">
</form>
</td>
</tr>
<tr>
<td colspan="<?=$cols?>"><hr></td>
</tr>
<tr>
<td colspan="<?=$cols?>">
<table width="100%">
<tr>
<td><?=$prev?></td>
<td align="right"><?=$next?></td>
</tr>
</table>
</td>
</tr>
<?
for ( $i=0; $i<$rows; $i++ ) {
?>
<tr>
<?
for ( $j=0; $j<$cols; $j++ ) {
?>
<td class="img">
<?
if ( $k < $n ) {
?>
<a href="<?=$images[$k][0]?>" target="blank">
<img src="<?=$thumbdir."/".$images[$k][0]?>" border="0"><br />
<?=$images[$k][0]?>
</a><br />
<?=round($images[$k][2]/1024)?>Kb, <?=date("d-m-y",$images[$k][1])?>
<?
} else {
?>
&nbsp;
<?
}
?>
</td>
<?
$k++;
}
?>
</tr>
<?
}
?>
<tr>
<td colspan="<?=$cols?>"><hr></td>
</tr>
</table>

</body>
</html>

<?

/*

USEFUL FUNCTIONS

*/

function isValid($f,$a) {
$t = getSuffix($f);
return ( in_array($t,$a) ) ? true : false;
}

function getSuffix($f) {
$n = strrpos($f,".");
return substr($f,$n,strlen($f)-$n);
}

function createThumb($f) {
// use max width config value
global $maxw, $curdir, $copyfile, $thumbdir, $slash;
$input = 0;
$type = getSuffix($f);
// png or jpeg?
// either way get image
if ($type==".png") {
$input = imagecreatefrompng($f);
} else {
$input = imagecreatefromjpeg($f);
}
if ( !$input ) {
error("not a valid image file sad.gif");
return false;
}
// get size ( [0]=width, [1]=height )
$tmp = getimagesize($f);
if ( !$tmp ) {
error("Could not get input image size");
return false;
}
// get width of new thumbnail by taking
// the smaller value from max and tmp width
$w = ($tmp[0]>$maxw) ? $maxw : $tmp[0];
// scale height according to width
$h = $tmp[1] * ($maxw/$tmp[0]);
// create output image with true color JPG Thumbnail if imagecreatetruecolor() is present
if (function_exists(imagecreatetruecolor)){
@ $output = imagecreatetruecolor($w,$h);
}else{
@ $output = imagecreate($w,$h);
}
if ( !$output ) {
error("could not create output image");
return false;
}
// copy big image over to thumbnail, and resize down
imagecopyresized( $output,$input, 0,0, 0,0, $w,$h, $tmp[0],$tmp[1] );
$newfile = $thumbdir.$slash.$f;
// do the outputting!
if ( $type == ".png" ) {
imagepng($output,$newfile);
} else {
imagejpeg($output,$newfile);
}
}

function sortme($a,$b) {
// setup
global $sortby, $ascdsc;
if ( $sortby == "name" ) {
$n = 0;
} elseif ( $sortby == "date" ) {
$n = 1;
} elseif ( $sortby == "size" ) {
$n = 2;
}
$m = ( $ascdsc == "ASC" ) ? 1 : -1;
if ( $a[$n] == $b[$n] ) return 0;
return ($a[$n] > $b[$n]) ? $m : -1*$m;
}

function error($str) {
echo '<div style="background-color:#ffffff;color:#660000;font-weight:bold">'.$str.'</div>';
}

?>

</body>
</html>


EDIT: Poradziłem sobie... chyba wczorajsze zmęczenie nie dało pomyśleć:
  1. while ( $file = readdir($handle) ) {
  2. if ( isValid($file,$sfxs) ) {
  3. $tmp = array();
  4. $tmp[0] = $file;
  5. $tmp[1] = filemtime($thumbdir."/".$file);
  6. $tmp[2] = filesize($thumbdir."/".$file);
  7. array_push($images,$tmp);
  8. // Czy trzeba tworzyć miniatury?
  9. if ( !file_exists($thumbdir.$slash.$file) ) {
  10. createThumb($file);
  11. }
  12. }
  13. }

@erix, miałeś rację, że to ścieżki ślepe... winksmiley.jpg

Ten post edytował Kshyhoo 9.01.2010, 20:41:49


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 15.07.2025 - 18:02