What's wrong with this code?
Results 1 to 6 of 6

Thread: What's wrong with this code?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3

    What's wrong with this code?

    Hi
    I'm trying to learn PHP with this book: PHP for absolute beginners by Jason Lengstorf i got stuck with this code:

    Code:
    <?php
    // Checks if the form was submitted
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Checks if a file was uploaded without errors
    if(isset($_FILES['photo'])
    && is_uploaded_file($_FILES['photo']['tmp_name'])
    && $_FILES['photo']['error']==UPLOAD_ERR_OK) {
    // Checks if the file is a JPG image
    if($_FILES['photo']['type']=='image/jpeg') {
    $tmp_img = $_FILES['photo']['tmp_name'];
    // Creates an image resource
    $image = imagecreatefromjpeg($tmp_img);
    // Tells the browser what type of file
    header('Content-Type: image/jpeg');
    // Outputs the file to the browser
    imagejpeg($image, '', 90);
    // Frees the memory used for the file
    imagedestroy($image);
    } else {
    echo "Uploaded file was not a JPG image.";
    }
    } else {
    echo "No photo uploaded!";
    }
    } else {
    // If the form was not submitted, displays the form HTML
    ?>
    
    <form action="test.php" method="post"
    enctype="multipart/form-data">
    <label for="photo">User Photo:</label>
    <input type="file" name="photo" />
    <input type="submit" value="Upload a Photo" />
    </form>
    <?php } // End else statement ?>
    when i try it in the browser i only get an error message:
    "the image: "http://localhost/marco/phpscripts/test.php" cannot be displayed because it contains errors"

    Thanks in advance

  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    The most common issue for this error is the JPG is saved with CMYK encoding not RGB encoding. This is more often associated with older versions of Firefox. Check to see if the images are saved as RGB and then see if the work in IE. If it works consider upgrading Firefox.
    The code worked perfectly for me.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thank you for your answer, i still can't get it to work though. I am running the latest Firefox, chrome and safari, i'm using the latest version of internet explorer is also .
    Could it be the settings for the localhost (i am running xampp 1.81)?
    Because I uploaded to a remote server and it seems to work. But i cannot see where the image gets stored.
    Thanks

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,949
    If you are going to output the image, then you cannot output anything except the image, so maybe:
    Code:
    // Creates an image resource
    $image = imagecreatefromjpeg($tmp_img);
    // Tells the browser what type of file
    header('Content-Type: image/jpeg');
    // Outputs the file to the browser
    imagejpeg($image, '', 90);
    // Frees the memory used for the file
    imagedestroy($image);
    exit;
    }
    You also need to make sure nothing else is output before the image output, such as a BOM at the start of your script if it's saved as UTF-8, or anything else that is not part of the image data.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    I still can't get it to work on the localhost...maybe i'll just skip this chapter and come back later. thanks anyway

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    One thing you can do is temporarily change the header the "image" is served under:
    PHP Code:
    header('Content-Type: text/plain'); 
    so that you can look at the data instead of just seeing a browser-generated error message.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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