SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres my problem. I have to find this out for someone...

    Basically, he enters an article into a database while adding it to a category. He also needs to upload am image into a directory, and give it a link in th database at the same time.

    Here is his structure.

    DB name = articles

    TABLE name = contents
    - id
    - pid
    - Text
    - Image Link
    - CategoryID

    TABLE anme = category
    - id
    - name



    Bascially, once in his CMS, he enter a article into a category, and the file must be saved into the directory named after the category. Also a link must be made to this file in 'Image Link'

    Does anyone know the code for this? And could tell me it....

    BTW, the folders for these categories are allready created...



  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly do you want the code for the uploading part or the storing the file name in the database?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, he has changed his mind, now, the image is uploaded to a dir called images.

    I need to code that uploads the image, into 'images' and also the code that inserts the links into the field called 'Image Link'

    Thanx

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your form had a file field named userimage after the form gets posted you have a variable named $userimage whcihc is the uploaded file with a weird temp name that the system gives it you also will have $userimage_name which is the name of the orginal file, $userimage_size the size of the file in bytes and $userimage_type the type of file.

    After the form submits you need to copy the file from the tmp location to the permanent home in this case the images folder

    so

    copy($userimage, "fullpath/to/imagesdir/".$userimage_name);

    And then just insert $userimage_name into the db however you had planned to do it the only catch you need enctype="multi-part/formdata" in the form tag
    <form action="" method="post" enctype="multi-part/formdata">
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a bit confused there.

    Say here is my form:

    <form action="insert.php" method="post" enctype="multi-part/formdata">
    <input type=file name="userimage">
    // rest of fields here....

    So basically the uploaded file is inserted into the directory using:

    <?

    copy($userimage, "home/username/images/".$userimage_name);

    ?>

    Then the link is inserted into dataabse

    <?

    $sql = "INSERT INTO articles SET " .
    "Image Link='$userimage_name'";
    if (mysql_query($sql)) {
    echo("<P>New article added</P>");
    } else {
    echo("<P>Error adding new article: " .
    mysql_error() . "</P>");
    }

    ?>

    Then when i called the page i would insert the image like this?

    <?
    $sql = mysql_query("SELECT * FROM articles WHERE id=$id");
    if (!$sql) {
    echo("<P>Error retrieving articles from database!<BR>".
    "Error: " . mysql_error());
    exit();
    }

    while ($get = mysql_fetch_array($sql)) {
    $image = $get["Image Link"];
    echo("<img src='/images/$image'>");
    }
    ?>

    Are you sure that this uploads the file?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, my example is a very general example you will need to make it work for your system yes if the folder exists and is set to 777 then you should be able to copy files into it, as for the code for copying the file, I am positive it is correct.o make sure the path is correct usually you would want /home if home is in the root level of the sevrer
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay thanx

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting an error:

    Warning: Unable to create '/home/petesmc/public_html/imagesbanner64.gif': Permission denied in /home/petesmc/public_html/up.php on line 7


    Line 7 is:

    copy($userimage, "/home/petesmc/public_html/images".$userimage_name);

    I have cmhoded to 777

    any ideas?

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah

    copy($userimage, "/home/petesmc/public_html/images/".$userimage_name);


    You forgot the slash between the images and the image name
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, How could i say, if userimage is null then userimage_name = smile.gif

    ???

    And how could i check that only *.gif, *.jpg and *.jpeg files are used

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($userimage_size > 0) {
    //You know they actually uploaded something
    }
    else {
    //Make image smile.gif
    }

    $okaytypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/jpg");


    if (!in_array($userimage_type, $okaytypes)) {
    print "you must either upload a gif or jpeg";
    }
    else {
    //Process upload
    }


    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx allot.....

  13. #13
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say i needed to uplaod 6 images....+ another images

    into fields
    userimage_name (this one must have something entered otherwise the defualt pic is smile.gif
    image_1
    image_2
    image_3
    image_4
    image_5
    image_6


    Now, image_1 - image_6 do not have to have images in thme. How owuld i do this:

    Here is my current code:
    --------------------------

    <?

    mysql_select_db("petesmc_articles1");

    if ($userimage_size > 0) {

    $okaytypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/jpg");
    if (!in_array($userimage_type, $okaytypes)) {
    print "you must either upload a gif or jpeg";
    } else {
    copy($userimage, "/home/petesmc/public_html/images/".$userimage_name);
    $sql = "INSERT INTO articles SET " .
    "Image='$userimage_name', " .
    "Text='$text'";
    if(mysql_query($sql)) {
    echo("Image submitted");
    } else {
    echo("there was an error uploading the image");
    exit();
    }
    } } else {

    $userimage_name = "smile.gif";
    $sql = "INSERT INTO articles SET " .
    "Image='$userimage_name', " .
    "Text='$text'";
    if(mysql_query($sql)) {
    echo("Image submitted");
    } else {
    echo("there was an error uploading the image");
    exit();
    }

    }
    ?>
    -----------------------
    So how would i do this?


  14. #14
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, i did it:
    -------------
    <?
    mysql_connect("");
    mysql_select_db("petesmc_articles1");

    if ($userimage_size > 0 ) {
    copy($userimage, "/home/petesmc/public_html/images/".$userimage_name);
    } else { $userimage_name = "smile.gif"; }

    if ($image1_size > 0 ) {
    copy($image1, "/home/petesmc/public_html/images/".$image1_name);
    } else { $image1_name = ""; }

    if ($image2_size > 0 ) {
    copy($image2, "/home/petesmc/public_html/images/".$image2_name);
    } else { $image2_name = ""; }


    if ($image3_size > 0) {
    copy($image3, "/home/petesmc/public_html/images/".$image3_name);
    } else { $image3_name = ""; }

    if ($image4_size > 0) {
    copy($image4, "/home/petesmc/public_html/images/".$image4_name);
    } else { $image5_name = ""; }

    if ($image5_size > 0) {
    copy($image5, "/home/petesmc/public_html/images/".$image5_name);
    } else { $image5_name = ""; }

    if ($image6_size > 0) {
    copy($image6, "/home/petesmc/public_html/images/".$image6_name);
    } else { $image3_name = ""; }

    if ($image7_size > 0) {
    copy($image7, "/home/petesmc/public_html/images/".$image7_name);
    } else { $image7_name = ""; }

    if ($image8_size > 0) {
    copy($image8, "/home/petesmc/public_html/images/".$image8_name);
    } else { $image8_name = ""; }

    if ($image9_size > 0) {
    copy($image9, "/home/petesmc/public_html/images/".$image9_name);
    } else { $image9_name = ""; }

    if ($image10_size > 0) {
    copy($image10, "/home/petesmc/public_html/images/".$image10_name);
    } else { $image10_name = ""; }


    $sql = "INSERT INTO articles SET " .
    "Image='$userimage_name', " .
    "Image1='$image1_name', " .
    "Image2='$image2_name', " .
    "Image3='$image3_name', " .
    "Image4='$image4_name', " .
    "Image5='$image5_name', " .
    "Image6='$image6_name', " .
    "Image7='$image7_name', " .
    "Image8='$image8_name', " .
    "Image9='$image9_name', " .
    "Image10='$image10_name', " .
    "Text='$text'";
    if(mysql_query($sql)) {
    echo("Image submitted");
    } else {
    echo("there was an error uploading the image");
    exit();
    }
    ?>


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
  •