spires Posted December 18, 2008 Share Posted December 18, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/ Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 You can use $_SESSION to do that, or set the $filename as a global. http://us3.php.net/manual/en/language.variables.scope.php for more details on the global part. Quote Link to comment https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/#findComment-718943 Share on other sites More sharing options...
.josh Posted December 18, 2008 Share Posted December 18, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/#findComment-718946 Share on other sites More sharing options...
spires Posted December 18, 2008 Author Share Posted December 18, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/#findComment-718949 Share on other sites More sharing options...
Maq Posted December 18, 2008 Share Posted December 18, 2008 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 . " "; Quote Link to comment https://forums.phpfreaks.com/topic/137558-passing-a-variable-outside-a-function/#findComment-718957 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.