PHP File Upload Script


PHP File Uploading Code

Take a moment to commit the following list to memory—
it contains the variables that are automatically placed in the
 $_FILES superglobal after a successful file upload.
 The base of img1 comes from the name of the input
 field in the original form.

$_FILES[$img1] [tmp_name]. The value refers to the temporary file on the Web server.

$_FILES[img1] [name]. The value is the actual name of the file that was uploaded. For example, if the name of the file was me.jpg, the value of $_FILES[img1] [name] is me.jpg.

$_FILES [img1] [size]. The size of the uploaded file in bytes

$_FILES [img1] [type]. The mime type of the uploaded file, such as image/jpg

uploaded file by PHP code




The goal of this script is to take the uploaded file and copy it to the document root of the Web server and return a confirmation to the user containing values for all the variables in the preceding list.

Open a new file in your text editor and start a PHP block:

<?

Create an if…else statement that checks for a value in $_FILES[img1].

if ($_FILES[img1] != "") {

If $_FILES [img1] is not empty, execute the copy function. Use @ before the function name to suppress warnings, and use the die() function to cause the script to end and a message to display if the copy() function fails.

@copy($_FILES[img1][tmp_name], "/usr/local/bin/apache_1.3.26/
htdocs/".$_FILES[img1][name]) or die("Couldn't copy the file.");




 Note  If the document root of your Web server is not /usr/local/bin/apache_1.3.26/htdocs/ as shown in step 3, change the path to match your own system. For example, a Windows user might use /Apache/htdocs/.


Continue the else statement to handle the lack of a file for upload:

} else {
   die("No input file specified");

Close the if…else statement, then close your PHP block:

}
?>

Add this HTML:

<HTML>
<HEAD>
<TITLE>Successful File Upload</TITLE>
</HEAD>
<BODY>
<H1>Success!</H1>


Mingle HTML and PHP, printing a line that displays values for the various elements of the uploaded file (name, size, type):

<P>You sent: <? echo $_FILES[img1][name]; ?>, a <? echo
$_FILES[img1][size]; ?> byte file with a mime type of <? echo
$_FILES[img1][type]; ?>.</P>

Add some more HTML so that the document is valid:

</BODY>
</HTML>

Save the file with the name do_upload.php.

Related Posts:
  • php-best top 20 Open Source Content Management Systems best top 20 php Open Source Content Management Systems php-best top 20 Open Source Content Management Systems All this open source content management system written in PHP/mySQL and well configured themes and modules for … Read More
  • PHP Sessions - setcookie mplement a session timeout of your own.  Both options mentioned by others session.gc_maxlifetime  and session.cookie_lifetime are not reliable. session.gc_maxlifetime session.gc_maxlifetime specifies the number o… Read More
  • PHP method of securely Tips PHP method of securely website PHP Web security tips Passwords used within your PHP application  should always be encrypted. If the server you are using does not support mcrypt(), use crypt() to encrypt the password… Read More
  • Top PHP Tutorial website PHP - Wikipedia, the free encyclopedia PHP is a server-side scripting language designed for  web development but also used as a general-purpose programming language. As of January 2013, PHP was installed on ... http… Read More
  • Working with Cookies in PHP Cookies are key/value pairs, that are sent to the browser along with some other information, such as which paths the cookie is valid for and when it expires. Since PHP is designed to solve “the Web problem,” it has some g… Read More
  • php Sessions code Php Sessions Sessions are used to help maintain the values of variables across multiple web pages. This is done by creating a unique  session ID that is sent to the client browser. The browser  then sends the … Read More
  • Top PHP Interview Questions with Answers For Job   What is PHP?     PHP is a server side scripting language  used for web development applications.     Php is the powerful tool for making dynamic website.     Many … Read More
  • 555 php Interview Questions What are “GET” and “POST”? What are the advantages of stored procedures, triggers, indexes? PHP? What is LAMP? How to Read an RSS Feed With PHP  What is the maximum length of a table name, database name, … Read More
  • XML in PHP-SimpleXML-DOM-XMLReader There are many ways we can work with XML in PHP, and they’re all useful in different situations. There are three main approaches to choose from and they all have their advantages and disadvantages: 1. SimpleXMLis the most… Read More
  • How to Sending Data to a Database php How to Sending Data to a Database php Save Data to a Database by php The process of adding information to a table is similar  to creating the table itself in terms of which functions  you use, but the SQL quer… Read More
  • All Operators Of PHP   All PHP Operators  An expression is the basic building block of the language. Anything with a value can be thought of as an expression. Examples include: 5 5+5 $a $a==5 sqrt(9) By combining many of these bas… Read More
  • Variables in PHP Variables in PHP Variables are used for storing a values, like text strings, numbers or arrays. When a variable is set it can be used over and over again in your script All variables in PHP start with a $ sign symbol. Th… Read More
  • PHP Array Introduction Function Description PHP  Testing Array and sizeof( )<?php$fixture = Array( );// $fixture is expected to be empty.$fixture[] = "element";// $fixture is expected to contain one element.?>A really simple way … Read More
  • Creating a Php Script to Mail Your Form Php Mail Script According to the form action in simple_form.html, you  need a script called send_simpleform.php. The goal  of this script is to accept the text in  $_POST[sender_name], $_POST[sender_email],… Read More
  • PHP File Upload Script PHP File Uploading Code Take a moment to commit the following list to memory— it contains the variables that are automatically placed in the  $_FILES superglobal after a successful file upload.  The base of im… Read More