PHP Question - From a Newbie!

Discussion in 'Mac Programming' started by gavd, Jan 30, 2006.

  1. macrumors 6502a

    Joined:
    Jan 30, 2006
    #1
    OK, this is a bit of a long post, but it's because I've copied my code in!
    I'm learning PHP from a Wrox published book, but I can't get the following code to work.
    It works up to the if (move_uploaded_file()) point, but doesn't seem to do anything from there. It's not erroring because the page loads all the HTML code, but the PHP variables hold no values.

    Can anyone shed any light?

    Thanks

    <?php
    //connect to the database
    $link = mysql_connect("localhost", "*****", "*****")
    or die("Could not connect: " . mysql_error());
    mysql_select_db("moviesite", $link)
    or die (mysql_error());

    //make variables available
    $image_caption = $_POST['image_caption'];
    $image_username = $_POST['image_username'];
    $image_tempname = $_FILES['image_filename']['name'];
    $today = date("Y-m-d");

    //upload image and check for image type
    //make sure to change your path to match your images directory
    //$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
    $ImageDir ="/Users/gavdent/Sites/images/";
    $ImageName = $ImageDir . $image_tempname;

    if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
    $ImageName)) {
    //get info about the image being uploaded
    list($width, $height, $type, $attr) = getimagesize($ImageName);

    switch ($type) {
    case 1:
    $ext = ".gif";
    break;
    case 2:
    $ext = ".jpg";
    break;
    case 3:
    $ext = ".png";
    break;
    default:
    echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
    "PNG file.<br>";
    echo "Please hit your browser's 'back' button and try again.";
    }
    //insert info into image table
    $insert = "INSERT INTO images
    (image_caption, image_username, image_date)
    VALUES
    ('$image_caption', '$image_username', '$today')";
    $insertresults = mysql_query($insert)
    or die(mysql_error());
    $lastpicid = mysql_insert_id();
    $newfilename = $ImageDir . $lastpicid . $ext;
    rename($ImageName, $newfilename);
    }
    ?>
    <html>
    <head>
    <title>Here is your pic!</title>
    </head>
    <body>
    <h1>So how does it feel to be famous?</h1><br><br>
    <p>Here is the picture you just uploaded to our servers:</p>
    <img src="images/<?php echo $lastpicid . $ext; ?>" align="left">
    <strong><?php echo $image_name; ?></strong><br>
    This image is a <?php echo $ext; ?> image.<br>
    It is <?php echo $width; ?> pixels wide
    and <?php echo $height; ?> pixels high.<br>
    It was uploaded on <?php echo $today; ?>.
    </body>
    </html>
     
  2. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #2
    FYI: next time use the php tags

    PHP:
    <?php
    //connect to the database
    $link mysql_connect("localhost""*****""*****")
    or die(
    "Could not connect: " mysql_error());
    mysql_select_db("moviesite"$link
    or die (
    mysql_error());

    //make variables available
    $image_caption $_POST['image_caption'];
    $image_username $_POST['image_username'];
    $image_tempname $_FILES['image_filename']['name'];
    $today date("Y-m-d");

    //upload image and check for image type
    //make sure to change your path to match your images directory
    //$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
    $ImageDir ="/Users/gavdent/Sites/images/";
    $ImageName $ImageDir $image_tempname;

    if (
    move_uploaded_file($_FILES['image_filename']['tmp_name'], 
    $ImageName)) {
    //get info about the image being uploaded
    list($width$height$type$attr) = getimagesize($ImageName);

    switch (
    $type) {
    case 
    1:
    $ext ".gif";
    break;
    case 
    2:
    $ext ".jpg";
    break;
    case 
    3:
    $ext ".png";
    break;
    default:
    echo 
    "Sorry, but the file you uploaded was not a GIF, JPG, or " .
    "PNG file.<br>";
    echo 
    "Please hit your browser's 'back' button and try again.";
    }
    //insert info into image table
    $insert "INSERT INTO images
    (image_caption, image_username, image_date)
    VALUES
    ('
    $image_caption', '$image_username', '$today')";
    $insertresults mysql_query($insert)
    or die(
    mysql_error());
    $lastpicid mysql_insert_id();
    $newfilename $ImageDir $lastpicid $ext;
    rename($ImageName$newfilename);
    }
    ?>
    <html>
    <head>
    <title>Here is your pic!</title>
    </head>
    <body>
    <h1>So how does it feel to be famous?</h1><br><br>
    <p>Here is the picture you just uploaded to our servers:</p>
    <img src="images/<?php echo $lastpicid $ext?>" align="left">
    <strong><?php echo $image_name?></strong><br>
    This image is a <?php echo $ext?> image.<br>
    It is <?php echo $width?> pixels wide 
    and <?php echo $height?> pixels high.<br>
    It was uploaded on <?php echo $today?>.
    </body>
    </html>
     
  3. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #3
    Can we see the code from the page which I assume as a html form of some kind?
     
  4. thread starter macrumors 6502a

    Joined:
    Jan 30, 2006
    #4
    HTML Code

    Sure, here it is. Thanks for the PHP tags tip as well. Didn't realise you could do that.
    Code:
    <html>
    <head>
    <title>Upload your pic to our site!</title>
    </head>
    <body>
    <form name="form1" method="post" action="check_image.php" 
        enctype="multipart/form-data">
    <table border="0" cellpadding="5">
      <tr>
        <td>Image Title or Caption<br>
          <em>Example: You talkin' to me?</em></td>
        <td><input name="image_caption" type="text" id="item_caption" size="55" 
              maxlength="255"></td>
      </tr>
      <tr>
        <td>Your Username</td>
        <td><input name="image_username" type="text" id="image_username" size="15" 
              maxlength="255"></td>
      </tr>
        <td>Upload Image:</td>
        <td><input name="image_filename" type="file" id="image_filename"></td>
      </tr>
    </table>
    <br>
    <em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
    <p align="center"><input type="submit" name="Submit" value="Submit">
       
      <input type="reset" name="Submit2" value="Clear Form">
    </p>
    </form>
    </body>
    </html>
    
     
  5. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #5
    oh... heh it also the stupid things...

    you need to change the permission on the images directory to 767 from terminal

    Code:
    cd Sites
    chmod 767 images
    
    or just cmd click or right the directory then get info and under the ownership & permissions info set it to.

    You can read & write
    read & write
    group:
    no access

    others read & write
     
  6. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #6
    i haven't looked at your code, but it's possible the problem is on the server end.

    i'd written some php to upload files to one of my sites and it failed on the move. i went round and round with the hosting company (hostgator.com, and they were actually pretty awesome about keeping at it) and, suddenly, it all worked.

    they said they made a change on their end, but repeated attempts to ask what it was didn't net me an answer (the not so awesome part).

    for the next few days, sometimes it would work and sometimes it wouldn't. now it mostly works, but sometimes it still fails. and i have no idea why.

    work up the smallest test case you can that still fails and see if your hosting company can reproduce the problem.
     
  7. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #7
    if this helps, here's my small test case. you'll have to edit the php file to match your particular file destination.

    Code:
    <html>
    <head>
    <title>upload test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    
    <form enctype="multipart/form-data" action="upload_test.php" method="post">
            <hidden name="MAX_FILE_SIZE" value=15000 />
            <input name="user_file" type="file" />
            <br>
    
            <input type="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    


    PHP:
    <html>
    <head>
    <title>upload test php</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <? 
    //----------------------------------------------------------------
    //
    // %Z%%M% %I% %G%
    //
    // upload_test
    //
    // sxz 041219
    //
    //----------------------------------------------------------------

    /*
    session_start();
    header("Cache-control: private");
    */
    ?>

    <body>
    <br />

    <?php
            
    //-----------------------------------------------------------
            // rejectFile
            //-----------------------------------------------------------

            
    function rejectFile($reason)
            {
    ?>
                    <h3>
                            Failure
                    </h3>
                    <p>
    <?php
                            
    echo $reason;
                            exit();
    ?>
                    </p>
    <?php
            
    }

            
    //-----------------------------------------------------------
            // baseMusicPath
            //-----------------------------------------------------------

            
    function baseMusicPath()
            {
                    return 
    "members/upload_test";
            }

            
    //-----------------------------------------------------------
            // wholeMusicPath
            //-----------------------------------------------------------

            
    function wholeMusicPath()
            {
                    return 
    $_SERVER['DOCUMENT_ROOT'] . "/" baseMusicPath();
            }

            
    //-----------------------------------------------------------
            // processUploadedFile
            //-----------------------------------------------------------

            
    function processUploadedFile($orig_filename$tmp_filename)
            {
                    
    // ensure uploaded file is good

                    
    if (!is_uploaded_file($tmp_filename))
                    {
                            
    rejectFile("Danger: uploaded file " $tmp_filename
                                    
    " not uploaded correctly");
                            exit;
                    }

                    
    // grab some data so we can determine where to put
                    // this thing

                    
    $path wholeMusicPath();
                    
    $dst $path "/" $orig_filename;

                    
    $src $tmp_filename;   // for php library function xfers

                    
    if (!move_uploaded_file($src$dst))
                    {
                            
    rejectFile("Could not move " $src " to destination " $dst);
                            exit;
                    }

                    return 
    $dst;
            }

            
    //-----------------------------------------------------------
            // main
            //-----------------------------------------------------------

            // grab needed info for our uploaded file

            
    $orig_filename $_FILES['user_file']['name'];
            
    $tmp_filename $_FILES['user_file']['tmp_name'];

    /*
    echo "orig_filename : " . $orig_filename; newline();
    echo "tmp_filename : " . $tmp_filename; newline();
    */

            
    echo "attempting to move " $orig_filename " from " $tmp_filename;
            echo 
    "<br><br>";

            
    $url processUploadedFile($orig_filename$tmp_filename);

            
    $msg "File " $orig_filename " uploaded to " $url;
    ?>

    <h3>
            Success
    </h3>
    <p>
            <?php echo $msg?>
    </p>

    </body>
    </html>
     
  8. thread starter macrumors 6502a

    Joined:
    Jan 30, 2006
    #8
    Progress!

    OK, thanks to superbovine I can now upload the image to the images folder within my sites folder.

    However, the html which should then display the image doesn't!

    I have also tried to load the image using preview from the directory, but I get the following message...

    Anyone got any ideas?

    Thanks for your help so far!
     
  9. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #9
    download the image and see if you can open it on your mac. or even run diff on it. if it's changed, then you know something happened when it was uploaded or moved.

    if it's not changed, then you can start looking at the browser display technology.
     
  10. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #10
    if your image over 8 meg that is the default limit for move_uploaded_file.
     
  11. thread starter macrumors 6502a

    Joined:
    Jan 30, 2006
    #11
    The images I've been using to test the code are less than 100K so it's not the size that's the issue.
    I'm not sure if it could be the renaming of the image which might cause the problem. Not sure what else would cause it to corrupt and not let me be able to open it. :confused:
     

Share This Page