Skip to main content

In today post we will focus on how to create php script that could upload picture to server

Before writing script to upload files to server, you have to check ensure that php file upload is set to 'On' in php.ini. Open C:\xampp\php\php.ini file locate the line that says upload_file = On and also change the php upload max setting, example: php_max_upload = 100m. Now save the file, restart the server and start writing php script to upload your file.

Step 1: Below is the html code to implement our practice of file upload. You can write it manually or copy and paste.

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" method="POST"> <fieldset style="border-radius:10px;"> <legend>Uploader</legend> <input type="file" name="fileUpload"> <input type="submit" name="uploadFile" value="Upload"> </fieldset> </form>

Step 2: Php command to target the upload folder, php comman to get file name, php command to check upload status, and php command to validate file extension. Below is our sample code:

<!--php $folder = "uploads/"; $image = $folder . basename($_FILES["fileUpload"]["name"]); $status = 1; $filetype = strtolower(pathinfo($image,PATHINFO_EXTENSION)); </xmp--> <p><b>Step 3: </b> We will use php global object called <b>$_POST</b> to get the image object. And check if the file is image or not, if it's not image the upload status will be set to zero. Below is our sample code: </p> <xmp style="background:#000000; color:white; border-radius:5px; padding:5px; overflow-x:scroll;"> if(isset($_POST["uploadFile"])) { $check = getimagesize($_FILES["fileUpload"]["tmp_name"]); if($check !== false) { echo "<p>File is an image - " . $check["mime"] . ".</p>"; $status = 1; } else { echo "<p>File is not an image.</p>"; $status = 0; } }

Step 4: We will use php function object called file_exists() to check if the file already exist. If the file already exist, our upload status is set to zero.

if (file_exists($image)) { echo "<p>Sorry, file already exists.</p>"; $status = 0; }

Step 5: We will validate the size of file that should be allows to be uploaded in our server. If the file is too large php will reject the upload. Below is our upload sample files size validation.

if ($_FILES["fileUpload"]["size"] > 500000) { echo "<p>Sorry, your file is too large.</p>"; $status = 0; }

Step 6: We will check file extension, we don't allow every file to be uploaded to our server. If upload file with extension we don't accept, php should reject the upload Below is file extension validation.

if($filetype != "jpg" && $filetype != "png" && $filetype != "jpeg" && $filetype != "gif" ) { echo "<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>"; $status = 0; }

Step 7: If everything goes well try upload our file. But if status was set to zero, return error message or continue to upload the file.

if ($status == 0) { echo "<p>Sorry, your file was not uploaded.</p>"; } else { if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $image)) { echo "<p>The file ". htmlspecialchars( basename( $_FILES["fileUpload"]["name"])). " has been uploaded.</p>"; } else { echo "<p>Sorry, there was an error uploading your file.</p>"; } }

Finally, below is complete code to upload our file, you can do copy and paste.

<!--php $folder = "uploads/"; $image = $folder . basename($_FILES["fileUpload"]["name"]); $status = 1; $filetype = strtolower(pathinfo($image,PATHINFO_EXTENSION)); if(isset($_POST["uploadFile"])) { $check = getimagesize($_FILES["fileUpload"]["tmp_name"]); if($check !== false) { echo "<p-->File is an image - " . $check["mime"] . ".</p>"; $status = 1; } else { echo "<p>File is not an image.</p>"; $status = 0; } if (empty($image)) { echo "<p>Sorry, you did not select file to upload.</p>"; $status = 0; } if (file_exists($image)) { echo "<p>Sorry, file already exists.</p>"; $status = 0; } if ($_FILES["fileUpload"]["size"] > 500000) { echo "<p>Sorry, your file is too large.</p>"; $status = 0; } if($filetype != "jpg" && $filetype != "png" && $filetype != "jpeg" && $filetype != "gif" ) { echo "<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>"; $status = 0; } if ($status == 0) { echo "<p>Sorry, your file was not uploaded.</p>"; } else { if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $image)) { echo "<p>The file ". htmlspecialchars( basename( $_FILES["fileUpload"]["name"])). " has been uploaded.</p>"; } else { echo "<p>Sorry, there was an error uploading your file.</p>"; } } } ?> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" method="POST"> <fieldset style="border-radius:10px;"> <legend>Uploader</legend> <input type="file" name="fileUpload"> <input type="submit" name="uploadFile" value="Upload"> </fieldset> </form>

If the above code spot error, we can get in touch, you can contact us for further code review. Thanks for reading our articles.

Comments

Latest post

We will take a closer look on how to bind webpage in python programming language

Today we will take a closer look on how to run python in your webpage under windows operating system. Let's assume you have installed xampp in your windows operating system and have python program installed as well. If you have not done so, then go on to installed xampp in your computer together with python program. Remember to select software version compactible to your computer. When you are ready with these two software, then copy the code below: #!"C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\python.exe" print ("Content-Type: text/html\n") print (" ") print (" ") print (" ") print (" ") print (" Hello world! ") print (" Welcome to my first python webpage ") print (" ") print (" ") Save the above code as index.py in C:\xampp\htdocs the path should be: C:\xampp\htdocs\index.py On your browser address bar you can type http://localhost/index.py, ...

Our today post will focus on how to implement php class and method

A class is a container for objects, and object is items in class. Class is container that we can store all kinds of obejects, such as variable, function etc. Today we will implement class and objects , property, set, get and the rest. If you look at the php class below, this is exactly how to declare empty php class. Php class container with public objects, function set parameters and gets parameters. In php class below, we created two functions, one to set a value and one to get the value, pass the instance object and its value. Now you can copy the below code for your testing. name = $name; } function getname() { return $this->name; } } $username = new KcFuns(); $username->setname('Kenan Chiquado'); echo "Output:"; echo "Username is: ".$username->getname(); echo "----------------------------------"; ?> Php class several objects function and parameters In our second php class implementation, we will lo...

Today we will look into how to implement cUrl in php application

Passing json file via cUrl in php We will implement passing json file via cUrl and submit to next server. cUrl is all about special communication between two or more server. Such as server1.com sending data to server2.com. A lot of beginner had always ask endless question about what is cUrl and what cUrl actually does. A simple answer there is: cUrl is a special communication between two or more server. Now, if you look at json code below, it has been neatly written and indented and ready for testing. Launch your notepad copy the json data below and save it as data.json place it in a folder where you saved other php file. { "id": "1", "image": "image\/chiquado.jpg", "fullname": "Kenan Chiquado", "fulladdress": "Udah, Igbo-eze North LGA, Enugu state, Nigeria", "emailaddress": "2348130802790", "phonenumber": "presschiquado@gmail.com...

Our today post will focus on how to implement php hits counter

PHP counter step 1: Have you ever wonder how you can monitor, view or learn how many times your website is been processed by visitors. How many times your website is been browse by your visitors? If your answer is 'no', then it's time to learn all these. You can learn how many times browser processed your website by binding certain php script on top of your webpage. Now, if you look at our php script below, this script will help you count how many times your website was processed at any given moment. To apply our php script below, you can copy and paste this script on top of your webpage before html declaration. PHP counter step 2: Again, the below script, you can copy and put it in a separate file. All you need to do is check time to time and see the digits number your script return. That's exactly how many times your website was processed. Example of the counter: Hits counter While applying our tutorials, if our code spot error, don't hesit...