tmyonline Posted August 4, 2007 Share Posted August 4, 2007 Guys: I asked this question before but I guess I was not clear enough. I'd like to ask again if you don't mind. Basically, I have the following piece of code in my "upload.php": if (array_key_exists('submit', $_POST)) define('UPLOAD_DIR', '/comments/'); $file_renamed = time().$_FILES['upload']['name']; move_uploaded_file($_FILES['upload']['tmp_name'], UPLOAD_DIR.$file_renamed); } Here's the question: In my index.php page, I included the "upload.php" page (i.e., the above code) so that I can use the variable "$file_renamed" (to create a link to the uploaded file). In short, I want to create a link like this: <h3><a href="/comments/"<?php echo $file_renamed; ?>>Draft</a></h3> But, for some reason, the variable "$file_renamed" does not seem to preserve its value; it's somehow lost after the uploading process, which works fine. Any idea? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/ Share on other sites More sharing options...
ballhogjoni Posted August 4, 2007 Share Posted August 4, 2007 have you tried sessions? Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315744 Share on other sites More sharing options...
BlueSkyIS Posted August 4, 2007 Share Posted August 4, 2007 can you post code? Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315748 Share on other sites More sharing options...
mrjcfreak Posted August 4, 2007 Share Posted August 4, 2007 Are you trying to preserve the value of a variable between pages (i.e. seperate mouse clicks or form submittals) or between different source files (i.e. for one action you need code from a variety of php files) If it's the former, then tmyonline is correct, you need sessions: if you use $_SESSION['file_renamed'] = $file_renamed (and of course put session_start() on the first line of the first php file)- once you set this variable you will be able to go back to it if the same user accesses php files on the same domain and path... if you need variable to persist simply between php files when you need more than one php souce file for an action- well, this is natural behaviour, when you use include and require it is as though you are joining the php files seamlessly- there should be no difference in functions or variables. Otherwise there would be no point. I think the problem may be 'variable scope'- if you define a variable outside a function, then inside that function, the variable will have a different value. To use a global variable, then first of all define your variable outside of any functions; e.g. with a simply $file_renamed = ''; and then to access and manipulate this variable inside functions, add the line global $file_renamed at the top of your function. The manual explains it pretty well It also explains sessions too. Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315756 Share on other sites More sharing options...
tmyonline Posted August 4, 2007 Author Share Posted August 4, 2007 mrjcfreak: Thanks! but unfortunately, it doesn't work. I did as you said; I used "$_SESSION['file_renamed'] = $file_renamed" in my upload.php page, like this: if (array_key_exists('submit', $_POST)) { define('UPLOAD_DIR', '/comments/'); $file = str_replace(' ', '_', $_FILES['upload']['name']); $file_renamed = $file; $_SESSION['file_renamed'] = $file_renamed; move_uploaded_file($_FILES['upload']['tmp_name'], UPLOAD_DIR.$_SESSION['file_renamed']); } And, in my index.php page, I put on the first line: <?php session_start(); ?> Down below, I created the link: <h3><a href="/comments/"<?php echo $_SESSION['file_renamed']; ?>>Draft</a></h3> It didn't work. I then restored back my original code, which had been working, my code didn't work this time. Hymn,... what's going on!? Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315788 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Look at the source code of your HTML... what is being generated? Session variables would absolutely be in scope. Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315794 Share on other sites More sharing options...
tmyonline Posted August 5, 2007 Author Share Posted August 5, 2007 I think I get the idea. Ok, so $_SESSION is the thing. I'll do some reading tonight. In the meantime, I welcome any new suggestions and ideas. Thanks a lot guys. Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-315797 Share on other sites More sharing options...
tmyonline Posted August 5, 2007 Author Share Posted August 5, 2007 Guys, I've done some reading. Yes, I understand how session works. Still, my code doesn't work! In my upload_code.php page, this is what I have: session_start(); if (array_key_exists('submit', $_POST)) { define('UPLOAD_DIR', '/comments/'); $file = str_replace(' ', '_', $_FILES['upload']['name']); $_SESSION['file_upload'] = time().$file; move_uploaded_file($_FILES['upload']['tmp_name'], UPLOAD_DIR.$_SESSION['file_upload']); } In other page, test.php, I have: <?php session_start(); echo $_SESSION['file1_upload']; echo $_SESSION['file2_upload']; ?> When I loaded my test.php page to the browser, I did not see any output from echo; I expect to see the name of the files I uploaded (I think my cookie worked fine). Any ideas ? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-316112 Share on other sites More sharing options...
tmyonline Posted August 5, 2007 Author Share Posted August 5, 2007 Excuse me! I made a mistake when copying and pasting the code: In my test.php page, this is what I have: <?php session_start(); echo $_SESSION['file_upload']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-316114 Share on other sites More sharing options...
tmyonline Posted August 5, 2007 Author Share Posted August 5, 2007 I got it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63357-how-to-preserve-the-value-of-a-variable-in-php/#findComment-316149 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.