Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(isset($_FILES['avatar'])){
- // Let's store that bad boy in a variable
- $img = $_FILES['avatar'];
- // In what folder do we want to save the file in?
- $folder = "uploads";
- // Let's find that extension
- $extension = pathinfo($img['name'], PATHINFO_EXTENSION);
- // Generate a 'random' name
- $randomFileName = time() . ".$extension"; // Example: 123124124.png
- // The new generated file name that points to the location that we want the file to be in
- $fullFileName = $folder . "/" . $randomFileName; // Example: uploads/123124124.png
- // If it has to be an IMAGE:
- $isImage = getimagesize($img['tmp_name']);
- if(!$isImage) {
- $error = "The file has to be an image!";
- }
- // If it has to be an IMAGE:
- $size = filesize($img['tmp_name']);
- if($size > 2097152) { // 2MB
- $error = "This file is too large. It must be smaller than 2MB!";
- }
- // If there are no errors, let's move the file
- if(!isset($error)){
- // Try to move the file
- $upload = move_uploaded_file($img['tmp_name'], $fullFileName);
- if($upload){
- // Yay! It is uploaded.
- } else {
- $error = "File could not be uploaded. Such a shame!";
- }
- }
- }
- ?>
- <form action="upload.php" enctype="multipart/form-data" method="POST">
- <?php if(isset($error)) echo $error ?>
- <input type="file" name="avatar">
- <input type="submit">
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement