perezf Posted October 30, 2007 Share Posted October 30, 2007 how can i make my script only allow jpg, gif, png also i wanted to know how i can rename the file on upload? the upload works i just need to know how to rename the file on upload and add the restrictions <?php // start session session_start(); // check to make sure the session is registered if(session_is_registered('username')) {} else {} ?> <?php include('includes/header.inc.php'); ?> <?php if($_POST['uploadimage'] == "Upload Image") { $target_path = "uploaded_pictures/"; $target_path = $target_path . basename( $_FILES['imgfile']['name']); $_FILES['imgfile']['tmp_name']; if(move_uploaded_file($_FILES['imgfile']['tmp_name'], $target_path)) { $imguploadnote = "<p>The file ". basename( $_FILES['imgfile']['name']). " has been uploaded</p>"; } else{ $imguploadnote = "<p>There was an error uploading the file, please try again!</p>"; } } ?> <?php print $imguploadnote; ?> <h1>Add a <span class="green">Picture</span></h1><br> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="50000"> Upload Image: <input type="file" name="imgfile"><br> <input type="submit" name="uploadimage" value="Upload Image"> </form> <?php include('includes/footer.inc.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75399-solved-simple-php-image-upload-help/ Share on other sites More sharing options...
MadTechie Posted October 30, 2007 Share Posted October 30, 2007 could search the form.. change $target_path = $target_path . basename( $_FILES['imgfile']['name']); to <?php $ext = false; switch($_FILES['imgfile']['type']) { case "image/pjpeg": case "image/jpeg": case "image/jpg": $ext = ".jpg"; break; case "image/png": $ext = ".png"; break; case "image/gif": $ext = ".gif"; break; } if($ext === false) die("Bad File Type"); $target_path = $target_path ."NewName".$ext; ?> EDIT: written live, so just a quick untested script also $target_path = $target_path ."NewName".$ext; might need to be $target_path = $target_path ."NewName.".$ext; //Note the extra dot(.) Quote Link to comment https://forums.phpfreaks.com/topic/75399-solved-simple-php-image-upload-help/#findComment-381416 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.