Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2003, 10:45 PM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default URGENT!!! Sending file to server in browser

Dear all,

May I know how do I send image files from my hard disk to the web server. Can I do accomplish this when I click the browse button and there will a window pop up allowing me to choose which file I wanna upload. Last but not least, how can I control which images will be store in a desire directory.

Please kindly advise me on this. Thanks alot.

Warmest regards,
Keng Yiam
 
Old September 18th, 2003, 11:50 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Keng,
    The process is actually not as daunting as you may think.

Code:
<?php

    # You'll notice a few differences between this form and HTML forms which do not submit data
    # The first being the inclusion of the 'enctype' attribute, which will tell 
    # the browser that it is posting both normal form data and mixed data
    # Another difference will be the inclusion of the MAX_FILE_SIZE hidden field which tells 
    # the browser how large the file may be.  This attribute can be easily side-stepped, 
    # so it should be used in conjunction with server-side file size validation.

    # In PHP once a file is posted it's attributes becomes available in the $_FILES
    # superglobal within a multi-dimensional array which contains five sub indices
    #       
    # $_FILES["fieldname"]["name"] - name of the file on the user's machine
    # $_FILES["fieldname"]["type"] - the type of file, i.e. image/jpeg, image/pjpeg
    # $_FILES["fieldname"]["size"] - the size of the file in bytes
    # $_FILES["fieldname"]["tmp_name"] - the temporary name assigned to the file by PHP
    # $_FILES["fieldname"]["error"] - the error code associated with the upload, if any
    #
    #
    # http://us4.php.net/manual/en/feature...ad.post-method

if (!isset($_FILES["userfile"]["tmp_name"])) 
{

    echo "<form method='post' enctype='multipart/form-data' action='{$_SERVER["PHP_SELF"]}'>
        <input type='file' name='userfile' size='20' />
        <input type='hidden' name='MAX_FILE_SIZE' value='1000000' />
        <input type='submit' name='do_action' value='Post Image' />
        </form>";

}

else if ($_POST["do_action"] == "Post Image")
{

    # If you'd like to validate the information, do so here...
    # You are strongly encouraged to implement some sort of validation scheme
    # Test the size, check file type, etc.
    # For the sake of this demonstration, I'm going to skip that part
    #
    # The copy function will move the file, it expects two arguments
    # boolean copy(string source, string destination) 
    # For this demonstration the source argument will be the temporary name assigned to the file    
    #
    # The destination expects an absolute path which is why I have used $_SERVER["DOCUMENT_ROOT"]
    # beyond that include the remaining path to the image and assign a new file name, or to keep 
    # the same file name append $_FILES["userfile"]["name"] where newfilename.jpg 
    # appears
    #
    # http://www.php.net/copy

    # If the copy function fails you want to ensure that you have set the proper write permissions 
    # on the destination directory.  
    # This can be done in unix with the chmod command, which likely will have to be done 
    # from the telnet command prompt.  Many ISPs block the PHP function chmod()
    # You can attempt it though.
    #
    # A chmod setting of 664 would likely get your PHP script going.
    #
    # boolean chmod(string file path, integer mode)
    #
    # At the command prompt you can get more information about how chmod works by typing in 
    # man 1 chmod
    # man 2 chmod
    # And there is a little more about it in the PHP manual
    # http://www.php.net/chmod
    #
    # In windows all you need to do is go to the folder in question and set read/write attributes 
    # via the properties option from the right click menu.

    if (!copy($_FILES["userfile"]["tmp_name"], $_SERVER["DOCUMENT_ROOT"]."path/to/image/newfilename.jpg"))
    {

        echo "Error: File copy failed.";

    } else {

        echo "Success! File copied!";

    }

}
?>
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old September 19th, 2003, 12:23 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also,

I missed this part:
Quote:
quote:
how can I control which images will be store in a desire directory.
To check what images are going into the directory, you will need to implement a validation scheme. Its not entirely foolproof, but works good enough IMO!

Code:
# assign type to a smaller variable!
$type = $_FILES["userfile"]["type"];

if ($type != "image/jpg" && $type != "image/pjpeg" && $type != "image/jpeg")
{

    echo "Error: not a valid JPEG image!";

}
?>
Additional information about the file type attribute can be found under the HTTP_ACCEPT line in phpinfo() output. This *usually* but not always has several file types listed there where you may better see how the type is structured for various applications (Sometimes, however, it only has the */* wildcard). And if a validation fails but you know the type to be correct, it also helps to simply output the value of the 'type' variable to the browser.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old September 19th, 2003, 11:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great job on the reply, Rich, but you forgot something that many people find convenient:

 http://www.php.net/is_uploaded_file
 http://www.php.net/move_uploaded_file

Using these functions means that you don't have to deal with regular file manipulation routines, and guarantees that the file you're moving is, in fact, an uploaded file.


Take care,

Nik
http://www.bigaction.org/
 
Old September 19th, 2003, 02:19 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ah, that is good to know.. I didn't even know there were functions written explicitly for uploaded files.



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent: word file on aspx page from sql server bijgupt ASP.NET 1.0 and 1.1 Professional 5 November 15th, 2006 07:27 AM
Error in Sending XMl file to Server r_arvindk2000 XML 0 April 8th, 2005 01:52 PM
Error in sending XML file to server r_arvindk2000 XML 0 April 8th, 2005 01:49 PM
Help sending XML file to server, reading response bradartigue XML 1 September 5th, 2003 07:42 AM
Sending "file:" to the browser DeepBlue03 Classic ASP Basics 3 June 26th, 2003 02:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.