- Forums
- PHP
- PHP Learn How To Upload Images Make PHP Script Code Tutorial Guide Steps
This Page Contains information about PHP Learn How To Upload Images Make PHP Script Code Tutorial Guide Steps By wallpaperama in category PHP with 12 Replies. [1008], Last Updated: Sat May 18, 2024
wallpaperama
Sun Feb 11, 2007
12 Comments
2072 Visits
yesterday i wanted to know how those websites do it so that they allow their visitors to upload pictures,images,pic,photos or photographs from their visitors into their website. well, i contacted our support team at www.webune.com and they were able to explain it to me. so i won't forget how to do thin in the future i am writing this small tutorial with its very own script code to show it works.
the first thing its to make sure you have PHP on your webstie. if you don't, you can visit www.webune.com and signup with one of their PHP plans. I recommend them to all my customers, Webune has excellent service and support.
step 2 is to make open your favorite text editor. since i am using windows xp, i will be using notepad.exe
step 3 copy and paste the following code into a blank notepad:
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>
<?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>
<?php }
# 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();
}
?>
step 4 save this file as upload.php (if you are going to be using notepad like i am, make sure when you save it put quotes like this: "upload.php" otherwise, notepad will save the file as notepad.php.txt)
step 5. FTP to your website and create a directory (folder) called upload.
step 6. once you have created a directory called "upload" (withoug quotes) upload the upload.php file to this new directory.
step 7. now create a new directory called image and make sure you give it write permission of 777. if you dont' do this step, the file will never upload to your website and this script is not going to work. and you will get an error
step 8. now you should have a file called upload.php and a directory called images in the upload directory. Open the upload.php file with your browser (example:
http://www.yourdomain.com/upload/upload.php and try uploading an image. try uploading a small .gif or .jpg or .jpeg file.
step 9. if everything goes well, you should have an image file in the images directory
NOTE: this is a very basic script. there are other things you should use to make it more secure, like only allowing image files and limiting the image size for example, but that's another tutorial, the purpose of this tutorial was to teach you or give you and idea of how it works. later you can customize it and make it more secure and perhaps post your code ont his thread to share your knowledge with other just like i am doing today with you.
DONE