Archive for November, 2006

A Simple Form for Uploading a File A

Tuesday, November 21st, 2006


A simple form for uploading a file

The enctype attribute of the form element is set to multipart/form-data this is required for file uploads to work. The enctype attribute specifies the encoding that should be used by the browser to encode the form parameters. The default value of enctype attribute is application/x-www-form-urlencoded:

The input element should be of type file: Enter file name:

This page will appear in the browser as: On receiving an HTTP request with input elements of type file, PHP stores the contents of the uploaded file in a temporary file. The temporary file is created in the server’s default temporary directory, unless another directory is specified by the upload_tmp_dir directive in the php.ini file. The server’s default temporary directory can be changed by setting the environment variable TMPDIR. The temporary file is deleted at the end of the request, so the PHP script handling the request should copy the file if the file data is to be preserved for later use. The details of the uploaded file are accessible to PHP scripts through a two-dimensional global array HTTP_POST_FILES. Supposing that the name of the input element (of type file) is userfile, then: . The variable $HTTP_POST_FILES[ userfile ][ name ] contains the original name of the file on the client machine . The variable $HTTP_POST_FILES[ userfile ][ type ] contains the mime type of the file . The variable $HTTP_POST_FILES[ userfile ][ size ] contains the size, in bytes, of the uploaded file . $HTTP_POST_FILES[ userfile ][ tmp_name ] contains the temporary filename of the file in which the uploaded file was stored on the server Below is a PHP script, upload.php, which copies the uploaded file to the temp/ directory: Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

Uploading Files from Clients There are two mechanisms

Tuesday, November 21st, 2006

Uploading Files from Clients There are two mechanisms that can be used by browsers for uploading files to the server: . Based on the HTTP PUT method . Based on the HTTP POST method The HTTP protocol provides three basic methods GET, PUT, and POST for accessing information from the web server. The GET method is used to fetch web pages. When GET is used to fetch dynamic pages, the form variables are passed as part of the URL. It is safe for the browser to fetch a page using the GET method as many times it likes the GET method should not cause any permanent change on the server. Some web servers have a limit on the length of the URL (mostly 1024) that they can handle, so GET should not be used to pass form data greater than this limit. The PUT method is used for updating information on the server. It requests that the enclosed data be stored as the requested URL on the server. The PUT method is used mostly for publishing pages. The POST method is used if requesting the URL will cause a permanent change on the server. For example, deleting a folder in a web based e-mail application. The browser should not execute the POST method again without getting user confirmation. The POST method sends all the form variables as part of the request body. The POST method is also used when lots of form data needs to be passed to the URL. Uploading Files with PUT A typical HTTP PUT request looks like this: PUT /path/filename.html HTTP/1.1 In this case the client would like the web server to store the contents of the request as the specified URL (/path/filename.html) in the web server’s URL namespace. The web server, by default, would not handle such a request. Rather it specifies a script to handle such requests. Web site specific policies for uploading files can be implemented in the specified script. For example, in Apache this can be done with the script directive (stored in httpd.conf). The following script directive means that you want the cgi script put.cgi to handle HTTP PUT requests: Script PUT /cgi-bin/put.cgi PHP provides support for writing PUT handlers. When PHP gets a PUT request, it stores the contents of the request in a temporary file, which is deleted after the request is processed. The temporary file name is stored in the $PHP_PUT_FILENAME variable, and the URL name is stored in $REQUEST_URI. This simple PHP script, for handling PUT requests, copies the uploaded file to the specified URL location in the web server’s URL name space: The PUT mechanism for uploading files is non-functional in the current implementations of PHP. You could monitor the status of this bug at http://www.php.net/bugs.php?id=10383. Uploading Files with POST HTML form elements allow users to enter the name of the file that will be submitted to the web server. This feature is implemented by recent versions of both Netscape and Microsoft browsers. A simple HTML form (uploadfile.html) for submitting the file looks like this: Page 249
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

$dir = opendir($directory); Read an entry from the

Monday, November 20th, 2006

$dir = opendir($directory); Read an entry from the directory handle: while (($file = readdir($dir))) { If the read entry is a file, then delete the file if it has not been accessed in the last ten days: if (is_file($directory . “/” . $file)) { $accessTime = fileaTime($directory . “/” .$file); $time = time(); if (($time - $accessTime) > 10*24*60*60) { if (unlink($directory . “/” .$file)) { printf(”File %s is removed from %s directory
n”, $file, $directory); } } If the read entry is a directory, then call the function again with the read entry as its argument. Here, we use the recursive cleanTemporaryFiles() function. Note that we do not call the function again if the read entry is “.” or “..”: } else if (is_dir($directory . “/” .$file) && ($file != “.”) && ($file != “..”)) { cleanTemporaryFiles($directory . “/” . $file); } } Close the directory: closedir($dir); } cleanTemporaryFiles(”c:/temp”); ?> Adding and Deleting Directories int mkdir(string directoryname, int mode) The mkdir()function can be used to create a new directory. The argument directoryname is the path name for the directory to be created. The argument mode specifies the access permissions for a UNIX directory. On Windows this argument is ignored. The mode entry is specified as an octet string and should always start with a zero character. The function returns true on success, falseon failure. The following code creates a new directory C:temptest: Page 247
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

Directories Apart from manipulating individual files, PHP provides

Monday, November 20th, 2006

