SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image upload code problem

    hey,

    I am trying to have the ability for a single image to be uploaded to a specific folder on my site when I add a new article to my database. I also want it to be given a name based on the id of the article i have just inserted.

    The following code is what I put in to allow this to happen, but it doesnt work, and it is giving me an error on the line starting with

    $filename = ...

    PHP Code:
    $pid mysql_insert_id();

    if (
    "image/pjpeg" == $uploadedfile_type) {
     
    $extension ".jpg";
     } else {
     
    $extension ".gif";
    }
    $filename "http://www.nintendoodle.com/media/news/" $pid $extension;
    if (
    copy($uploadedfile$filename)) {
     echo(
    "<p>Image added successfully</p>");
     } else {
     echo(
    "<p>Error adding image</p>")

    Any help would be much appreciated. Thanks in advance!

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $filename needs to be based on your local file structure, ie /var/http/htdocs/images instead of basing it on your url.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgive me for being an idiot, but what would

    http://www.nintendoodle.com/media/news/

    be written as for the $filname variable?

    thanks

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $filename = $DOCUMENT_ROOT . "/media/news" should work.
    ck :: bringing chris to the masses.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn, its still not working.

    Here is the error message in full, I forgot to put it in earlier. Sorry.

    Parse error: parse error, expecting `','' or `';'' in /home/nintendo/nintendoodle-www/staffonly/newseditor.php on line 21

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to post the code then.
    ck :: bringing chris to the masses.

  7. #7
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing this code:
    PHP Code:
    $filename "http://www.nintendoodle.com/media/news/" $pid $extension
    to:
    PHP Code:
    $filename "http://www.nintendoodle.com/media/news/".$pid."".$extension.""
    I'm thinking that the problem probably was you were telling the script that $extension = http://www.nintendoodle.com/media/news instead of having the filename and extension.

    I hope that helps!

    -Corbb
    Last edited by JustForWebmasters; Jul 29, 2001 at 21:11.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  8. #8
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The syntax for the code he had was fine, and yours has an error anyway.
    ck :: bringing chris to the masses.

  9. #9
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchos,

    How does mine have an error?

    I believe you, but I'm just curious to know where my error is.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  10. #10
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're missing a period before the last "".
    ck :: bringing chris to the masses.

  11. #11
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh. Didn't even notice that. All fixed now.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here is the full code i am using

    PHP Code:
    <?php 
    if (isset($submit)) {
     include( 
    "security.inc" ); 
    $sql "INSERT INTO news SET  newstitle='$newstitle',newsdescription='$newsdescription',newstext='$newstext',AID='$id',newsdate=CURDATE()";

    $pid mysql_insert_id();

    if (
    "image/pjpeg" == $uploadedfile_type) {
     
    $extension ".jpg";
     } else {
     
    $extension ".gif";
    }
    $filename $DOCUMENT_ROOT "/media/news/" $pid $extension;
    if (
    copy($uploadedfile$filename)) {
     echo(
    "<p>Image added successfully</p>");
     } else {
     echo(
    "<p>Error adding image</p>")
    }

    if (
    mysql_query($sql)) {
    echo( 
    "<p>News article added successfully</p>" );
     } else {
    echo( 
    "<p>Error adding News article. Please forward the following error message to Mark.<br><br>" mysql_error() . "</p>");
     }
     } else { 

    //HTML FORM goes here
    So why is it causing an error (as I listed in my last post) on line 21?

    Thanks for your help in advance.

  13. #13
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo("<p>Error adding image</p>") needs a semicolon...
    ck :: bringing chris to the masses.

  14. #14
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anarchos.

    My code is now giving this error message when the form is submitted though

    Warning: Unable to create '/home/nintendo/nintendoodle-www/media/news/0.jpg': Permission denied in /home/nintendo/nintendoodle-www/staffonly/newseditor.php on line 17

    Error adding image

    My site, like most, has password and username to protect the directories. Would this be the problem, and if so, how do I get PHP to use these codes?

    Also, it appears to be attempting to save the pic as 0.jpg, but I wanted it to have the same ID as the article, which is 13. Any reasons? Thnks

  15. #15
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The .htaccess files won't affect that, it is due to the permissions on the directories.

    I looked at it again and the reason that you're getting 0.jpg instead of 13.jpg is that you need to run the sql query first. So put $result = mysql_query($sql); before the mysql_insert_id(); line. if (mysql_query($sql)) should be changed to if ($result) also.
    Last edited by Anarchos; Jul 30, 2001 at 18:23.
    ck :: bringing chris to the masses.

  16. #16
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what am I doing wrong?

  17. #17
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the permissions on your directory that you store the images in to 777, you do it in most FTP clients or through telnet.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  18. #18
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your help, it was really needed, cause I am an idiot so far at PHP


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
  •