2 files
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title>Upload Files using normal form and PHP</title> </head> <body> <form enctype="multipart/form-data" method="post" action="process.php"> <div class="row"> <label for="fileToUpload">Select a File to Upload</label><br /> <input type="file" name="fileToUpload" id="fileToUpload" /> </div> <div class="row"> <input type="submit" value="Upload" /> </div> </form> </body> </html> |
(process.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php if ($_FILES['fileToUpload']['error'] > 0) { echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />"; } else { // array of valid extensions $validExtensions = array('.jpg', '.jpeg', '.gif', '.png'); // get extension of the uploaded file $fileExtension = strrchr($_FILES['fileToUpload']['name'], "."); // check if file Extension is on the list of allowed ones if (in_array($fileExtension, $validExtensions)) { // we are renaming the file so we can upload files with the same name // we simply put current timestamp in fron of the file name $newName = time() . '_' . $_FILES['fileToUpload']['name']; $destination = 'uploads/' . $newName; if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination)) { echo 'File ' .$newName. ' succesfully copied'; } } else { echo 'You must upload an image...'; } } ?> |