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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.