Julian Posted July 6, 2007 Share Posted July 6, 2007 Hi Guys. I have this code to upload images to the server and then save the name on a database (this part of the code not included). This code works excellent if I want to upload one image, what I'm trying to do is convert this code to a function in order to upload as many images as I want. Thanks for help guys... always the best. if(isset($_POST['submit'])) { if($_FILES['userfile']['size'] > 1) { $info = getimagesize($_FILES['userfile']['tmp_name']); //check the extension. $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png")) die("<BR><BR>Error: la extension del archivo no es reconocida. Asegurese que este utilizando la imagen correcta (.JPG or .PNG)"); //CHECK TYPE: (what the browser sent) if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) { die("<BR><BR>Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas."); } //DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE! if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { die("<BR><BR>Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas."); } //rename file, move it to location. if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { $file_name = $_FILES['userfile']['name']; //create a random file name $rand_name = md5(time()); $rand_name .= rand(0,999999999); } //Subir el Archivo move_uploaded_file($_FILES['userfile']['tmp_name'] , $_SERVER['DOCUMENT_ROOT']."/admin/fotos/".$rand_name . '.' . $ext); $filename=$rand_name . '.' . $ext; }} Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 6, 2007 Share Posted July 6, 2007 im too lazy to type that again so just read this http://www.bytemycode.com/snippets/snippet/528/ Quote Link to comment Share on other sites More sharing options...
Julian Posted July 6, 2007 Author Share Posted July 6, 2007 Thanks teng84, but anybody have an idea instead of using that class, create my own to use this code? or adapt this one to be a class. Thanks again. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 6, 2007 Share Posted July 6, 2007 function upload($filename,$filetype='',$filesize,$filetemp,$foldername_or_dir) { // use the variables here to upload } usage upload($_FILES['userfile']['name'],'',$_FILES['userfile']['size'],$_FILES['userfile']['tmp_name'],'folder name',); now try that and give us the updated code and we'll help to debug if theres an error Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.