Upload ppt, doc or any kind of file in PHP

Do u have to upload files using PHP ? Files like .ppt, .doc, any kind of image files etc you can upload using following code. Make sure the folder where you want to upload files have write permission.

In your interface(like named resource.php) add the following code:

<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
<p>
<label for=”file”>Select a file:</label> <input type=”file” name=”userfile” id=”file”> <br />
<button>Upload File</button>
<p>
</form>

Now create another page named upload.php to write upload functions:

<?php

//set target path where u wanna upload files

$target_path = “./pics/”;

$target_path = $target_path . basename( $_FILES['userfile']['name']);
//for upload file this function we use
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
echo “The file “. basename( $_FILES['userfile']['name']).
” has been uploaded”;
} else{
echo “There was an error uploading the file, please try again!”;
}

?>

Look it is very simple in PHP to upload files for you. Be happy and continue working.

Leave a Reply