Jump to content

Convert to function


Julian

Recommended Posts

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;

}}

Link to comment
https://forums.phpfreaks.com/topic/58782-convert-to-function/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/58782-convert-to-function/#findComment-291656
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.