Skip to main content

When I was learning the basics of PHP, I coded a simple PHP image uploading script. This script is totally programmed using the basics of PHP. So heres the PHP working. If you are learning PHP yourself then this code sample should help you understand the basic working of variables, if else statements etc. If you feel like downloading the whole script , I have posted a link at the bottom of the post!

<?php

// Author = Prateek Katyal
 if (isset($_FILES['image']))
 {
 $errors = array();
 $allowed_ext = array("jpg", "jpeg", "png", "gif");

$file_name = $_FILES['image']['name'];
 $file_ext = strtolower(end(explode(".", "$file_name")));
 $file_size = $_FILES['image']['size'];
 $file_temp = $_FILES['image']['tmp_name'];
 $finalurl = "http://prateekatyal.com"; // Enter URL of the script here (Do not end in slash)

if (in_array($file_ext, $allowed_ext) === false)
 {
 $errors[] = "<div id='result'><font face='MONACO'>Errrrrrrrrrrrrr,try again!</font></div>";
 }
 if ($file_size>2097152)
 {
 $errors[] = "<div id='result'><font face='MONACO'>I am afraid we don't accept images larger than 2MB</font></div>";
 }

if (empty($errors))
 {
 if (move_uploaded_file($file_temp, "images/".$file_name))
 {
 echo "<div id='result'><font face='MONACO'>Bingo! The file has been uploaded successfully. <a href='$final_url/upload/images/$file_name' target='_blank'>$file_name</font></a></div>";

}
 }
 else {
 foreach($errors as $error)
 echo "$error <br>";

}

}

?>

 

Prateek Katyal

Prateek Katyal

I create ‘stuff’ on the interwebs ? ‘Stuff’ ? photos, videos, websites ?

Leave a Reply