PDA

View Full Version : file_name and file_type


Jabbamonkey
08-22-2002, 04:10 AM
I'm using a webform to upload a file. After the user selects the file ($file), they are sent to the next page..... I thought the selected file ($file) would automatically assign a file name ($file_name) and file type ($file_type).

When I perform the following code, the page prints out the variable $file, but not $file_name or $file_type. If these do not automatically exist, how do I designate these?



$pathonserver = "D:\\ISERVER\\USER\\temp\\";
$fileonserver = $pathonserver."".$file_name;

echo "<br><br>File: ".$file;
echo "<br><br>File Name: ".$file_name;
echo "<br><br>File Type: ".$file_type."<br><br>";

// UPLOAD FILE

copy($file, $fileonserver);
unlink($file);

mordred
08-22-2002, 04:02 PM
The code above has badly terminated strings, there are some double quotes amiss. I would be quite surprised if this code does run properly.

Anyway, try to see which data was set for your uploaded file by using

var_dump($_FILES);

or (if you are on an older PHP server)

var_dump($HTTP_POST_FILES);

stuntboy
08-22-2002, 04:34 PM
I would watch this line most specifically:

$pathonserver = "D:\ISERVER\USER\temp\";

\ are for excaping. you can get around it by escaping that slash like:

$pathonserver = "D:\\ISERVER\\USER\\temp\\";

or you can switch to single quotes. double quotes are really only needed if you need something parsed in the string.

this line:
$fileonserver = $pathonserver."".$file_name;

you do not need the ."". you can have simply
$fileonserver = $pathonserver.$file_name;

the other lines you can actually embed the variables right in the string since you are using double quotes:
echo "<br><br>File: $file";
echo "<br><br>File Name: $file_name";
echo "<br><br>File Type: $file_type<br><br>";

in php there is a difference in the single and double quotes. the double quotes have thier contents parsed and the single does not. Which means double quotes will cause slightly more overhead (though in your script there is so few no real difference will be there)


Hope this helps

Jabbamonkey
08-22-2002, 04:50 PM
I fixed the path line...

$pathonserver = "D:\\ISERVER\\USER\\temp\\";

But the file variables were still not showing up. $file exists, but $file_type and $file_name are still not there...

I tried var_dump($_FILES) and it printed the following...

string(35) "C:\\WINDOWS\\Desktop\\ba60582_b.jpg"

... but still no $file_type or $filename. Not sure what's going on...



Jabbamonkey

stuntboy
08-22-2002, 08:33 PM
instread try:

$_FILES['file']['name']
$_FILES['file']['type']

the bolded file change that to whatever you have the file input named

Jabbamonkey
08-22-2002, 10:19 PM
I tried the following....

if ($file)
{
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];

$pathonserver = "D:\\\\ISERVER\\\\123\\\temp\\\";
$fileonserver = $pathonserver.$file_name;

echo "File: ".$file;
echo "<br>File Name: $file_name";
echo "<br>File Type: $file_type";
}

... but only got this as a result...

File: C:\\WINDOWS\\Desktop\\ba60582_b.jpg
File Name:
File Type:

I don't know what's going on....
I also tried...

$_FILES['file']['name']; // without $file_name =
$_FILES['file']['type']; // without $file_type =


... and it didn't work.

Jabbamonkey
08-26-2002, 06:10 PM
Ok, I tried using the script on my own server (as opposed to using it on my other server). The script worked fine.

I did notice, however, that when I use the script on the new server, the uploaded file is never placed into a temporary folder...

For example, when $file is passed from the web form to the confirm page, the file should be placed in a temporary spot on the server (awaiting some action, etc.). On the old server, the file is moved from...

c:/windows/desktop/filename.jpg

to

/servername/user/home/tmp/123456name.jpg
("the root web directory for my server" / tmp)

So, $file = "/servername/user/home/tmp/123456name.jpg"

On the new server, the file is never moved to a temporary directory and $file still equals "c:/windows/desktop/filename.jpg"
Hence, this is probably why I am having trouble performing any actions and getting an info (file_name, file_type)

How do I make the file copy it temporarily? I thought this was automatic...

Jabbamonkey