Jump to content

Recommended Posts

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.

 

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.

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!?

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!

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.