Jump to content

[SOLVED] simple php image upload help?


perezf

Recommended Posts

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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/75399-solved-simple-php-image-upload-help/
Share on other sites

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(.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.