- Colection of 65 PHP scripts for $4.29 each
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.
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.
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:
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.
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.
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.
68 Comments to "File uploading with PHP"







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);
});
}
});