- Forums
- PHP
- How To Test If Images Are Allowed To Upload On Your Website
this is a very simple php script to test if upload file works on website or not. nothing complicated, it will help you find out errors or problems you may have with uploading images or picture or other files on your website [8882], Last Updated: Sat May 18, 2024
phpguro
Wed Dec 31, 1969
0 Comments
101 Visits
if you have a website that allows your users to upload files or images to your website, its sometimes useful to test yours site for file uploads. this is also important for troubleshooting.
so lets say you have a PHP script which allows your visitors to upload a picture, but its not working. so what do you do?
i have a simple solution. run a simple script to test if you can upload to your website. so follow these simple steps.
1. login to your website with your FTP account.
2. create a directory called test
3. create a directory in the test directory called uploads
4. if you are using windows as i am, open a blank notepad and copy and paste the following code. then save the file as: test-upload.php
<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>
<?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();
}
?>
5. now upload the script to your website in the test directory
6. step six is very important, if you fail to do this step, your script will not work. you need to set 777 permissions to the uploads directory.
7. with the proper permissions, now access the test-upload.php from your website: for example: http://www.example.com/test/test-upload.php
thats it