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.

Advertisements

One Response

  1. how can i cross check with php if this file is doc file or excel if not i dont want to allow uploads

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

%d bloggers like this: