Video Tutorials for Uploading Images/Photos in PHP

By | June 10, 2013
uploading images in phpWe’ve been publishing PHP video tutorials on this blog and this will be continued, because I myself love writing codes. So I’ve come up with another PHP Tutorial which is about uploading images/photos in PHP. We’ll upload the images to the local server, but the same way is used to upload the images/photos to online web server. Uploading Images in PHP is a very easier thing. However, retrieving the images from the database and file server and showing them on the web page is a little bit logical task, however, in my upcoming Advance CMS and website project I’ll do this job very easily. Actually I want you to become a PHP web developer by following my this blog. So here we are, just learn uploading Images in PHP with video tutorials.

Uploading Images in PHP

We always have two servers for saving our content on the web, the first server is file server itself, and the second server is the database server which locates in the file server. However, the file server always sends the request to the database for accessing the information. So remember, that the text content is always stored in a database, while the media (images,videos,files,documents etc) is saved inside the file server. So during today’s tutorial we’ll upload our files to local file server.
We’ll use only HTML and PHP to make it happen, however, in a later tutorial of PHP, I’ll also guide you how to insert the Image name to Database and then show that image back on the web page. Stay tuned for that tutorial, but continue reading this post.

What you need to do?

Here is the basic HTML structure for uploading images/photos to web server, this is a simple HTML form which is just having an input element:

<html>
<head>
<title>Uploading images</title>
</head>
<body>
<form action=”upload_image.php” method=”post” enctype=”multipart/form-data”>
Select Image:<input type=”file” name=”image”>
<input type=”submit” name=”upload” value=”Upload Now”>
</form>
</body>
</html>

In the above form one thing is important that always add enctype=”multipart/form-data” attribute when dealing with media files such as images etc. Without adding this attribute the images won’t be uploaded to the server. Because an image has multiple data such as the name of the file, the type of the file, the size of the file and etc.
Now here are the video tutorials you can watch to learn this all, PHP codes is provided below the videos:
PHP Uploading Images Video #1
PHP Uploading Images Video #2
Here is the PHP script for uploading the images:

<?php
if(isset($_POST[‘upload’])){
$image_name = $_FILES[‘image’][‘name’];
$image_type = $_FILES[‘image’][‘type’];
$image_size = $_FILES[‘image’][‘size’];
$image_tmp_name= $_FILES[‘image’][‘tmp_name’];
if($image_name==”){
echo “<script>alert(‘Please Select an Image’)</script>”;
exit();
}
else
move_uploaded_file($image_tmp_name,”photos/$image_name”);
echo “Image Uploaded Successfully Here is the image”;
echo “<img src=’photos/$image_name’>”;
}
?>

After uploading the image to the file server, we also displaying the image directly on the browser by echoing that image. So this is just done by using $_FILES super Global Array in PHP.

Now here is the code altogether:

<html>
<head>
<title>Uploading images</title>
</head>
<body>
<form action=”upload_image.php” method=”post” enctype=”multipart/form-data”>
Select Image:<input type=”file” name=”image”>
<input type=”submit” name=”upload” value=”Upload Now”>
</form>
<?php
if(isset($_POST[‘upload’])){
$image_name = $_FILES[‘image’][‘name’];
$image_type = $_FILES[‘image’][‘type’];
$image_size = $_FILES[‘image’][‘size’];
$image_tmp_name= $_FILES[‘image’][‘tmp_name’];
if($image_name==”){
echo “<script>alert(‘Please Select an Image’)</script>”;
exit();
}
else
move_uploaded_file($image_tmp_name,”photos/$image_name”);
echo “Image Uploaded Successfully Here is the image”;
echo “<img src=’photos/$image_name’>”;
}
?>
</body>
</html>

I hopefully can say that you’ll now be able to upload images easily in PHP, however for Advance projects and CMS in PHP kindly stay tuned with this blog. Take Care.

6 thoughts on “Video Tutorials for Uploading Images/Photos in PHP

Speak Your Mind