To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Misc Help > ClientSide Technologies

ClientSide Technologies Discuss HTML/CSS/Javascript, and any other client-side technologies, here.

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-30-2006, 09:51 PM   #1
DirtDemon
Junior Member
 
Join Date: May 2006
Location: IN
Posts: 11
Verify Image File Upload - Popup

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 Code:
<?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
        
}
        
?>
DirtDemon is offline   Reply With Quote
Old 05-31-2006, 03:34 AM   #2
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
You probably have better luck at a javascript forum, as this has nothing to do with PHP.
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 05-31-2006, 08:26 AM   #3
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
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");
}
}
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 05-31-2006, 05:58 PM   #4
DirtDemon
Junior Member
 
Join Date: May 2006
Location: IN
Posts: 11
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.
DirtDemon is offline   Reply With Quote
Old 05-31-2006, 06:25 PM   #5
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Nowhere in your PHP file, but in the HTML one that holds the txt field.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 05:06 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.