/* */
MEDIA PENDIDIKAN dan PEMBELAJARAN Ilmu Mantiq (Logika): Kaidah Berfikir yang Memelihara Akal, agar tidak terjadi Kerancuan dalam Berfikir.

Sunday, October 2, 2011

Upload files to the server using php

This time we will discuss uploading files using PHP. It would be nice if we as users of the website, can upload pictures, documents or videos to our web site. This article will explain how to upload files with PHP keserver us. PHP makes things easier in handling file uploads. To create a form upload, make sure we include the attribute enctype = "multipart / form-data", and make sure methodnya is post. Okay we just see the following HTML code: 
<Form method = "post" action ="<?=$ PHP_SELF?> "Enctype =" multipart / form-data ">
<input type="file" name="myfile">
<input type="submit" name="Submit" value="Submit">
</ Form>
As we see, we give a name to upload "myfile". The name is very important, because when a file has been uploaded then the file is automatically given a unique name and stored in a temporary directory. Of course the url path to the file can be accessed, because it will automatically show up global variables, which have the same name with which we uploaded, in this case myfile. There will be four new variables which are all preceded by myfile and followed by an underscore (underscore), namely:
• $ myfile. This variable contains the location information file on the server.
• $ myfile_name. The original file name when it was still dikomputer clients.
• $ myfile_size. The size of the file (in bytes).
• $ myfile_type. The file type.
Simple Upload
We will try to upload the following examples, save with the name upload.php:
<html>
<head>
<title> Upload file </ title>
</ Head>
<body>
<?
if (isset ($ upload)) {
    echo "File Location: $ myfile <br>";
    echo "File Name: $ myfile_name <br>";
    echo "Size: $ myfile_size byte <br>";
    
echo "File Type: $ myfile_type <br>";
    copy ($ myfile, "$ myfile_name") or die ("Failed to upload");
}
?>
<Form enctype = "multipart / form-data" action ="<?=$ PHP_SELF?> "Method =" post ">
<input type="file" name="myfile"> <br>
<input type="submit" value="upload" name="upload">
</ Form>
</ Body>
</ Html> 

When the upload button is pressed, then the url path to the file will be stored in the variable $ myfile, and we show dibrowser. Also displayed the name of the file that is stored in the variable $ myfile_name, the file size in the variable $ myfile_size and file type in the variable $ myfile_type.
We use the function copy( ), to move files from your computer's temporary directory on our server. Function copy( ) takes two arguments namely the location of the file and the new location on the server. If we want to upload to a folder on the server, (let's say we save the image difolder) then we need to convert it to: copy ($ myfile, "images / $ myfile_name").
/*
*/