move_file_uploaded() can´t read a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

move_file_uploaded() can´t read a variable

Post by Master_Phantom »

Hi,

I created a Flash script that allows to upload a file to a dynamic folder.
The script can upload the files but in the same folder as the SWF file :?

I managed to do the following:

I created an Array with the 12 months of the year, when I choose a Month I use this script:

Code: Select all

<?php
 
$month = $_POST['month'];
$finalMonth = '"'.$month.'"';
$monthHandler = fopen("variable.txt", "w");
@fwrite($monthHandler, $finalMonth);
 
?>
It creates the variable.txt file with the following inside: "/November/"

Everything is fine until here with no problems, because I got the String that I needed. 8)

Now here comes my nightmare, the following is the code that uploads the file (Note that it works but it doesn´t move the file to the desired folder ):

Code: Select all

<?php
 
$file = fopen("variable.txt","r");
$destino = fread($file,filesize("variable.txt"));
move_uploaded_file($_FILES['Filedata']['tmp_name'], $destino.$_FILES['Filedata']['name']);
echo($destino);
fclose($file);
 
?>
The problem is that the function move_uploaded_file() doesn´t want to read the variable $destino. I know that the variable is passed because the echo shows me the full content of the file variable.txt.

So I want that the function use that variable to move to the desired folder.

Tell me if you want more explanation.

Thx
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: move_file_uploaded() can´t read a variable

Post by susrisha »

have you created the folders with the names "/November/" ? i mean it cant upload into a directory which doesnot exist. Check for the error log of your apache to see whats wrong with moving the uploaded file.

I see it as a path problem.
The other might be the size problem. If your file size exceeds the size that php can handle(defined in php.ini file), then it cannot move the uploaded file.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: move_file_uploaded() can´t read a variable

Post by Benjamin »

Master_Phantom wrote:I know that the variable is passed because the echo shows me the full content of the file variable.txt.
So you are trying to use the full contents of the file variable.txt as a valid path?
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Re: move_file_uploaded() can´t read a variable

Post by Master_Phantom »

Hi,

I will tell what the full script does, first it´s a registration form that when it´s successfull completed it creates the folders user/January, user/February, user/March, etc.

Now the other half is an admin area where I can select the user and upload a file to his/her specific folder.

For example:

I choose the user "johnsmith" and I select November, so when I upload the file, it will upload to /johnsmith/november/

The problem is that the variable /johnsmith/november/ isn´t passed to the script so it uploads in the same folder as the script.

I managed to do a "Confirm" button that saves that variable to a empty txt file. So the upload script reads the path from that file instead of the Flash form.

The most strange is that using echo it shows me exactly the string that I need, but it doesn´t use it.

I need a way to read that variable.

I´m using the well known WAMP that starts with the letter X.

Thx
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: move_file_uploaded() can´t read a variable

Post by Ziq »

Can you write that you have in output?

Code: Select all

 
var_dump($destino.$_FILES['Filedata']['name']);
 
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Re: move_file_uploaded() can´t read a variable

Post by Master_Phantom »

Ziq wrote:Can you write that you have in output?

Code: Select all

 
var_dump($destino.$_FILES['Filedata']['name']);
 
"/johnkapri/November/"string(30) ""/johnkapri/November/""
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: move_file_uploaded() can´t read a variable

Post by Ziq »

Are you sure?

Code: Select all

var_dump($destino.$_FILES['Filedata']['name']);
This must output something like this

Code: Select all

 
string(28) "/johnkapri/November/file.ext"
 
Why are you using " (quote)?
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: move_file_uploaded() can´t read a variable

Post by Ziq »

Also you should understand that if you are using "/" it means that you have folder johnkapri in a root of file system. Not in the same folder with this file!

Use

Code: Select all

 
./some/path/to/file/file.ext
 
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Re: move_file_uploaded() can´t read a variable

Post by Master_Phantom »

Well, I don´t know what happens, I´m going to try to rename the file after upload, maybe it works, I appreciate all the help.

If someone is expert in Flash, I´m using fileReference.upload()

I have a final question, does move_uploaded_file() accepts _POST ?

Well, If I succeed in this I´m going to release this script as opensource. :D

Thx a million to everyone

------------------------------------------------------------------------------

Update: Yeah!!!! I fixed it. :drunk:

So when the script is ready (2 days) I will ask a moderator to upload to the snippets board.

The solution was this:

I stored a variable in a txt file with quotes = "/johnkapri/November/" that´s wrong, it need to be like this ./johnkapri/November/ and the upload file like this:

Code: Select all

<?php
 
$file = fopen("variable.txt","r");
$destino = fread($file,filesize("variable.txt"));
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
rename("./files/".$_FILES['Filedata']['name'], $destino.$_FILES['Filedata']['name']);
fclose($file);
 
?>
f

See ya!!! and thx again.
Post Reply