go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

File uploading with PHP

Tuesday, 6th December, 2011  /  PHP Tutorials  /  68 Comments
PHP allows you to upload files using a simple HTML form on your web page directly on the server. There are few things that you need to do to make this working – page with web form, uploading script, folder on your server with permission set to be able to write in it.

Lets start with the form. Below you can see a basic web form for uploading a file.

<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="filename" />
<input type="submit" value="Upload" />
</form>


The most important about the form is that you should make it uses POST and not GET method and also you should use enctype="multipart/form-data" so it knows that a file will be transferred.
Now we need to make a folder (names 'uploading') on our web server and set its permissions to 777 so the upload.php script that we will do is able to write the file in it. In rare cases depending on your server configuration you may not need to do this. We should also know the absolute path on the server for that folder (example: /home/username/www/uploading/)
Now we are ready to make our upload.php script.

<?php
$folder = “/home/username/www/uploading/”;
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo “File uploaded”;
} else {
Echo “File not moved to destination folder. Check permissions”;
};
} else {
Echo “File is not uploaded.”;
};
?>


Now, I will explain what the above code do. When, a file is uploaded it is first given a temp filename and then put in the temp folder of your web server. That temp filename is accessible using the global $HTTP_POST_FILES array variable. On our web form we have our browse field named “filename” (<input type="file" name="filename" />), so the name of that temp file is:
$HTTP_POST_FILES['filename']['tmp_name']


The real name of the file being uploaded is stored in another variable called $HTTP_POST_FILES['filename']['name']. As you can see this just another array element named “name” in the $HTTP_POST_FILES['filename'] array.

Now after having that file upload in our web server temp folder we need to move it to the folder specified $folder=“/home/username/www/uploading/”. This is done using the move_uploaded_file() function.

move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])


the first parameter that it takes is the temp filename and the second parameter is the destination folder and filename. If it successfully moves the temp file to the folder that we want it returns TRUE and we made it print “File uploaded” message on the screen.
Share on:

68 Comments to "File uploading with PHP"

Amisha Shah

Amisha Shah / July 13, 2015 at 13:02 pm

I want to code for insert image in particular web form & dispaly in image field using php & mysql

GANDE

GANDE / June 16, 2015 at 13:05 pm

i want passport ( photo) uploaded in application form with file size less than 15kb and the passport previewed on same place without form submission. somebody pls help

jayasree

jayasree / September 30, 2014 at 12:08 pm

nice

Peter

Peter / August 1, 2014 at 10:16 am

Hi

Plse help, after the file is uploaded via the above upload script I want it also to be displayed on a php page. The next users upload must be displayed on the same page but next to the previous users etc etc... is this possible?

vipul singh

vipul singh / July 1, 2014 at 10:49 am

while applying the mention code in the tutorial, i'm getting the folllowing error msg..


Notice: Undefined variable: HTTP_POST_FILES in C:xampphtdocsfile_uploadupload.php on line 23
File is not uploaded.

Daniel

Daniel / April 10, 2014 at 07:56 am

So when I do it. every time it yells at me saying file is not uploaded. What should I change in the code to make it work?

Jamil

Jamil / February 8, 2014 at 22:09 pm

Consider having someone proof-read articles before uploading them. It's important to make your content look professional.

rokad naina

rokad naina / January 29, 2014 at 19:05 pm

for php website solution.......

jams

jams / December 7, 2013 at 17:01 pm

how we send input type="file" path's to php page via Ajax ???

 $("#uploadBtn1").change(function (){
var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1)
$("#val1").html("Only '.jpeg','.jpg', '.png', '.gif', '.bmp' formats are allowed.");
else {
$.post('checkImg.php',{action:$(this).val()},function(res){
$("#val1").html(res);
});
}
});

jams

jams / December 7, 2013 at 17:03 pm

i get File path in Php page but how i use $_File['uploadBtn1']['name'] and other such tags ???

kavimani

kavimani / September 7, 2013 at 09:34 am

hi i want coding for download the upload file in php without using database

Add your comment

Captcha

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts