SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    take string in variable

    My Add articles section allow user to formatted input or insert picture

    such as when type

    [Bold] Hello [/Bold] result is Hello
    [Under] Hello again [/Under] result is Hello again

    and when user want to add image in articles, First, user upload image to temp folder in server and I use PHP to generate code and user can push code where he want to add picture such as

    [pic="5489.gif"]

    user can post many image and can formatted font

    PHP Code:
    [pic="1235.jpg"]
    [
    BoldHello world [/Bold]
    PHP is a server-sidecross-platformHTML embedded scripting language
    [pic="4586.gif"][pic="4586.gif"
    User type this code in textarea and I keep in single variable $articles and I keey value of $articles in database as Text, the problem is I want also keep all image that user add in database, but I don't how to take each picture name that in $articles.

    Can anyone help me?
    Is right way to keep all image file in to array and add to database later?
    I live in Thailand. My English grammar not well.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, to date, I have never actually stored an image into a binary blob field of a database. I have always stored images in the file system and stored their file name in the database. And for what you want to do, my instinct would be to do the following:

    1) When you create the table "articles" use a auto_increment unsigned int as the primary key.

    2) INSERT a new row (record) in the articles table of your database for this user supplied article.

    3) Query mysql to find out what primary key it assigned to the new row by using mysql_insert_id()

    4) Prepend the primary key to the user provided name of all the user uploaded files. For example, if the user uploaded a file with the name "foo.jpg" you will rename it "123_foo.jpg" when you save it to the file system (where 123 is the value of the primary key for that article as determined in step 4). This ensures that each image will have a unique file name.

    5) Now when you pull the article out of the database using a SELECT query, you know that when you come accross your mark-up tags - in the field that holds the article text - for example[pic="foo.jpg"], you know that you need to do a regular expression replace and subtitute in something like <img src="images/123_foo.jpg">

    Anyway, that is just how I would instinctively do it. I'm sure there are pros and cons of keeping binary files in the files system versus storing them as blobs in the database.

    P.S. One day when we are all using object-oriented DBMS then we will throw everything into databases be they text, binary, pointer, etc because integration with our code will be seemless (that is no sql invlolved). But for RDBMS I don't see much merit - intuitively - in storing BLOBs in a database. Some one else might be able to enlighten me for which I would be grateful.
    Last edited by freakysid; Aug 25, 2001 at 22:15.

  3. #3
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, freakysid but I don't understand step 2-4.
    For simple if my table has only 2 column ID(primary key) and Detail(variable from textarea)
    In step 2 to 4, I understand that
    step 2 --> Insert only primary key to the lastest row (this step Detail is null)
    step 3 --> Query to find primary lastest key
    step 4 --> Prepend primary key to all user upload file (this step user can add article and update Detail column in table)


    1. Is I understand correct ?
    2. If correct, Can you show code example in step3
    3. Should I store all image in one folder, if one folder has many image file (about 1000 file). Is it slow to select 5 picture from 1000 picture and use <img src=pic.gif>

    Thanks.
    Last edited by itsource; Aug 26, 2001 at 09:17.
    I live in Thailand. My English grammar not well.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I'll write some code to illustrate my idea. But I won't be able to do this till tonight - so could be another 12 hours until I post - I got to look up the functions I am thinking about - I have a very bad memory.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My guess is that 1,000 files is not too many files to store in one directory. OK, here is some code (untested) that illustrates what I was thinking...

    Lets say your have a database table:

    CREATE TABLE Article(
    art_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
    art_text TEXT
    );
    PHP Code:
    <?php
    /*  Assumptions:
    1) You have opened a connection to mysql and selected the database.

    2) $articleText holds the text POST by the user

    3) In your form you created an array of file upload input boxes like:
       <input name="userfile[]" type="file">
       So now you script will have received the folowing variables via POST:
          $userfile[] - an array of the temp filenames on your server
          $userfile_name[] - an array of the filenames as they were named 
             on your user's computer.
          $userfile_size[]
          $userfile_type[]

    4) It is assumed that your user will have embedded [pic="xyz.gif"] type 
       tags and provided the correct file names to match the files they actually
       uploaded through the form. You could create some validation code to validate
       this if you wanted to.

    5) It is assumed that you have used strip_tags() and add_slashes(), etc,
       to user data to make it friendly.
    */

    // insert $articleText into a new row in table Article for the new article

    $sql "INSERT INTO Article SET art_text='$articleText'";
    $result mysql_query($sql);

    // grab the value of art_id for this new row
    $artId mysql_insert_id();

    // now loop through the uploaded files and:
    // - create a string as the file name under which they are to be saved locally.
    // - then copy the file into its location using the file name
    for ($i 0$i count($userfile); i++) {
       
    $newFileName $artId '_' $userfile_name[$i];
       
    copy($userfile[$i], "/path/to/image/dir/$newFileName");
    }
    ?>
    As an example, if the user uploaded files "foo.jpg", "bar.jpg", and the value of the auto-incrementing primary key for the new record is 123, they will be saved as:

    /path/to/image/dir/123_foo.jpg
    /path/to/image/dir/123_bar.jpg

    Now when you want to grab the article out of the database, you will have to do a regular expression find and replace for the file names inside your mark up and remember to append the value of art_id for the record onto them to match them to the file names as they are stored in the file system.

  6. #6
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My Idea that already written in PHP like you, but little different

    I have only one <input type=text> for user upload file to server.
    user can upload many file, first user can click browse to select file that want upload, for example network.jpg then I write PHP that upload and copy this picture to my tempfolder and gererate code such as

    [pic="45878545852_network.jpg"]

    I also use time() to append file name to prevent same file name from one member. user must copy this code put in textarea where he want to paste picture and user can preview article and image any time and can upload next picture and paste code to textarea.

    In textarea I check that if found [pic="45878545852_network.jpg"] and translation to

    <img src="tempfolder/45878545852_network.jpg">

    I upload all picture to tempfolder because I want user to preview picture and some user only want to test picture, he may upload 10 picture and preview it, and when submit he can delete picture in textarea and submit 5 picture.

    So. There is many picture that user may test upload and preview in tempfolder that not is in article.

    I think that only when user submit article to database, I will search throught varible in textarea, that is the picture that really picture in article

    PHP Code:
    [pic="1235.jpg"]
    [
    BoldHello world [/Bold]
    PHP is a server-sidecross-platformHTML embedded scripting language
    [pic="4586.gif"][pic="4586.gif"
    and keep all picture in array and assign $artid prepend to all image file later.

    The problem is how can I search image file in string and keep each image file name in array.
    I live in Thailand. My English grammar not well.

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I totally understand your question - but you are going to need a good regular expression replace to search the string and replace the file names. For example you will want to skip whitespace so that "[pic=" and "[ pic = " both work. I am hopeless at regular expressions - but I am sure someone will come up with what you need


Bookmarks

Posting Permissions

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