Jump to content

Recommended Posts

Hi Guys.

 

I have created a function that allows me to upload files to my server.

However, I need to pass the file name to outside of the function, how is this done.

 

Thanks

 

function details_image1_upload($Iname){
if ($_FILES['image']['name']==''){

}else{

     $filedir = 'images/details/1/'; // the directory for the original image

     $maxfile = '1000000'; // max file size
     $mode = octdec('0666'); // octdec -- Octal to decimal.
 						// The mode parameter consists of three octal number components specifying access restrictions for the owner,
						// the user group in which the owner is in, and to everybody else in this order. e.g (666)
      
     $userfile_name = $_FILES['image']['name']; 
     $userfile_tmp = $_FILES['image']['tmp_name']; 
     $userfile_size = $_FILES['image']['size']; 
     $userfile_type = $_FILES['image']['type'];
    $userfile_error = $_FILES['image']['error'];

	 $Itype = 'jpg';
		$strName =  str_replace(' ', '_', $Iname);
	 $Dimg1 = $strName.".".$Itype;

 	// $prod_img = the image folder and the image and name
         $prod_img = $filedir.$Dimg1; 


	 //move_uploaded_file -- Moves an uploaded file to a new location.
	 //move the uploaded file to the image folder with a tempory name.
         if(move_uploaded_file($userfile_tmp, $prod_img)){
        $in =  "Thank You, Your File Has Been Uploaded";
         }
         else{
        $out =  "Sorry, Your File Failed To Upload";
         }
	 // chmod -- Changes file mode, 
	 // set the user interface for the image
         chmod ($prod_img, $mode); 
     
				global $Dimg1;
				} 
}


echo $Dimg1;

Link to comment
https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/
Share on other sites

I do not recommend making variables global inside an array.  Defeats the purpose of having scope. 

 

It depends on what you mean by "passing the file name outside the function".

 

Are you calling another function inside the function? Just pass it as an argument in the function call.  Are you wanting to have this file name after the function is called, and its done it's business? Just return the file name.

 

function blah() {
  $filename = "somefile.php";
  return $filename;
}

$filename = blah();

echo $filename; // output: somefile.php

Hi Guys,

 

Thanks for your help.

 

This is what I did

// Call Main Image Upload
details_image1_upload($_POST['Iname1']);
// Call 2nd Image Upload
details_image2_upload($_POST['Iname2'])	;


$Dimg1 = details_image1_upload($_POST['Iname1']);
$Dimg2 = details_image2_upload($_POST['Iname2']);

 

However, how do I get three variable that were created inside a function, to be used in the rest of my script?

 

Thanks

Return an array instead.

 

function blah() {
  $filename[] = "somefile1.php";
  $filename[] = "somefile2.php";
  $filename[] = "somefile3.php";

  return $filename;
}

$filename = blah();

foreach($filename as $key)
   echo $key . "
";

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.