Directories Apart from manipulating individual files, PHP provides functions for handling directories. PHP’s directory APIs can be used to create and delete new directories, and to read the contents of a directory: int chdir(string directory) The current directory of the PHP process can be set by the chdir()function. If you are accessing a number of files in a directory, then you may want to change the current directory to point to the directory containing these files: string getcwd() You can get the current directory using the getcwd() function. It returns the current working directory: int opendir(string path) For reading the contents of a directory, you first need to open the directory. The opendir()function opens the directory specified by path. It returns a directory handle on success, which can be used to refer to the open directory in subsequent function calls: string readdir(int dir) Once you have opened the directory, you can read its contents with the readir()function. The readir()function returns the name of the next entry in the directory. If there are no more entries in the directory, or if the directory handle is invalid, then the function returns false. Apart from files and sub-folders, each directory contains two special entries “.” and “..”. The “.” entry refers to the current directory and “..” refers to the parent directory. The current directory can be opened by opening “.”: $dir = opendir(”.”) Once you have read the contents of the directory, you should close it and free all the resources associated with it: void closedir($dir) The following code deletes files which were not accessed in the last ten days from the temp/ directory: Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

int filemtime(string filename) The filemtime()function returns the time

Monday, November 20th, 2006

int filemtime(string filename) The filemtime()function returns the time when the file content was last modified: int filesize(string filename) The filesize() function returns the size of the file in bytes: string filetype(string filename) The filetype() function returns the type of the file. On success, the return value of filetype() may be one of the following string values: Value Description fifo Entry is a FIFO (named pipe) char Entry is a character special device dir Entry is a directory block Entry is a block special device link Entry is a symbolic link file Entry is a regular file unknown File type cannot be determined void clearstatcache(void) Most file system calls, which return the details of the file, are very expensive. To avoid this performance overhead, PHP caches the details of the file. This cache can be cleared by calling the clearstatcache()function. There is a set of functions which indicate the type of the file. These are: boolean is_dir(string filename) boolean is_executable(string filename) boolean is_file(string filename) boolean is_link(string filename) These functions return trueif the named file is a directory, an executable file, a regular file, or a symbolic link, respectively: boolean is_readable(string filename) boolean is_writable(string filename) The functions is_readable()and is_writable()can be used to find if the PHP script has permissions to read from or write to the named file. We use some of these functions in the Online Storage Application, later in the chapter. Page 245

Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Virtualwebstudio java web hosting provider

se co nd link co nti nu es

Monday, November 20th, 2006

se co nd link co nti nu es to poi nt to the dis k blo ck co nta inin g the dat a, an d the OS will not fre e it as lon g as it do es. Determining File Attributes PHP provides a number of functions which can be used to find additional information about the file: int file_exists(string filename) The file_exists() function can be used to check that the file exists. It returns true if the file exists; otherwise false is returned: int fileatime(string filename) The fileatime() function returns the time when the file was last accessed: int filectime(string filename) The filectime()returns the time when the file, either the content or meta data information like permissions, was last modified: Page 244
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

g to an oth er file. So a

Monday, November 20th, 2006

g to an oth er file. So a us er ca n ma ke a ba ck up of a file by jus t cre ati ng an oth er link to it. Th en the dat a is still ava ilab le eve n if the firs t file (lin k) is del ete d, be ca us e the Page 243

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

to a dis k blo ck, tha t

Monday, November 20th, 2006

to a dis k blo ck, tha t blo ck is co nsi der ed to be in us e. Wh en a blo ck is no lon ger ref ere nc ed by an y link , the OS co nsi der s it “fre e” an d will ove rwri te it wit h dat a bel on gin Page 242
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

the return value for end-of-file condition: while (feof($fp))

Monday, November 20th, 2006

the return value for end-of-file condition: while (feof($fp)) { $buffer = fread($fp, 1024); } Copying, Deleting, and Renaming Files PHP supports functions for copying, deleting, and renaming files. Though copying and renaming files can be implemented using the read, write, and delete APIs, it is almost always easier to use the specific PHP functions: int copy(string source, string destination) The copy() function copies the file source to destination. It returns true on success, false on failure: int rename(string oldname, string newname) This function changes the name of the file oldname to newname. It returns true on success, false on failure: int unlink(string filename) The unlink()function should be used to delete a file permanently. The function returns trueon success, falseon failure. No Ev te ery file in UN IX is act uall y a link to a dis k blo ck. As lon g as the re is at lea st on e link Page 241

Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Virtualwebstudio java web hosting provider

printf(”%s”, $fileArray[$i]); } ?> Writing to Files The

Monday, November 20th, 2006

printf(”%s
“, $fileArray[$i]); } ?> Writing to Files The functions fputs() and fwrite() can be used for writing to files. These two functions are identical: int fputs(int fp, string stringtoWrite [,int length]); int fwrite(int fp, string stringtoWrite [,int length]); The first argument fp is the file handle of the file to be written to. The second argument stringToWrite is the string to be written to the file. The third optional argument is the number of characters from the string to write. If this last parameter is not included then the entire string will be written. The return value is true on success, or false on failure. Navigating within Files When reading from a file, the current position indicator within the file moves to the next unread character. PHP provides a set of functions for changing the current position indicator within the file: int rewind(int fp) The rewind() function sets the current position indicator to the beginning of the file. The argument fp is the file pointer of the file. The function returns true on success, or false on failure: int fseek(int fp, int offset [, int whence]) The fseek() function can be used to set the current position indicator to any position in the file. The argument fp is the file handle of the file. The third argument whence can be any one of the following: . SEEK_SET Sets position indicator to offset bytes. . SEEK_CUR Sets position indicator to current location plus offset. . SEEK_END Sets position indicator to end-of-file plus offset. Note that a negative value can be specified as offset. The default value of the whence argument is SEEK_SET. The fseek() function returns true on success, false on failure: int ftell(int fp) This function can be used to find the current position within the file. It returns the position in the file: int feof(int fp) feof() can be used to find whether or not the current position is at the end of the file. The function returns true if the file with pointer fp is at the end of the file or if an error occurs; otherwise it returns false. The following example uses feof() function to read the contents of the file. There is no need to check Page 240

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services