Hello everyone,

I'm in the process of learning php in a class and had a quick question for one of my assignments. I am uploading a file to a server which the user can name the directory. However, after they upload the file, I have to give them the option to either rename the file or delete it. I'm having a hard time accessing the original file name on the page that I link to rename the file. I tried using the super global files thinking that it doesn't get wiped after you leave the first page, but its not working.

rename ($_FILES[userfile][name],$newName);

I've also tried using a session to remember the the filename. userfile_name is a variable. In my upload page, I do this in the very beginning:



$userfile_name = $_FILES[userfile][name];

Then on the rename page, I do this at the very beginning:

$user_file = $_SESSION[userfile_name];


rename ($user_file,$newName);

But in the response page, I get a warning with rename that shows only the second parameter, which is a local variable on the page that the user enters what they want to rename the file to, makes it. The first parameter, original file name that I tried to save with a session didn't.

Any thoughts?

Thanks a bunch. If I can get this working I can use the same procedure for delete.

Edited by Trekker182: n/a

8 Years
Discussion Span
Last Post by chrishea

I got it working with the sessions however for some reason as soon as the page loads its executes what is listed under the else statement below:

	if ($_POST[action] == "change") {
	$newName = $_POST[newName];
	rename ($userfile_name,$newName); 
	echo "The file has been renamed.";
	}  else {
	echo "The file rename failed.";

I tried taking away the brackets because it wasn't multiple lines of code and it still displays 'The file rename failed.' as soon as the page loads. I also tried putting it inside the if's brackets and got a syntax error. I could just take it away but I like to have a message in case it doesn't work. After I enter my new filename to rename the original too and click change, I get the correct message of 'The file has been renamed.'

Any ideas?



You need to do a bit of debugging. You need to know what the value of $_POST[action] is when it goes to the ELSE. You can echo the value so you'll know.

If this is a fragment of your code and if you have a form that drives this code (in this same module) and sets the POST values and if you execute the code that you've shown above every time (ie before your Post values are set), then you will go to the Else routine the very first time and get your error message.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.