Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   image upload (http://www.go4expert.com/forums/image-upload-t22971/)

pein87 9Aug2010 00:27

image upload
 
Well some people may or may not know how to do this but I find that its best to explain the process. First were going to upload the file and uses the auto global $_FILE to get the uploaded files info before we save it.

HTML Code:

<form method="post" action="upload.php">
<input type="file" name="img" />
</form>

upload.php

PHP Code:

<?php

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types
$max_size 40000// max file size in bytes

$img_size $_FILE["img"]["size"]; // returns the files size in bytes

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image

$img_err $_FILE["img"]["error"]

/* start the process of the validation */

if ($img_err 0)
{
 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again.";
}
else
{

if (
in_array(substr($img_name,0,-3), $img_exts) && $img_size $max_size)
{

move_uploaded_file($img_temp_loc,"/images" $img_name);

}

else
{

echo 
"either your image was to big or the file was not a valid image files please correct this and try again.";

}

}

?>

instead of checking the $img_type variable which returns image/jpeg we check the $img_name since the extension is at the end of the name of the file. We also check to see if the size in bytes is less than our max bytes and if so move it to a permanent location in the webserver. By default the image is stored in the tmp directory of the server and deleted after the script runs. Thats why we moved it so it can be saved in the file system. You can of course rename the file using rename() and if you know GD you can resize the image to.

ManzZup 9Aug2010 20:34

Re: image upload
 
niz one
thnkxx for he mechanism of selecting the file format, i didnt know that :D

johnny.dacu 9Aug2010 21:49

Re: image upload
 
So... now i found something useful... if a want to get last 3 chars from a string i can use negative values: substr($img_name,0,-3).
One more thing. what you do if you have .jpeg extention? there are 4 letters.

pein87 10Aug2010 04:56

Re: image upload
 
you can make an exception for that, generally most jpeg files are named .jpg but you can make a special exception for .jpeg.

PHP Code:

else if (substr($img_name,0,-4) == "jpeg"$img_exts) && $img_size $max_size


move_uploaded_file($img_temp_loc,"/images" $img_name); 



The above generally should be the first if statement because of the program logical flow, you normally start with the odd ball first because in this case its first going to check if the files extension is in the array thus iterating through the arrays values and then if its not there then check if its jpeg. The full notion would be

PHP Code:

<?php 

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types 
$max_size 40000// max file size in bytes 

$img_size $_FILE["img"]["size"]; // returns the files size in bytes 

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here 

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion 

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image 

$img_err $_FILE["img"]["error"

/* start the process of the validation */ 

if ($img_err 0

 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again."

else 


if (
substr($img_name,0,-4) == "jpeg"$img_exts) && $img_size $max_size


move_uploaded_file($img_temp_loc,"/images" $img_name); 

}
else if (
in_array(substr($img_name,0,-3), $img_exts) && $img_size $max_size


move_uploaded_file($img_temp_loc,"/images" $img_name); 



else 


echo 
"either your image was to big or the file was not a valid image files please correct this and try again."





?>

If you do you need to add an else if statement for that with the code to process it or do a conversion based on if its .jpeg to .jpg when your saving the file. Also I dont recommend using negatives in substr() because it may or may not work properly the best case would be to use:

PHP Code:

substr($text_var,strlen($text_var) - 3,strlen($text_var)); 

We use strlen() because it gives the length in numbers per character including white space of the string. In this case subtracting the number of points we wanted to from the end of the file and making it our start number and then using the total length as the end number gives us the same result but with an almost 100% guarantee to work properly over using negatives.

version using strlen()

PHP Code:

<?php 

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types 
$max_size 40000// max file size in bytes 

$img_size $_FILE["img"]["size"]; // returns the files size in bytes 

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here 

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion 

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image 

$img_err $_FILE["img"]["error"

/* start the process of the validation */ 

if ($img_err 0

 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again."

else 


if (
substr($img_name,strlen($img_name) - 4,strlen($img_name)) == "jpeg"$img_exts) && $img_size $max_size


move_uploaded_file($img_temp_loc,"/images" $img_name); 

}
else if (
in_array(substr($img_name,strlen($img_name) - 3,strlen($img_name)), $img_exts) && $img_size $max_size


move_uploaded_file($img_temp_loc,"/images" $img_name); 



else 


echo 
"either your image was to big or the file was not a valid image files please correct this and try again."





?>


pein87 10Aug2010 05:04

Re: image upload
 
the
PHP Code:

else if (substr($img_name,0,-4) == "jpeg"$img_exts) && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  



should be
PHP Code:

else if (substr($img_name,0,-4) == "jpeg" && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  



and the full versions should be the below, I would have edited them in but I cant edit my posts.

none strlen

PHP Code:

<?php  

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types  
$max_size 40000// max file size in bytes  

$img_size $_FILE["img"]["size"]; // returns the files size in bytes  

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here  

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion  

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image  

$img_err $_FILE["img"]["error"]  

/* start the process of the validation */  

if ($img_err 0)  
{  
 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again.";  
}  
else  
{  

if (
substr($img_name,0,-4) == "jpeg" && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  


else if (
in_array(substr($img_name,0,-3), $img_exts) && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  

}  

else  
{  

echo 
"either your image was to big or the file was not a valid image files please correct this and try again.";  

}  

}  

?>

strlen version

PHP Code:

<?php  

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types  
$max_size 40000// max file size in bytes  

$img_size $_FILE["img"]["size"]; // returns the files size in bytes  

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here  

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion  

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image  

$img_err $_FILE["img"]["error"]  

/* start the process of the validation */  

if ($img_err 0)  
{  
 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again.";  
}  
else  
{  

if (
substr($img_name,strlen($img_name) - 4,strlen($img_name)) == "jpeg" && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  


else if (
in_array(substr($img_name,strlen($img_name) - 3,strlen($img_name)), $img_exts) && $img_size $max_size)  
{  

move_uploaded_file($img_temp_loc,"/images" $img_name);  

}  

else  
{  

echo 
"either your image was to big or the file was not a valid image files please correct this and try again.";  

}  

}  

?>

I was doing some copy and pasting and forgot to remove the in_array() stuff from the jpeg search sorry about that. Now its done and save memory and processes if the file is a .jpeg instead of searching the arrays values first it does a simpler call to substr(). Ta-Da.


All times are GMT +5.5. The time now is 00:26.