Dynadot โ€” .com Registration $8.99

PHP search form

Spaceship Spaceship
Watch

Barrucadu

Established Member
Impact
64
Can someone make or teach me how to make a PHP search form for my image hosting site (wip) it should search for .gif, .jpg, .png and .bmp. It should also when they click on a result take the user to "/uploads/browseupload.php?file=[resultname].[resultextension]". If someone could do that it would be really great, thanks in advance.


if possible a bit of php that would load the image on the "/uploads/browseupload.php?file=[resultname].[resultextension]" page. thanks again.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
I'll do it if you can add to my reputation ;)

What you might want to do is store the files in a MySQL database (they're MUCH easier to search with instead of raw file names) and then search that...
 
0
•••
hmmm... id prefer to not use MySQL, i dont like it (dunno why). Reputation updated!
 
0
•••
Thanks, um, ... actually I guess I wasn't clear - I don't know how to do it without MySQL ... :-/
 
0
•••
you can do it without mysql

what is the search criteria for the images?
 
0
•••
Try this (i can up with it quickly though, so MIGHT be some errors)

It should work for the most part I believe :/
PHP:
<?php 

$form = "<form method=\"POST\" action=\"search.php\">"; 
$form .= "Search for: <input type=\"text\" name=\"criteria\"> 
             <input type=\"Submit\" name=\"Submit\" value=\"Submit\">"; 
$form .= "</form>"; 

// check is user submitted, if not, show the form 
if ($_POST['Search'] != "Search") 
{ 
    echo $form; 
} 
else 
{ 

$directory = "/home/username/public_html/images/"; 

if (is_dir($directory)) 
{ 
   $open_dir = opendir($directory); 
    while (($file = readdir($open_dir)) !== false) 
    { 
        // Place filenames into an array 
        $files[] = $file; 
    } 
    closedir($open_dir); 

    // files are in an array, check if search criteria meets any names in array 
    if (in_array($_POST['criteria'], $files)) 
    { 
        foreach ($files AS $file_result) 
        { 
            if (stristr($file_result, $_POST['criteria'])) 
            { 
               echo "<a href=\"uploads/browseupload.php?file=".$file_result."\"></a><br />"; 
            } 
        } 
    } 
    else 
    { 
        echo "Search Criteria not found<br /> Please try again<br /><br />"; 
        echo $form; 
    } 
} 

} 

?>

Hope this helps :)
 
Last edited:
0
•••
thanks, i'll test it when i can get PHP on my webserver working.

ive just realized something.. I dont know how to use URL parameters in PHP, whats the syntax to get the parameter?
 
0
•••
0
•••
... don't forget the ; at the end of the line.
 
0
•••
searching images by filename?! You'd be better off just showing a bazillion thumbnails... putting keywords in a database would be much more effective.
 
0
•••
Yeah, in fact, you should store each image in the database as a BLOB. I know it slows things down, but would be much more effecient otherwise. You could store the uploader's IP address with it as well, plus the upload date, time, and even keywords for searches! Then order them how you want, prune old images, etc, etc! It would be a fabulous alternative.

Example - you could have a database table with these rows in it:

imageid - BIGINT - 10 - UNSIGNED - auto_increment PRIMARY KEY (unique ID no.)
imagedata - BLOB (this would be the actual image as binary)
submitter_ip - TEXT (i just use text for this, but this is the IP address of the uploader)
keywords - TEXT (seperate by spaces, maybe, then use explode() to search)
date - DATE (upload date)
time - TIME (time of upload)
views - BIGINT - 10

Now, I added that last field in because you will have to access the image BLOB via a .php file, right? Why not keep track of the number of views an image gets? Might be a neat little extra feature. Just put this in the PHP file before it sends the image to the browser. Do whatever you like! ^_^
 
0
•••
Mikor said:
hmmm... id prefer to not use MySQL, i dont like it (dunno why). Reputation updated!

Thats why I used the method above :-/
I agree that mysql is a better option...
 
0
•••
DONT store images as a blob in a mysql database its not what its designed for and if your site recieves and traffic it will grind to stand still.

just keep the images in a folder and record the filename and path in the mysql database.
 
0
•••
Do you realize how LARGE that folder will become? MySQL is designed to handle large databases! Large folders take longer to search through!
 
0
•••
if you re-read my post

myslef said:
just keep the images in a folder and record the filename and path in the mysql database.

you dont need to search through a folder if you know the images name and path now do you?

mySQl is designed for storing large amounts of data but that data should be in the form of many small records rather than few large records. Storing large files in Blobs is MESSY and should be avoided at all cost.
 
0
•••
The point is, though, directories aren't built to handle hundreds, or thousands of files in them. Having lots in 1 directory is still going to slow it down, whether you know the filename or not.
 
0
•••
compuXP said:
The point is, though, directories aren't built to handle hundreds, or thousands of files in them. Having lots in 1 directory is still going to slow it down, whether you know the filename or not.

directories are just files which contain lists of file records

you can store thousands of files in a directory with very little reduction in speed

if you look at a linux distros architecture some of the directories contain 10's of thousands of files so no offence but you're wrong a directory can be used to store A LOT of files.
 
Last edited:
0
•••
Directories are designed to handle many SUBdirectories, to divide the files and make them easier to work with.

Mikor - use MySQL blobs -_- They're much easier to handle. And no slower than storing the images in 1 directory.
 
0
•••
i'm sorry but you have no idea what you're talking about. when you also have a degree in computer science we can sit down and discuss this on a level playing feild but until then prehaps you could refrain from giving out false information and bad advice

compuXP said:
Directories are designed to handle many SUBdirectories, to divide the files and make them easier to work with.

Mikor - use MySQL blobs -_- They're much easier to handle. And no slower than storing the images in 1 directory.

a sub directory is a directory and a directory is a file so therefore by simple dedtuction directories can handle many files :snaphappy:

mysql becomes ALOT slower if you start using using blobs, if you read up about mySQL optimisation on the mySQL website you will see that they recommend avoiding storing binary objects in a database.
 
Last edited:
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back