Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 18th, 2003, 11:45 PM
Registered User
 
Join Date: Sep 2003
Location: Singapore, , Singapore.
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
Reply With Quote
  #2 (permalink)  
Old September 19th, 2003, 12:50 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old September 19th, 2003, 01:23 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old September 19th, 2003, 12:53 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
  #5 (permalink)  
Old September 19th, 2003, 03:19 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
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
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02:52 PM
Error in sending XML file to server r_arvindk2000 XML 0 April 8th, 2005 02:49 PM
Help sending XML file to server, reading response bradartigue XML 1 September 5th, 2003 08:42 AM
Sending "file:" to the browser DeepBlue03 Classic ASP Basics 3 June 26th, 2003 03:05 AM



All times are GMT -4. The time now is 02:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.