Posted by: prajapatinilesh | May 8, 2009

Process the uploaded file in php instead of storing it into physical location

Hi,
We are using   move_uploaded_file($_FILES[‘myFile’][‘tmp_name’], ‘/upload_files/myFile.txt’);   function in php to upload files into physical location (destination directory).
But in case if you want to process that uploaded file instead of storing it into destination directory then we can use other php function for file handling.
For example:

if (is_uploaded_file($_FILES[‘myFile’][‘tmp_name’]))
$fileData = file_get_contents($_FILES[‘myFile’][‘tmp_name’]);

$fileData = str_replace(“A”, “B”, $fileData);

OR
if file size is large then better to use following way: means use of diff file handling functions of php such as fopen(), feof(), fclose(), etc.

if (is_uploaded_file($_FILES[‘myFile’][‘tmp_name’])) {
$filePointer = fopen($_FILES[‘myFile’][‘tmp_name’], “rb”);

if ($filePointer!=false){
while (!feof($filePointer)){
$fileData = fread($filePointer, 4096);
// Process the contents of the uploaded file here… and also we can make insert query to store into db
}
fclose($filePointer);
}

}

Working Example:

<?php
if ($_FILES) {
echo “<pre>”;
print_r($_FILES);
echo “</pre>”;

$fileContent = file_get_contents($_FILES[‘upload’][‘tmp_name’]);
echo “<pre>”;
print_r($fileContent);
echo “</pre>”;
//now you can process your content here…
die;
}
?>

<form enctype=”multipart/form-data” method=”POST”>
<input type=”file” name=”upload”>
<button type=”submit”>Enter</button>
</form>
Thanks,   Nilesh.

Advertisements

Responses

  1. hello Nileshbhai,

    I want upload file using ajax & dont want to refresh page (PHP) ,
    actually I had used popup (ajax- like lightbox) & from there I want to upload file & want to print data whatever in that file in one text area which is on the same page which displayed after close of ajax-pop-up & that popup closed after file read or uploaded file whatever .
    so can you help me??????


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: