Uploading an image using a form?

Discussion in 'PHP' started by mokimofiki, Apr 9, 2009.

  1. #1
    I am having trouble uploading an image using a form. Please any help would be greatly appriciated.

    Code:
    
    <?php session_start(); ?>
    
    <html>
    
    <head>
    <title>Submit a new Home</title>
    </head>
    
    <?php
    if(!$_POST['tempsvar'])
        {
            echo "<form enctype=multipart/form-data method=\"post\" action=\"submithome.php\">";
            echo "<table width=\"800\" border=\"1\" cellspacing=\"2\" cellpadding=\"2\">";
            echo "<tr>";
            echo "<td colspan=\"4\"><font size=\"4\"><b>Fill out the form completely</b></font><br>If any field is skipped the new home will not be added to the database and you will have to start the form over.  Please double check your entries before submission as well to avoid inaccurate information being displayed within the database.<br /><br /></td>";
            echo "</tr><tr>";
            echo "<td>Model #</td><td><input type=\"text\" name=\"modelname\"></td><td></td><td></td>";
            echo "</tr><tr>";
            echo "<td>Collection</td><td><select name=\"collection\"><option value=\"nocollection\">Select Collection</option></select></td><td>Model Name</td><td><input type=\"text\" name=\"modelname\"></td>";
            echo "</tr><tr>";
            echo "<td>Bedrooms</td><td><select name=\"bedrooms\"><option value=\"nobedrooms\">Select No Bedrooms</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option></select></td><td>Bathrooms</td><td><select name=\"bathrooms\"><option value=\"nobathrooms\">Select No Bathrooms</option><option value=\"1\">1</option><option value=\"1.5\">1.5</option><option value=\"2\">2</option><option value=\"2.5\">2.5</option><option value=\"3\">3</option><option value=\"3.5\">3.5</option><option value=\"4\">4</option></select></td>";
            echo "</tr><tr>";
            echo "<td>Square Footage</td><td><input type=\"text\" name=\"sqft\"></td><td></td><td></td>";
            echo "</tr><tr>";
            echo "<td>Upload Image</td><td><input type=hidden name=MAX_FILE_SIZE value=150000><input type=hidden name=completed value=1><input type=file name=filename></td><td></td><td></td>";
            echo "</tr><tr>";
            echo "<td><input type=\"hidden\" name=\"tempsvar\" value=\"1\"></td><td></td><td></td><td><input type=\"submit\" value=\"Add Home To Database\"></td>";
            echo "</tr>";
            echo "</table>";
            echo "</form>";
        }
    else
        {
            echo "Display the listed home";
            
            //Start Verify and upload image
                if ($_REQUEST[completed] == 1) { 
                $source = "homesimg";
                move_uploaded_file($_FILES['filename']['tmp_name'], "../$source/".$_FILES['filename']['name']);
            // Following code to fix line ends
                if (! eregi ('(gif|jpg|pdf)$',$_FILES['filename']['name'])) {
                $fi = file("../$source/".$_FILES['filename']['name']);
                $fi2 = fopen("../$source/".$_FILES['filename']['name'],"w");
                foreach ($fi as $lne) {
                    $n = rtrim ($lne);
                    fputs ($fi2,"$n\n");
                    }
                    }
            //End Verify and upload image 
                }
        
        }
    
    ?>
    
    Code (markup):
    You can see the form in action at http://www.mofiki.com/gallery/submithome.php

    And if you try and upload an image you can see the error :)
     
    mokimofiki, Apr 9, 2009 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Check that the directory where you're uploading the image exists and that you've set the correct permissions to the folder to allow you to upload the file there.

    Here is a file upload handler that I've only just written today that does the trick;
    
    				// set the target folder for the image
    				$target = $_SERVER['DOCUMENT_ROOT'] . "/orderfiles/"; 
    
    				// add a timestamp to the image name
    				$timestamp = date('U');
    				$image_arr = explode(".",$_FILES['attachments']['name']);
    				$image = $image_arr[0] . "-" . $timestamp . "." . $image_arr[1];
    				$target = $target . $image; 
    				$ok=1; 
    				if(!(move_uploaded_file($_FILES['attachments']['tmp_name'], $target)))
    				{
    					header('Location: add-order.php?e=1');
    				}
    
    PHP:
    I always stick a timestamp on uploaded files to make sure I don't overwrite any older uploaded files.
     
    Weirfire, Apr 9, 2009 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    The permissions are set to allow the file to be uploaded correctly .... I believe that the error is in the code itself but this is my first attempt at file uploading using php.

    Here is the error that I get when I submit the form (of course only the image upload should happen the rest of the form I will complete later)

    Warning: move_uploaded_file(../homesimg/bg2.gif) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/mofiki/public_html/gallery/submithome.php on line 39

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phppXzuoR' to '../homesimg/bg2.gif' in /home/mofiki/public_html/gallery/submithome.php on line 39


    What in the world is '/tmp/phppXzuoR' ? and where did that come from?
     
    mokimofiki, Apr 9, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    "/tmp/phppXzuoR" is the name of the temporary file where PHP stored the upload while waiting for you to use move_uploaded_file to put it in its permanent resting place.

    Your error is because ../homesimg does not exist, at least not relative to your script's current working directory. Your CWD is probably not what you think it is.
     
    SmallPotatoes, Apr 9, 2009 IP
  5. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Ok thank you I will play with that a bit then...
     
    mokimofiki, Apr 9, 2009 IP