alconebay Posted April 2, 2008 Share Posted April 2, 2008 I was following this tutorial (http://adriaanvm.com/archives/howto-make-a-simple-files-upload-system) on a file upload system for images and everything is working perfect, only the upload system lets anything under 100kb in. Even if it's not a image. That tutorial said you could restrict file types to images using this: $allowed_extensions = array( 'png', 'gif', 'bmp', 'jpg', 'jpeg' ); and using this: $filename = $_FILES['file']['name']; $new_filename = explode('.', $filename); if(in_array($new_filename[count($new_filename) -1], $allowed_extensions)) { // Continue with the code } else { // Stop the code. } But where do I put that code within this: <?php if(isset($_POST['submit'])) { // If action is add. if($_POST['action'] == 'add') { // Define the folder where the file will be uploaded. $upload_path = './uploads/' . $_FILES['file']['name']; // Check if the file already exists in the folder. if(!file_exists($upload_path)) { // Check the size of the file. // The file should be under 100 kb. But OVER 0 bytes. if($_FILES['file']['size'] < 100000 && $_FILES['file']['size'] > 0) { // If there are any errors. if($_FILES['file']['error']) { echo "Error: " . $_FILES['file']['error']; } else { // Success! echo "File successfully uploaded.<br /><br />"; echo "The filename is: <strong>" . $_FILES['file']['name'] . "</strong><br />"; echo "The filesize is: <strong>" . $_FILES['file']['size'] . "</strong><br />"; echo "The filetype is: <strong>" . $_FILES['file']['type'] . "</strong><br />"; echo "The file is temporary saved in: <strong>" . $_FILES['file']['tmp_name'] . "</strong><br />"; // Move uploaded file to upload folder. move_uploaded_file($_FILES['file']['tmp_name'], $upload_path); } } else { echo $_FILES['file']['size'] < 1 ? 'Incorrect filename.' : 'File size is too big'; } } else { echo "File already exists in your folder. "; } } } ?> Ive tried just about every way I can think of to insert those two snips of code into the large section of code but I cant get it to work. It still lets non image files in. Quote Link to comment Share on other sites More sharing options...
zenag Posted April 2, 2008 Share Posted April 2, 2008 do like this... <?php if(isset($_POST['submit'])) { $allowed_extensions = array( 'png', 'gif', 'bmp', 'jpg', 'jpeg' ); $new_filename = explode('.', $_FILES['file']['name']); if(in_array($new_filename[count($new_filename) -1], $allowed_extensions)) { $upload_path = "D:/apachefriends/xampp/htdocs/prac/uploads/" . $_FILES['file']['name']; if(!file_exists($upload_path)) { if($_FILES['file']['size'] < 100000 && $_FILES['file']['size'] > 0) { if($_FILES['file']['error']) { echo "Error: " . $_FILES['file']['error']; } else { // Success! move_uploaded_file($_FILES['file']['tmp_name'], $upload_path); echo "File successfully uploaded."; echo "The filename is: <strong>" . $_FILES['file']['name'] . "</strong>"; echo "The filesize is: <strong>" . $_FILES['file']['size'] . "</strong>"; echo "The filetype is: <strong>" . $_FILES['file']['type'] . "</strong>"; echo "The file is temporary saved in: <strong>" . $_FILES['file']['tmp_name'] . "</strong>"; // Move uploaded file to upload folder. } } else { echo $_FILES['file']['size'] < 1 ? 'Incorrect filename.' : 'File size is too big'; } }else { echo "File already exists in your folder. "; } } else { echo "not a valid extension"; } } ?> Quote Link to comment Share on other sites More sharing options...
alconebay Posted April 2, 2008 Author Share Posted April 2, 2008 Works perfect! Thanks! Quote Link to comment Share on other sites More sharing options...
alconebay Posted April 2, 2008 Author Share Posted April 2, 2008 On more question, I want to rename the image being uploaded to add the uploaders user name (which is carried over from step one of the form) and add it to the beginning of the image being uploaded. So it would look like "username_theirimage.jpg." (btw, I don't want to limit it to jpgs) I'm assuming I would modify this line: $new_filename = explode('.', $_FILES['file']['name']); and integrate the $username into the filename. How would I do that? Thanks Quote Link to comment Share on other sites More sharing options...
alconebay Posted April 2, 2008 Author Share Posted April 2, 2008 It took me 4 hours (Im a noob) but I figured it out! Here is everything: <?php $username = 1234(or whatever); if(isset($_POST['submit'])) { $allowed_extensions = array( 'png', 'gif', 'bmp', 'jpg', 'jpeg', 'PNG', 'GIF', 'BMP', 'JPG', 'JPEG' ); $filename = $_FILES['file']['name']; $prefixed = $username . "_" . $filename; $filesize = $_FILES['file']['size']; $fileerror = $_FILES['file']['error']; $filetmp = $_FILES['file']['tmp_name']; $new_filename = explode('.', $prefixed); if(in_array($new_filename[count($new_filename) -1], $allowed_extensions)) { $upload_path = "./mypath/" . $prefixed; if(!file_exists($upload_path)) { if($filesize < 500000 && $filesize > 0) { if($fileerror) { echo "Error: " . $fileerror; } else { // Success! move_uploaded_file($filetmp, $upload_path); echo "Finished"; } } else { echo $filesize < 1 ? 'too big.'; } }else { echo "name already used"; } } else { echo "bad extension."; } } ?> That will rename name "photo.jpg" to "1234(or whatever)_photo.jpg" ($username_photo.jpg) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.