Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //form to upload a file
- <form action="" method="post" ENCTYPE="multipart/form-data">
- <input type="file" name="photo"/>
- <input type="submit" name="submit" value="Upload"/>
- </form>
- //PHP code
- <?php
- if(isset($_POST['submit'])) //if submit button is clicked
- {
- if(isset($_FILES['photo'])) //if there is any file browsed
- {
- //check what is file extension
- $s=strtolower($_FILES["photo"]["name"]); //get the file name
- $ext=end(explode('.', $s)); //explode the name from '.'; if the file name is abc.jpg, $ext will get value 'jpg'; this is to check file extension
- if($ext!='jpg' && $ext!='jpeg' && $ext!='png') //check the image type, you can add more type here
- {
- echo "Please choose jpg/jpeg/png file";
- }
- else if($_FILES["photo"]["size"]>1000000) //file maximum size 1mb
- {
- echo "Please choose a file smaller than 1mb";
- }
- else{
- $pat=$_SERVER['HTTP_HOST'].'/images/'.$_FILES["photo"]["name"];
- move_uploaded_file($_FILES["photo"]["tmp_name"],$pat); //upload the file
- if($pat)echo "File uploaded";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement