PHP Coding      |      ASP.net Coding        |      3d Studio Max      |      Adobe Photoshop      |      After Effects  
  
 
Uploading a file and changing the name
Uploading a file in PHP is very easy. With the help of just few commands you will be able to create an application for uploading files. In this tutorial we will not just learn how to upload the file but also how to change the name of uploaded file using a simple date/time feature of php.

The HTML form
Before you can use PHP to manage your uploads, you need first make an HTML form, which is building block for any client server interaction occurring in the programming. Have a look at the example below and save this HTML code as upload.php.
<html>
<body>
<form enctype="multipart/form-data" action="" method="post">
Choose a file: <input name="file_name" type="file" />
<input type="submit" value="Upload" name="submit" />
</form>
</body>
</html>
Next, we need to make a php code to handle the file upload. In this example we are going to use a picture file for the upload with a .jpg extension. Now add the code show below to your upload.php page right after html form.

<?php
if (isset($_POST['submit']))
{
$newname = date('YmdHis').".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"],
"pictures/" . $newname);
}
?>
 Since, we are using the php script on the same page as html form we need to make sure that the script does not run without pressing the "submit" button. The if (isset($_POST['submit'])) statement checks if the submit button has been pressed.


Then we create a variable called "newname" which is storing the new name of file using the date() function. You must put the extension of the file after date function or else the file will be store without an extension.

move_upload_file() is the main function for uploading files in php.
$_FILES["file"]["tmp_name"] is the temporary location of the file which php creates.

After submitting the file it will be given a unique name and will be stored in pictures folder as shown above in the code.