Uploading an image and copying to a folder

Discussion in 'PHP' started by PHPLOST, Nov 7, 2010.

  1. #1
    I'm trying to allow a user to upload an image, and have that image copied to an Image folder on the server
    When I run the code I get the success message near the bottom of the code, but the image never gets copied to the folder. I'm not sure what's causing it to not copy the image. It doesn't seem to be the permissions on the folder as I can add/edit/delete stuff from the folder.




    if($_POST["store"] != "") // if upload was hit
    {	$fileExt = strrchr($_FILES['userfile']['name'], "."); // grab extension
    	if($fileExt != ".jpg" && $fileExt != ".jpeg") // check extension	
    {		$_SESSION["badFileType"] = "You cannot upload a file of type ".$fileExt; // set error	
    }	
    else	
    {		
    $fileName = $_FILES['userfile']['name']; // set file name		
    if(!is_uploaded_file($_FILES['userfile']['tmp_name'])) 		
    {			
    echo "Problem: possible file upload attack!"; // set error			
    exit(); // end		
    }				
    $upfile = "../Images/lg_".$category.$counter.".jpg"; // set path		
    $newFileName = $category.$counter.".jpg"; // set new file name		
    if(!copy($_FILES['userfile']['tmp_name'], $upfile)) // if not able to copy		
    {			
    echo "Problem: file could not be copied to directory!"; // set error			
    exit(); // end		
    }		
    $_SESSION["badfileType"] = "File uploaded successfully!"; // set success message	
    }
    }
    else
    {	$_SESSION["badFileType"] = ""; // clear error
    } 
    
    PHP:

     
    PHPLOST, Nov 7, 2010 IP
  2. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    not sure exactly whats wrong with your code. but I have an image upload script I used to use. I can share it with you. maybe you can port it to your script if the spirit moves :
    
    function img_upld($img_name, $img_location){
    	global $_FILES;
    	$img_err = false;
    
    	if (file_exists($img_location.(str_replace(" ", "_" ,$_FILES[$img_name]['name'])))) {
    		$_SESSION['err'] = $_FILES[$img_name]['name']."  is already there, please choose another name.<BR>";
    		$img_err = true;
    	} 
    	//if the file size is less than 0 - throw an error and go to prev document
    	if ($_FILES[$img_name]['size'] == 0){
    		$_SESSION['err'] = "there is something wrong with one of your file sizes -- it's 0 bytes? <BR>";
    		$img_err = true;
    	}
    	//if the file size is larger than 5mb - throw an error and go to prev document
    	if ($_FILES[$img_name]['size'] > 5000000){
    		$_SESSION['err'] = "there is something wrong with one of your file sizes -- it's larger than 5 megabytes? <BR>";
    		$img_err = true;
    	}
    	//if the file is not a jpeg - throw an error and go to prev document
    	$allowedmimes = array ("image/pjpeg", "image/jpg", "image/pjpg", "image/jpeg","image/png", "video/flv", "image/gif");
    	
    	if (!in_array ($_FILES[$img_name]['type'],$allowedmimes)){
    		$_SESSION['err'] = "there is something wrong with one of your files $xx -- it's not a jpeg? <BR>";
    		$img_err = true;
    	}
    	if($img_err == false){
    		if (!move_uploaded_file($_FILES[$img_name]['tmp_name'], $img_location.($_FILES[$img_name]['name']))){
    			$_SESSION['err'] = "The files could not be moved";//.$img_location.(str_replace(" ", "_" ,$_FILES[$img_name]['name']));
    			?>
               <SCRIPT LANGUAGE="javascript" >alert('<?php echo $_SESSION['err']; ?>');</SCRIPT>
                <?php
    			exit();
    		}
    	}else{
    			?>
               <SCRIPT LANGUAGE="javascript" >alert('<?php echo $_SESSION['err']; ?>');</SCRIPT>
                <?php
    			exit();
    	}
    }
    ?>
    
    Code (markup):
    and the form would look something like this :
    
    <FORM enctype='multipart/form-data' ACTION="processor.php" METHOD="POST" TARGET="processor" NAME="imgform" ID='imgform' >
        	<INPUT TYPE="file" ID="img" NAME="img" />
            <INPUT TYPE='hidden' NAME='axn' ID='axn'  VALUE='uploadimg'/>
            <A STYLE='cursor:pointer;color:red;text-decoration:underline' onclick = 'document.forms.imgform.submit()' >UPLOAD IMAGE</A>	<!-- http://www.phantom48.net/HOME/imgs/abreaks.jpg -->
        </FORM><br />
    <DIV ID="imgprev" ></DIV>
    
    Code (markup):
    and the form handler :
    
    <?php
            $imgsroot = '../mydirectory/';
    	img_upld('img', $imgsroot);
    	?>
    	<SCRIPT LANGUAGE="javascript" >
        	parent.document.getElementById('imgprev').innerHTML = '<IMG SRC="<?php echo $hostroot."imgs/".$_FILES['img']['name']; ?>" HEIGHT="150" >&nbsp;&nbsp;<A onclick = "sendpost_formless(\'imgprev\', \'processor.php\', \'axn=deleteimg&img=<?php echo $_FILES['img']['name']; ?>\')" STYLE="cursor:pointer;font-size:x-large;border:solid thin red" >X</A>';
    		//parent.document.forms.recipe_form.img_file.value = '<?php echo $_FILES['img']['name']; ?>';
        </SCRIPT>
    
    Code (markup):
     
    Last edited: Nov 7, 2010
    buddyborg, Nov 7, 2010 IP
  3. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    BTW you may wanna start using swfupload : swfupload.org/
     
    buddyborg, Nov 7, 2010 IP
  4. PHPLOST

    PHPLOST Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, fo the info
     
    PHPLOST, Nov 7, 2010 IP