At 1:52 AM -0700 5/21/06, P. Guethlein wrote:
I'm at one of those frustration levels.... can't seem to get a script working that will post and upload a file to the server. I"m working with the below. Can you help?

Guethlein:

Yes, try this -- watch for line breaks. Also, create folders "uploads/tmp".

The code works "as is" for me except that I have to give the "tmp" folder 0777 permissions* because the code runs as "nobody". I haven't figured out how to get around that, but I can change the uploaded file's permissions without error. (If anyone wants to show me OFF-LIST how to do this without setting the tmp folder to 0777, I'm all ears, but don't beat me up because I'm trying to learn.)

Code follows:

<?php

// This allows users to upload files to the server.

if (isset($_POST['submit'])) // handle the form -- start of main Submit conditional..
        {

        // Create the file name.

        $filename = $_FILES['upload']['name'];
        $file_loaded = 0;

        // Move the file over.

if (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/tmp/$filename"))
                {
                echo '<p>The file has been uploaded to the server.</p>';
                chmod("uploads/tmp/$filename", 0755);
                echo ( '<p>' . $filename . '</p>' );
                }
        else
                {
echo ('<p><font color="red">ERROR: The file was not upload.</font></p>');
                }
        }
else
        {

        ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <p>
                <input type="hidden" name="MAX_FILE_SIZE" value="30000">
                </p>
                <fieldset>
                <legend>Select the file to upload:</legend>
                <p>
                <b>File:</b>
                <input type="file" name="upload" />
                </p>
                </fieldset>
                <p>
                <input type="submit" name="submit" value="Submit" />
                </p>
        </form>

        <?php
        }
?>

hth's

tedd

* Larry Ullman in his books says to use 0777 permission for uploading files. However, he does say that it is less secure and should be placed outside of the web directory.
--
------------------------------------------------------------------------------------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to