Click to See Complete Forum and Search --> : Picture in Guestbook


saud_iqbal
05-26-2005, 06:10 PM
I need your help. Can you help me in adding the option of uploading and displaying the picture. I want that the user can upload his photo and the photo is displayed below his message. Is it possible with text database.

Any help.

The code is


<?
$gbfile='gb.txt';
$separator= '^';

//====================================
//This function will add one line to
//the end of file
//====================================
function add($str){
global $gbfile;
$tmp = trim($str);
$fp=fopen($gbfile,'a+');
flock($fp, LOCK_EX);
fwrite($fp, $tmp. "\n");
flock($fp, LOCK_UN);
fclose($fp);
}

//====================================
//Function below gets specified number
//of lines and returns an array
//====================================
function get($start, $end){
global $gbfile;
$records=array();
$filename="gb.txt";
$fp=fopen($gbfile,'r');
flock($fp, LOCK_SH);
$i=1;
$tmp=TRUE;
while($i<$start && !feof($fp)) {
$tmp=fgets($fp);
$i++;
}
while($i<=$end && !feof($fp)) {
$tmp=trim(fgets($fp));
if ($tmp) { array_push($records, $tmp); }
$i++;
}

flock($fp, LOCK_UN);
fclose($fp);
return($records);
}

//=============================================
//Start of the script
//=============================================
//If the method is post then add new message
//to the guestbook file
if ($REQUEST_METHOD=='POST' && $_POST['text']) {
$msg = str_replace($separator, '', htmlspecialchars($_POST['text']));
$author = str_replace($separator, '', htmlspecialchars($_POST['author']));
$email = str_replace($separator, '', htmlspecialchars($_POST['email']));
$homepage = str_replace($separator, '', htmlspecialchars($_POST['homepage']));

$tmp = implode($separator, array($msg, $author, $email, $homepage));
add($tmp);
}

//Listing part

$start=$_GET['start'];
$end=$_GET['end'];

if (!$end || $start<=0) { $start=1; }
if (!$end) { $end=10; }
if ($end<$start) { $end=$start+1; }
$show=$end - $start;

//Get records from file into array
$records = get($start, $end);

//For each record get each field
foreach ($records as $rec) {
$tmp = explode($separator, $rec);
$msg = $tmp[0];
$author = $tmp[1];
$email = $tmp[2];
$homepage = $tmp[3];

//=================================
//Outputting
?>
<HR>
Author: <?=$author?><br>
Email: <?=$email?><br>
Homepage: <?=$homepage?><br>
Text: <?=$msg?><br><br>
<?
}

//Pagination
if ($start>$show) {
$start-=$show;
$end-=$show;
print "<a href=gb.php?start=$start&end=$end>Prev $show</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if (count($records)!=0) {
$start+=$show*2;
$end+=$show*2;
print "<a href=gb.php?start=$start&end=$end>Next $show</a>";
}
else {
print "No more records found !";
}
}
else {
$start+=$show;
$end+=$show;
print "<a href=gb.php?start=$start&end=$end>Next $show</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
?>

olaf
05-27-2005, 02:56 AM
it's easy just asking...
Do you have any (PHP)skills? or a budget?

saud_iqbal
05-28-2005, 03:52 AM
I just want to know that is it possible with PHP or not. I want to include the picture in the guest book. Is there anyone that can solve my problem :confused:

Thanks

olaf
05-28-2005, 07:59 AM
Of course it's possible why not but it's not doen be some posting, its a very complex scripting job.

bokeh
05-28-2005, 07:54 PM
It's not complex! It's as simple as you make it.

Heres the upload form:

<form action="some_url.php" enctype="multipart/form-data" method="post">
Attach photo<input id="fileatt" type="file">
<input id="submit" type="submit" value="Send">


Here is the php:

<?php
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$uploads_directory = '/some/directory/'; // fill this in...
$upload_dir_and_filename = $uploads_directory . $fileatt_name;

if (is_uploaded_file($fileatt)) {
if ( move_uploaded_file($fileatt, $upload_dir_and_filename)) {
echo "success!";
}else{
echo "fail!";
}
}else{
echo "no file uploaded!";

}
?>


Just integrate that into what you already have. And when you print the guest book page just add this line:

if (is_uploaded_file($fileatt)) {
print ('<img src="http://domain.tld/' . $upload_dir_and_filename . '">');
}