Welcome to Webune Forums

We know the how important is for you to write your own scripts.

We provide you with tutorial that really work on our servers.

so if you ever wanted to learn how to upload pictures to your website, today we will show you with a simple script how you can do it.

since i am using windows 7 to create this script, i will be using notepad. if you are not using a windows computer, you can use your favorite text editor.

the first step we are going to do is create a directory (or folder), lets call it upload.

once you have created a new directory called 'upload', lets open a blank notepad, copy and paste the following PHP code:

PHP CODE:
<p align="center">Courtesy of<br><a href="http://www.webune.com"><img src="http://www.webune.com/images/headers/default_logo.jpg" alt="Webune Hosting" border="0"><br />Webune Web Hosting</a></p>
<?php
# SCRIPT CREATED BY WEBUNE.COM - BEST PHP WEB HOSTING!!!!!

# CONFIGURATION SETTINGS:
# THIS IS THE RELATIVE PATH TO THE IMAGES DIRECTORY: YOU CAN ALSO PROVIDE THE ABSOLUTE PATH IF YOU NEED TO
# IMPORTANT: BE SURE TO GIVE WRITE PERMISSIONS TO THIS DIRECTORY TO 777 PERMISSIONS
$Cfg['ImageDir'] = 'images/';



# STOP HERE - NO MORE CONFIGURATIONS 


# FUNCTION UPLOAD IMAGE FORM IN HTML
function ImageForm(){ ?>
	<form action="" method="post" enctype="multipart/form-data">
    Select Image: <input type="file" name="ImageFile" />
    <input type="submit" name="Submitted" value="Upload" />
    </form>
<? }
# CHEC IF THE FORM HAS BEEN SUBMITEED:
if(isset($_POST['Submitted'])){
	# HERE WE CHECK FOR ANY ERRORS, IF NOT ERRORS, WE CONTINUE
	if($_FILES['ImageFile']['error'] == 0){
		# CHECKS TO BE SURE WE CAN PUT THE UPLOAD FILE IN OUR IMAGE DIRECTORY
		if(!move_uploaded_file($_FILES['ImageFile']['tmp_name'],$Cfg['ImageDir'].$_FILES['ImageFile']['name'])){
			echo '<h1 style="color:red">PERMISSIONS ERROR: <BR>Check permission on the image directory: '.$Cfg['ImageDir'].' is set to 777</h1>';
			ImageForm();
		}else{
			# EVERYTHING WORKED OK.
			echo 'Congratulations!!! - You have uploaded a <strong>'.$_FILES['ImageFile']['type'].'</strong> file:<br>These are the Details:<br>';
			echo '<hr><PRE>';
			print_r($_FILES['ImageFile']);
			echo '</PRE><hr>';
			echo 'This is the Picture you uploaded:<br>';
			echo '<img src="'.$Cfg['ImageDir'].$_FILES['ImageFile']['name'].'">';
			echo '<hr>Try Again<br>';
			ImageForm();
		}
	}else{
		# THE FILES UPLOAD CONTAINS ERRORS
		echo '<h1 style="color:red">THERE WAS AN ERROR UPLOADING YOUR FILE</h1>The error code is: '.$_FILES['ImageFile']['error'].'';
		ImageForm();
	}
	
}else{
	ImageForm();
}
?>


now save it as upload.php in the upload directory.

the third thing to do is to create another directory called images within the 'upload' directory. this is the directory where our script is going to put the images we upload

now you should have a file called upload.php and a directory called 'images' in the upload directory

now upload the upload directory to your website using your FTP program.

IMPORTANT: be sure to set the permissions to 777 to the images directory, otherwise, the script will fail

ok, its time to see the script in action, so open the upload.php file on your website: example:
http://www.example.com/upload/upload.php

test it and you should see the images you upload.

WARNING: this script is very basic. the purpose is so you can see how it works. do not publish on your website, there are many security reasons why you dont want to publish it on your public website. as you learn more about this topic, you will start to learn what you can do to enhance the security.

for example, you can check if the file type is the correct one you allow. lets say i only want to allow 'jpge' files, then you would write code to make sure the the file type is equals to jpge.

We have also create the full script, you can download instead of creating it yourself
Upload-Script.zip