Jump to content


php script to display image or txt depending on the extention


10 replies to this topic

#1 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 26 June 2006 - 05:07 PM

i want to make a script that display an image and if the image has a .txt with the name display that underneath but i dont want it to just do it for one group of image and txt files i would like it to do it over a whole folder, how could i do this the script that i was using before would only look in the folder for the whole file name and display them all in an image link even if it wasnt a image file, after many hours try to reactify this problem i have come to the conclusion that this script would be useless for what i want it to do now?

so how cud i do it i theroy its possible but i just cant seem to code it wrightly to do what i want (maby cus im a noob starting off in php ;) )


the theroy of the code:

[indent]$dir=search dir of for files
[indent]if $dir = $dir ".jpg, .gif, .png"
[indent]then display image
[indent]if $dir = $dir.txt
[indent]then include $dir.txt endif
[indent]end if[/indent] [/indent][/indent][/indent][/indent]add hair line then repeat script[/indent]


thats it what my theory of the script was but how can i develop it

Edited by fblade, 26 June 2006 - 05:13 PM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 26 June 2006 - 05:45 PM

Like i said before take the time to use a database, it will make the problem very simple and easy to manage it is so worth it!

Look around and I am sure you can get someone cheap / free to set it up, I would do it if you offered a bit of money, but like I said, well worth it!

Edited by deadly, 26 June 2006 - 05:46 PM.


#3 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 26 June 2006 - 05:51 PM

View Postdeadly, on Jun 26 2006, 11:45 PM, said:

Like i said before take the time to use a database, it will make the problem very simple and easy to manage it is so worth it!

Look around and I am sure you can get someone cheap / free to set it up, I would do it if you offered a bit of money, but like I said, well worth it!
well i would but i want to try and get a bit more knowledge in php first thats why im trying to do it all in php

edit- also i dont want to really msql if possible cus i have made my own upload script that i would really like to use

Edited by fblade, 26 June 2006 - 06:00 PM.


#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 26 June 2006 - 06:26 PM

you can edit the upload script to still add the image to the db, just need a query to add a record into the db.

#5 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 26 June 2006 - 06:33 PM

Quote

$dir=search dir of for files

if $dir = $dir ".jpg, .gif, .png"

then display image

if $dir = $dir.txt

then include $dir.txt endif

end if

add hair line then repeat script
bah, if you cant even get the correct structure, how do you expect to learn it!!!!?? it should be more like this:
$dir = dir containing files
	if $dir == *image extension*
		display image.
	endif

	if $dir == "*text extension*
		*include it??? i'll just link it...*
	endif
****

but seriously, heres the code....
<?
$imgs = array('.gif','.png','jpeg','.jpg', '.bmp');
$handle = opendir(".");
while($file = readdir($handle))
{
	if(!is_dir($file))
	{
		$ext = substr($file, -4, 4);
		
		if(in_array($ext, $imgs))
		{
			echo "<img src=\"$file\" /><br />\n";
		}
		
		if($ext == ".txt")
		{
			echo "<a href=\"$file\">$file</a><br />\n";
		}
	}
} 
?>

Edited by coolaid, 26 June 2006 - 06:34 PM.


#6 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 26 June 2006 - 07:07 PM

thanks coolaid i'll try that

okies whilst waiting for a response i sat down and merge a mysql tutorial into my script this is what i got but it errors on inserting mysql data but uploads the image any1 know what ?

......
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$file_name = $HTTP_POST_FILES['ufile']['name'];
$random_digit=date("dmy");
$path= "pimage/".$random_digit.$file_name;

$name= "www.64pixels.com".$path;
$lastname=$_POST['dec'];
$email=$_POST['author'];

if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
 // Insert data into mysql
$sql="INSERT INTO $tbl_name(name, dec, author)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successfuly sent data to mysql";
}

else {
echo "ERROR sending mysql data<br/>";
}

	echo "Successfuly uploaded file and inserted data in mysql <BR/>";
	echo "<BR>";
	echo "<a href='upload_rename.php'>Back to main page</a>";
}
else
{
echo "Error";
}
mysql_close(); ....


#7 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 26 June 2006 - 08:10 PM

well, you use $tlb_name as the table name, you didn't actually define a table name.

and to figure out what error you get, use the mysql_eror() function instead of an if-else statement, so replace this:
$sql="INSERT INTO $tbl_name(name, dec, author)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successfuly sent data to mysql";
}

else {
echo "ERROR sending mysql data<br/>";
}

with this:
$sql = "INSERT INTO $tbl_name(name, dec, author)VALUES('$name', '$lastname', '$email')";
$result = mysql_query($sql) or die(mysql_error());

so after you change "$tbl_name" to the actual table name, post back with the error message.

Edited by coolaid, 26 June 2006 - 08:11 PM.


#8 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 27 June 2006 - 08:38 AM

View Postcoolaid, on Jun 27 2006, 02:10 AM, said:

well, you use $tlb_name as the table name, you didn't actually define a table name.

and to figure out what error you get, use the mysql_eror() function instead of an if-else statement, so replace this:
$sql="INSERT INTO $tbl_name(name, dec, author)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successfuly sent data to mysql";
}

else {
echo "ERROR sending mysql data<br/>";
}

with this:
$sql = "INSERT INTO $tbl_name(name, dec, author)VALUES('$name', '$lastname', '$email')";
$result = mysql_query($sql) or die(mysql_error());

so after you change "$tbl_name" to the actual table name, post back with the error message.

sorry forgot to metion that i took that bit out as i know for a fact that theres nothing wrong with that bit i just posted a snippet which i managed to narrow it down to, thanks i'll try that

#9 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 29 June 2006 - 05:41 AM

sorry its been a bit but my mysql sever was down so i could access this but heres the error i get.

Quote

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dec, author)VALUES('http://www.64pixels.com/pimage/29060618586079664441722c3a1ad' at line 1

help please? thanks

#10 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 29 June 2006 - 10:30 AM

try this...

$sql = "INSERT INTO $tbl_name (`name`, `dec`, `author`) VALUES ('$name', '$lastname', '$email')";


#11 fblade

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 29 June 2006 - 11:14 AM

YAY it works now thanks so much





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users