Recent Posts

How to upload files to Web Server in PHP 2016

PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.I am also lover of PHP and learn it.I am feeling very happy to share the php tutorials with you.So today I came with  tutorial in which i am going to show you how to upload files to web server in php.


So Lets start it below:
If you want to learn php you must know about a offline software of php called "Xampp".

PHP you must have installed either "XAMPP" or "WAMP" which provide you a complete environment for making web applications in PHP.
Whether you install xampp or wamp, after installing the software you'll find its folder directly in your C: drive. There will be a folder called "htdocs" (in Xampp) & "www" (in wamp) in your installed folder, so this is your root folder and you'll have to save all your files there in that folder, and after creating a file or new folder there. You'll just open it on your browser with this address: http://localhost/test/file.php, so in this example localhost is my local server and the test is a folder which I created inside "htdocs" folder and the file.php is my file inside test folder. Also after installing the xampp or wamp package check the installation by entering this address in your browser:http://localhost, so if it is installed correctly then it should work just fine.
These two software have all the necessary tools which are required in order to run PHP on your computer, because PHP is not a normal web language like HTML, CSS & JavaScript. It is an advance server side language which needs to be execute by a server. Now lets start the tutorial about adding files via PHP.

How to validate the files before uploading?

Now may be interested in this section. We can't allow people to upload the files as they want. We'll validate the variables for allowing people to upload only the specific kind of files. We'll validate the type of the files, the size of the files and after that we'll save the files somewhere on our local computer or in web server. So here is the complete PHP script for validating the files uploading them:

<?php
if (isset($_POST['submit'])) {
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$tmp=$_FILES['file']['tmp_name'];
if ($name==''){
echo "<script>alert('Please Select a file from computer!')</script>";
exit();
}
if (($type == "image/jpeg") || ($type == "image/gif") || ($type == "image/png")) {
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "This file $name is already exist!<br> please try another one..";
exit();
}
if ($size <= 50000){
move_uploaded_file($tmp,"images/$name");
echo "<center><font color=red>File was uploaded successfully!</font><br>Uploaded Image is here<br><img src='images/$name'> </center>";
}
else {
echo "Size of the image $size is larger than 50kb and it's not allowed... <br> image size must be less than 50kb..";
}
}
else {
echo "$type this is not a valid type of file<br>only upload Jpeg, PNG and Gif images";
}
}
?>

In above example I have given the complete PHP script altogether. In this script we added all the necessary validations and also we uploaded the file to our server via themove_uploaded_file default PHP command.
In above example, we said in an if statement that only Jpeg, PNG & gif type of images will be allowed to upload, also we said that size must be less than 50kb, and we also used a query which checks for the existing file names, this query will stop the script if it finds a file which is already existing. And finally we added the file to our computer by usingmove_uploaded_file function.
We also printed the image on the same page. So when you'll upload an image and click the upload file button the image will be shown on the same page. And we moved the uploaded file to a folder called "images" which you should create inside the folder where you've kept the file.php file. However, if you don't want to save the images inside a folder then change: $tmp,"images/$name" to only $tmp, $name and also change <img src='images/$name'> to <img src='$name'> so it will be uploaded directly to the same folder where the file.php is existing.
Now for your clarification the final code including both HTML and PHP which you can directly copy and paste inside a blank notepad document, you'll have to just save it as file.php and will have to save it inside "htdocs" folder in the xampp or wamp installation folder, here is the complete code:

<html>
<head>
<title>Uploading Files via PHP</title>
</head>
<body>
<center><form action="file.php" method="post" enctype="multipart/form-data">
<table border="2">
<tr height="100"><td>Upload A file:</td>
<td><input type="file" name="file"></td></tr>
<tr height="100"><td>Click Upload File:</td><td><input type="submit" name="submit" value="Upload File"></td></tr>
</table>
</form></center>
<?php
if (isset($_POST['submit'])) {
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$tmp=$_FILES['file']['tmp_name'];
if ($name==''){
echo "<script>alert('Please Select a file from computer!')</script>";
exit();
}
if (($type == "image/jpeg") || ($type == "image/gif") || ($type == "image/png")) {
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "This file $name is already exist!<br> please try another one..";
exit();
}
if ($size <= 50000){
move_uploaded_file($tmp,"images/$name");
echo "<center><font color=red>File was uploaded successfully!</font><br>Uploaded Image is here<br><img src='images/$name'> </center>";
}
else {
echo "Size of the image $size is larger than 50kb and it's not allowed... <br> image size must be less than 50kb..";
}
}
else {
echo "$type this is not a valid type of file<br>only upload Jpeg, PNG and Gif images";
}
}
?>
</body>
</html>


Now after saving the file check it on the browser with this address:http://localhost/test/file.php and you'll find the HTML form which will be showing the file upload field and uploading button, just upload a picture and click the button; you'll see the result, but making it sure you've created  a folder 'test' inside 'htdocs' and have saved the file.php inside test folder.
This tutorial was for uploading files to your local server by using XAMPP or wamp packages, but you can also use this tutorial for uploading files to your online web server.
I hope that after reading this article you can easily upload files to web server in PHP.If you like this article share it on social media to help other blogger brothers.
Let me know you if you have any problem regarding to this article.If you have any problem you can comment me in comment box.Thanks for reading this article.Stay tuned for more articles.

Share this

Related Posts

Previous
Next Post »