Click to See Complete Forum and Search --> : Verify Image File Upload - Popup


DirtDemon
05-30-2006, 09:51 PM
Hello, I have the following script which uploads a file, I have some code that limits the file type to a JPG, GIF, or PNG and I want to pop a message if the user trys to upload something else and stay on the page. Currently it prevents the file from uploading but goes through with the update and goes to the page I set it to go to after the button is pressed.

I have tried several Java Script bits of code but can;t get it to work, so I figured I would post all the code up to see if someone can toss something out real quick.

Right now I just left the code echo "ERROR!"; but nothing shows..


Thanks!

<?php
if($_POST['Update']){
$uploaddir = '/homepages/12/d114244980/htdocs/MaximumDirt/images/drivers/';

//begin file type check
if ($_FILES['userfile']['name']) {
$ext=substr($_FILES['userfile']['name'], -3);
if ($ext != "jpg" && $ext !="gif" && $ext !="png") {
echo "ERROR!";
exit();
}
}
//end file type check

$picname = $_FILES['userfile']['name'];
$random=rand(1000,9999);
$newpicname=$random.$picname;
$uploadfile = $uploaddir.$newpicname;

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
$sql_update = mysql_query ("UPDATE driver_profiles SET pic1='".($newpicname)."'");

//begin resize code
// Get image dimensions
$imagesize = getimagesize($uploadfile);
//here im saying that if the width of the image is greater than 465 then it will be resized, otherwise everything below this is ignored completely
if ($imagesize[0] > 465) {
$newwidth = 465; $newheight = ((465 / $imagesize[0]) * $imagesize[1]);
//THE RESIZE
//find the file extension
$image_type = $imagesize[2];
//decide which imagecreate line to use- this is because i allow various types of image files to be uploaded (jpg, png etc) so the code needs to know whether to use imagecreatefromjpeg, imagecreatefromgif etc. If youre only using the one kind of image, you dont need this
if ($image_type == '2') {
$source = imagecreatefromjpeg($uploadfile);
$create = imagejpeg;
}
elseif ($image_type == '1') {
$source = imagecreatefromgif($uploadfile);
$create = imagegif;
}
elseif ($image_type == '3') {
$source = imagecreatefrompng($uploadfile);
$create = imagepng;
}
elseif ($image_type != '3' || $image_type != '1' || $image_type != '2') {
echo ("invalid file type");
exit; }
//image create
$newimage = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $imagesize[0], $imagesize[1]);
// Output
$create($newimage, $uploadfile, 75);
}
//end resize code
}
?>

leatherback
05-31-2006, 03:34 AM
You probably have better luck at a javascript forum, as this has nothing to do with PHP.

JPnyc
05-31-2006, 08:26 AM
Something like:

function fileCheck() {
var txtfield=document.forms[0].txtbox.value;
if((txtfield.indexOf('.png')==-1 && (txtfield.indexOf('.jpg')==-1 && (txtfield.indexOf('.gif')==-1) {

alert("File must be a PNG, JPG, or Gif");
}
}

DirtDemon
05-31-2006, 05:58 PM
Thanks for the reply JPnyc!

Could you help me to understand where to incorporate that into my code?

I am unfamiliar with the VAR, I have very basic PHP knowledge. :confused:

JPnyc
05-31-2006, 06:25 PM
Nowhere in your PHP file, but in the HTML one that holds the txt field.