uploading an image file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    uploading an image file

    hi every body,

    i am using the following code to upload an image. it directly jumps to the error message please tell me what im doing wrong here.

    Code:
    <form name="myform" enctype="multipart/form-data" method="post" action="">
    Upload Album/Artist Image : <input type="file" name="imguploader"> <br />
    <input type="submit" value="uploadnow" name="yes" />
    </form>
    
    <?php
    extract($_POST);
    
    if($yes){
    		$img_types2 = array("image/gif","image/pjpeg","image/jpeg","image/jpg","image/bmp","image/png");
    		
    		if($_FILES["imguploader"]["name"]!="" && in_array($_FILES["imguploader"]["type"],$img_types2)){
    		$img2 = time()."_".str_replace(" ","_",$_FILES["imguploader"]["name"]);
    		move_uploaded_file($_FILES["imguploader"]["tmp_name"],"images/".$img2);
    	} else {
    		echo "Image file not uploaded successfully.";
    	}
    
    }
    ?>

    this is displaying "Image file nto uploaded successfully". please tell me whats wrong with it.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    instead of $yes I'd go for isset($_POST['yes'])

    Comment

    • mfaisalwarraich
      New Member
      • Oct 2007
      • 194

      #3
      thanx dormilich its working now :). but what was the reason for not working with extract($_POST) ? cuz this is a very simple example. if im working on a more complex site where i have variables using extract($_POST) then i will not be able to upload the image file using this method? please tell me so i can know the concept of this. thank u again

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I don't know, but I consider that approach highly insecure (you may have unwanted (i.e. user injected) (global) variables going round which may serve to hack your script).

        Originally posted by mfaisalwarraich
        if im working on a more complex site where i have variables using extract($_POST) then i will not be able to upload the image file using this method?
        you have to know the variable's name in any case, so I see no problem. further, you can do this with isset:
        Code:
        if (isset($var1, $var2, $var3)) { … }
        // returns true only if all variables exist

        Comment

        • mfaisalwarraich
          New Member
          • Oct 2007
          • 194

          #5
          ok thank u once again. this is very useful help.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            There's an extra ending brace in there, too. Line 20.

            Comment

            • mfaisalwarraich
              New Member
              • Oct 2007
              • 194

              #7
              yes thank you its eliminated markus :)

              Comment

              Working...