O script consiste em 7 arquivos sendo eles:
criarcateg.php - usado para criar uma categoria
enviar.php - para fazer o upload de imagens
gravacat.php - grava a categria descrita no criarcateg.php
mini.php - mostra as imagens selecionadas pelo ver.php
n.txt - grava a quantidade de categoria criadas
postar.php - armazena os dados das categoria
ver.php - seleciona a categoria e mostrar as imagens pelo arquivo mini.php
é necessário ainda criar uma pasta chamada "imagens"
Bom, como podem notar não está incluido nenhum modo de autenticação de usuário (proximo tutorial, espero....) nem arquivo css, porém é bem simples acrescentar esse tipo de script.
Vamos ao script:
Página criarcateg.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <div id="quadro"> <h1>Criar nova Galeria de fotos</h1> <br /> <form action="gravarcat.php" method="get" name="postagem" target="_self" class="style6"> <fieldset> <legend>Use somente dados...</legend> <label for="pasta">Pasta :</label> <input name="pasta" type="text" class="campo" size="30" id="pasta" /> <span>Nome da pasta sem o uso de caracteres especiais ou "espaços" </span>.<br /> <br /> <label for="titulo">Titulo :</label> <input name="titulo" type="text" class="campo" size="30" id="titulo" /> <span>Titulo da galeria que será mostrado.</span><br /><br /> <br /><br /> <input name="Submit" type="submit" class="botao" value="Enviar" /> <input name="Submit2" type="reset" class="botao" value="Limpar" /> </fieldset> </form> </div> </body> </html>
enviar.php
<html>
<body>
<?
include("postar.php");
if($acao == "gravar") {
$pasta = $eventos[$opt]['pasta'];
if($file != "" or $file2 != "" or $file3 != "" or $file4 != ""){
$fp = fopen("$pasta/n.txt", "r");
$n = fgets($fp, 500);
fclose($fp);
$ex = $n;
if ( $file != ""){
$n = $n+1;
$fp = fopen("$pasta/n.txt", "w+");
fputs($fp, $n);
fclose($fp);
$nomearquivo = "$ex";
$none = $nomearquivo;
$status = move_uploaded_file($file,"$pasta"."/"."$nomearquivo.jpg");
chmod ("$pasta"."/"."$nomearquivo.jpg", 0777);
}
if ( $file2 != ""){
$ex = $ex+1;
$n = $n+1;
$fp = fopen("$pasta/n.txt", "w+");
fputs($fp, $n);
$nomearquivo = "$ex"; $none = $nomearquivo;
$status = move_uploaded_file($file2,"$pasta"."/"."$nomearquivo.jpg");
chmod ("$pasta"."/"."$nomearquivo.jpg", 0777);
}
if ( $file3 != ""){
$ex = $ex+1;
$n = $n+1;
$fp = fopen("$pasta/n.txt", "w+");
fputs($fp, $n);
fclose($fp);
$nomearquivo = "$ex"; $none = $nomearquivo;
$status = move_uploaded_file($file3,"$pasta"."/"."$nomearquivo.jpg");
chmod ("$pasta"."/"."$nomearquivo.jpg", 0777);
}
if ( $file4 != ""){
$ex = $ex+1;
$n = $n+1;
$fp = fopen("$pasta/n.txt", "w+");
fputs($fp, $n);
fclose($fp);
$nomearquivo = "$ex"; $none = $nomearquivo;
$status = move_uploaded_file($file4,"$pasta"."/"."$nomearquivo.jpg");
chmod ("$pasta"."/"."$nomearquivo.jpg", 0777);
}
die('<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
Sua Foto Foi Cadastrada com Sucesso!
');
}else{
die('<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
Erro... Prencha todos os campos
');
}}
?>
<div id="quadro">
<h1>Cadastrar imagens em uma Galeria</h1>
<br />
<form method="post" action="enviar.php?acao=gravar" enctype="multipart/form-data">
<fieldset>
<legend>Use somente arquivos com formato JPG.</legend>
<label for="opt">Galeria :</label>
<select name="opt" class="campo" id="opt">
<?php
$fp = fopen("n.txt", "r");
$n = fgets($fp, 500);
fclose($fp);
$cont = 0;
do {
echo '<option value="' .$cont. '">' . $eventos[$cont]['titulo'] . '</option>';
$cont = $cont+1;
} while ( $cont < $n );
?>
</select>
<br />
<br />
<label for="opt1"> Foto 1 : </label>
<input name="file" type="File" class="campo" size="30" id="opt1" />
<br />
<br />
<label for="opt2"> Foto 2 :
<input name="file2" type="File" class="campo" size="30" id="opt2" />
<br />
<br />
<label for="opt3"> Foto 3 :
<input name="file3" type="File" class="campo" size="30" id="opt3" />
<br />
<br />
<label for="opt4"> Foto 4 :
<input name="file4" type="File" class="campo" size="30" id="opt4" />
<br /><br /><br />
** CUIDADO PARA NÃO ESQUECER DE SELECIONAR A PASTA DA GALERIA **
<br />
<br /><br />
<input name="salvar" type="Submit" class="botao" value="Cadastrar" />
<input name="limpar" type="Reset" class="botao" value="Limpar" />
</p>
</fieldset>
</form>
</div>
</body>
gravacat.php
<html>
<body>
<div id="quadro">
<?php
$nome_arquivo = "";
$fp = "";
if ($pasta !="" and $titulo !="") {
$vali = is_dir("imagens/".$pasta);
if ($vali != "1") {
$nome_arquivo = "postar.php";
$fp = fopen($nome_arquivo, "a+");
fseek($fp, 800);
fputs($fp,"\n", 30);
fputs($fp,'<?php', 512);
fputs($fp,"\n", 512);
fputs($fp,'$eventos[] = array(', 512);
fputs($fp,"\n", 512);
fputs($fp,' \'pasta\' => \'', 512);
fputs($fp,'imagens/', 512);
fputs($fp, $pasta, 512);
fputs($fp,'\',', 512);
fputs($fp,"\n", 512);
fputs($fp,' \'titulo\' => \'', 512);
fputs($fp, $titulo, 512);
fputs($fp,'\'', 512);
fputs($fp,"\n", 512);
fputs($fp,');', 512);
fputs($fp,"\n", 512);
fputs($fp,'?>', 512);
fclose($fp);
$res = mkdir("imagens/".$pasta , 0777);
chmod ("imagens/".$pasta ,0777);
$fdp = fopen("imagens/$pasta/n.txt", "a+");
chmod ("imagens/$pasta/n.txt", 0777);
fputs($fdp, 0);
fclose($fdp);
if ($res == "true") {
echo 'Galeria Criada com Sucesso!! ';
$fp = fopen("n.txt", "r");
$n = fgets($fp, 500);
$n = $n+1;
$fp = fopen("n.txt", "w+");
fputs($fp, $n);
fclose($fp);
}
}
if ($vali == "1") {
echo ' ERRO!!! <br /><br /> Pasta existente, tente novamente...';
}}else {
echo ' ERRO!!! <br /><br /> Campos em branco, tente novamente...';
}
?>
</div>
</body>
</html>
mini.php
<html>
<?php
include("postar.php");
function get_file_extension($file)
{
$file = strtolower($file);
$extension = split("[/\\.]", $file);
$n = sizeof($extension) - 1;
return $extension[$n];
}
function is_valid_file_extension($file, $types = array())
{
$ext = get_file_extension($file);
return in_array($ext, $types) ? true : false;
}
$cfg = array();
$eventos = array();
$cfg['num_colunas'] = 2;
$cfg['file_exts'] = array('jpg','jpeg','gif', 'png');
include("postar.php");
reset($eventos);
?>
<head>
<title>Galeria - <?php echo $eventos[$galeria]['titulo']; ?></title>
<script type="text/javascript">
function show(url,larg,alt){
newWindow=window.open(url, '', "toolbar=0 ,location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, " + 'width=' + larg + ', height=' + alt);
}
</script>
</head>
<body>
<div id="quadrog">
<?php echo $eventos[$galeria]['titulo']; ?>
<div id="miniaturas">
<?php
if (isset($_GET['galeria'])){
$galeria = intval($_GET['galeria']);
} else {
die("Galeria não informado");
}
if (file_exists($eventos[$galeria]['pasta']))
{
$dir = @opendir($eventos[$galeria]['pasta']);
}
else
{
die("Pasta não encontrada");
}
?>
<?php
$arr_imagens = array();
while (false !== ($file = readdir($dir)))
{
if ($file != '.' && $file != '..')
{
if (is_valid_file_extension($file, $cfg['file_exts']))
{
$arr_imagens[] = $file;
}
}
}
$k = sizeof($arr_imagens);
for ($i=0; $i<$k; $i++)
{
echo '<a href="java script:show(\' '. $eventos[$galeria]['pasta'] .'/' . $arr_imagens[$i] . '\' ,504 ,494)"><img class="mini" src=' . $eventos[$galeria]['pasta'] .'/' . $arr_imagens[$i] . ' width="130" height="90"></a>' . "\n";
}
?>
</div>
</div>
</body>
</html>
n.txt (valor inicial zero)
0
postar.php (arquivo vazio... apenas para evitar erro na hora de criar e modificar o arquivo)
ver.php (aqui tem apenas um detalhe que gostaria de explicar, o script vai pegar as categorias e atribuir um valor "galeriae" ou "galeriad" alternadamente com o objetivo desse valores servirem para fazer um alinhamento lado a lado... Ex: a primeira categoria recebe "galeriae" a segunda "galeriad" a terceira novamente "galeriae" assim por diante. podemos setar esse valor como id de uma div e alinhar usando css...)
<html>
<head>
<title>Galeria de Fotos</title>
</head>
<body>
<div id="quadrog">
<?php
include("postar.php");
$num_colunas = 0;
for ($i=0; $i<sizeof($eventos); $i++)
{
$vali = $i%2;
if ($vali == 0){
$posi = galeriae;
}
if ($vali == 1){
$posi = galeriad;
}
echo '<div id="'. $posi .'">';
echo '<div id="galeriat">';
echo '<a class="texto" href="mini.php?galeria='. $i .'">' . $eventos[$i]['titulo'] . '</a>';
echo '</div>';
echo "</div>";
echo "\n";
}
?>
</div>
</body>
</html>
É isso, espero que vocês possam fazer um bom proveito desse script!
Vlw Galera!

Entrar
Cadastre-se
Ajuda
Responder


Quote


