Upload file using php

 Below code is used to upload a file using PHP.

sample.html:

<form  action="import.php" name="importform" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<tr>
                                <td height="30">&nbsp;</td>
                                <td class="Title">Profile </td>
                                <td><label>
                                  <input name="profile" type="file" id="profile" size="53" />
                                </label></td>
                              </tr>
</form>



import.php


// you can change this to any directory you want as long as php can write to it
$uploadDir1 = "/profilefolder/";

$fileName1 = $_FILES['profile']['name'];
$tmpName1  = $_FILES['profile']['tmp_name'];
$fileSize1 = $_FILES['profile']['size'];
$fileType1 = $_FILES['profile']['type'];

//Checking whether file is uploaded or not
if(strlen($fileName1)!= 0) {


// the files will be saved in filePath 
$rndno1=chr(rand(67, 85)).chr(rand(67, 85)).chr(rand(67, 85)).rand(0, 9).rand(0, 9).rand(0, 9).chr(rand(67, 85)).chr(rand(67, 85)).rand(0, 9).chr(rand(67, 85)).rand(0, 9);   

$filenme1=$rndno1;

//Extracting Extension of uploaded file
$ext1=explode(".",$fileName1);

//The file path where we will store the file where filename=randomnumber.ext
$filePath1 = $uploadDir1 . $filenme1.".$ext[1]";

// move the files to the specified directory
// if the upload directory is not writable or something else went wrong $result will be false

    $result1    = move_uploaded_file($tmpName1, $filePath1);
    if (!$result1) {
        echo "Error uploading file";
        exit;
    }
    
   
    if(!get_magic_quotes_gpc())
    {
        $fileName1  = addslashes($fileName1);
        $filePath1  = addslashes($filePath1);
    }  


}//if

No comments:

Post a Comment