Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2002
    Posts
    215

    easiest way to do this in php?

    I'm looking to make a SIMPLE (barebones) script that allows me to upload images, label them somehow with tags, and have them be searchable.

    A user visits my site, enters something like "smile" into a search box, and the images that are smiles appear.

    How would I do this?

  2. #2
    Join Date
    Mar 2004
    Posts
    1,468
    What do you mean by "label them somehow with tags"?

    Go to :
    http://php.resourceindex.com/Complet...ile_Uploading/

    Then check out the last five scripts towards the bottom, none of the other ones work.

    Hope that helps,
    Jason
    Haven't been on WHT for 6 years!

  3. #3
    Join Date
    Aug 2002
    Posts
    215
    Thats kinda what im looking for, but not really.

    I'm looking for an upload script that basically uploads the file, then lets you enter keywords for the image. That way, users can search the images. This is all the functionality I need.


    Thanks!

  4. #4
    Join Date
    Mar 2004
    Posts
    1,468
    Then just under the field for the image, add a field for keywords.

    Then have a database (such as MySQL) with a table that has two fields, one for the image url, and one for the keywords.

    Then, select from table where keywords like $keywordstring.

    Sorry, I can't write the code for you, but hopefully, if you know PHP, this will get you going.
    Haven't been on WHT for 6 years!

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Well, since I'm feeling generous today ... I'll slap something together for you. No guarantees that it will work (most probably it will), but hey don't say you never got anything for free

    First, the SQL for the database table:

    PHP Code:
    CREATE TABLE `upload_images` (`idINT NOT NULL AUTO_INCREMENT PRIMARY KEY, `fnameVARCHAR(255));

    CREATE TABLE `image_keywords` (`idINT NOT NULL AUTO_INCREMENT PRIMARY KEY, `img_idINT, `keywordVARCHAR(255)); 
    Now the settings file (this is the only file that you would modify):

    PHP Code:
    <?php

        
    /**
           * Filename : settings.php
           * 
           * Purpose : contains system-wide settings
        **/

        // -- Database specific information
        // -- using constants here, so that
        // -- information cannot be overwritten
        // -- by a script attack.

        
    define('DB_HOST','localhost');
        
    define('DB_USER','dbusername');
        
    define('DB_PASS','secretpass');
        
    define('DB_NAME','mydb');

        
    // -- Image related
        // -- Make sure upload directory is chmod 775

        
    define('IMG_UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/images/upload/');
    define('IMG_URL','http://'.$_SERVER['HTTP_HOST'].'/images/upload/');
    ?>
    Configuration File (you won't modify this)
    PHP Code:
    <?php

        
    require_once 'settings.php';

        function 
    randfilename($ext ""
      {
        if (
    $ext == "") { return strtolower(substr(md5(uniqid(time())), 08)); }
        if (
    $ext != "") { return strtolower(substr(md5(uniqid(time())), 08)).$ext; }
      }

       
    $conn mysql_connect(DB_HOST,DB_USER,DB_PASS);
       if (!
    $conn) { die(mysql_error()); }

       if (!
    mysql_select_db(DB_NAME)) { die(mysql_error()); }

       
    /** This function will insert an image along with its
             associated keywords into the database.

             You pass it the image file name and an array
             of keywords.

       **/
       
    function insertImage($filename,$keywords = array())
       {
             
    $query  "INSERT INTO `upload_images` (`id`,`fname`) ";
             
    $query .= "VALUES ('','".mysql_real_escape_string($filename)."')";

             
    $result mysql_query($query);
             if (!
    $result) { die($query."<br />".mysql_error()); }

             
    $image_id mysql_insert_id();

             if (
    is_array($keywords) && sizeof($keywords) > 0)
             {
                 
    // Lets add some keywords
                 
    foreach($keywords as $word)
                 {
                     
    $query "INSERT INTO `image_keywords` VALUES ('',";
                     
    $query .= $image_id.",'".mysql_real_escape_string($word)."')";
                     
    $result mysql_query($query);
                     if (!
    $result) { die($query."<br />".mysql_error()); }
                 }
             }
             return 
    true;
       }
    ?>
    Finally, the upload form itself :

    PHP Code:
    <form enctype="multipart/form-data" method="POST" action="<?php echo basename($_SERVER['PHP_SELF']) ?>">
    Enter the filename : <input name="imgfile" type="file" /><br />
    Enter some keywords (one per line) : <textarea rows="10" cols="20" name="keywords"></textarea><br />
    <input type="submit" name="submit" value="Go" />
    </form>

    <?php

        
    if ($_SERVER['REQUEST_METHOD'] == "POST")
        {
             require_once 
    'config.php'// configuration file
             
    $fname randfilename(substr($_FILES['imgfile']['name'],strrpos($_FILES['imgfile']['name'],".")));
         
    $loc IMG_UPLOAD_DIR.$fname;
             if (!
    move_uploaded_file($_FILES['imgfile']['tmp_name'], $loc))
         {
             die(
    "Cannot upload image file.<br />Check the permissions for $loc");
         }
             
    $keywords explode("\n",$_POST['keywords']);
             if (
    insertImage($fname,$keywords))
             {
                 echo 
    'Thank you, image has been uploaded';
             }
       }
    ?>
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  6. #6
    Join Date
    Aug 2002
    Posts
    215
    WOW! Thanks alot. That's pretty much exactly what I was looking for. However, 2 concerns:

    1) Here is the upload script in action (go ahead and try it):
    http://www.exit-row.com/wow/upload.php

    I fill out the form and click upload, and get these errors. Ive chmodded everything to 777.

    Warning: move_uploaded_file(/nfsn/content/exitrow/htdocs/images/upload/0d330a58.bmp): failed to open stream: No such file or directory in /nfsn/content/exitrow/htdocs/wow/upload.php on line 14

    Warning: move_uploaded_file(): Unable to move '/nfsn/content/exitrow/tmp/phpfthP0c' to '/nfsn/content/exitrow/htdocs/images/upload/0d330a58.bmp' in /nfsn/content/exitrow/htdocs/wow/upload.php on line 14
    Cannot upload image file.
    Check the permissions for /nfsn/content/exitrow/htdocs/images/upload/0d330a58.bmp

    2) How can I search the database now?

    Any ideas?

  7. #7
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Check the permissions for /nfsn/content/exitrow/htdocs/images/upload/0d330a58.bmp

    AND

    Make sure upload directory is chmod 775

    Should solve that issue.

    Oh c'mon -- you want me to write the search database part too? I'm sure someone else can
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  8. #8
    Join Date
    Aug 2002
    Location
    Southwest Michigan
    Posts
    935
    My recomendation for search is that you dive into programming a bit and learn to do it. I don't think a fulltext search is neccesary here, so use %like%

    Research and this will be easy. Fyrestrtr has already done a great job of geting you more than rolling

  9. #9
    Join Date
    Aug 2002
    Posts
    215
    Originally posted by fyrestrtr
    Check the permissions for /nfsn/content/exitrow/htdocs/images/upload/0d330a58.bmp

    AND

    Make sure upload directory is chmod 775

    Should solve that issue.

    Oh c'mon -- you want me to write the search database part too? I'm sure someone else can
    Checked, and rechecked. I am 100% sure about the file permissions being correct, yet im still getting the same error.

  10. #10
    Join Date
    Feb 2003
    Location
    Connecticut
    Posts
    5,441
    Try 777 for kicks..

  11. #11
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    What are your permissions on '/nfsn/content/exitrow/tmp/' ?
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  12. #12
    Join Date
    Aug 2002
    Posts
    215
    Originally posted by fyrestrtr
    What are your permissions on '/nfsn/content/exitrow/tmp/' ?

    They are 777.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •