Page 1 of 1

uploading files and file info

Posted: Tue Aug 27, 2002 10:55 am
by jabbamonkey
I created a webform that allows a user to choose a file from his computer, and send it. For some reason, the script I created to handle this works fine on one server, but not on another... I notice that when I use the script on the new server, the uploaded file is never placed into a temporary folder, and important information is not available ($file_type, $file_name)

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 form submission page.

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

Posted: Tue Aug 27, 2002 11:00 am
by volka

Posted: Tue Aug 27, 2002 12:29 pm
by jabbamonkey
That's not the problem. The webform I have set up works fine (it is transfering the variables from page to page), it's just the file upload that isn't working.

However, I did try it anyway (I had nothing else to do)...

Code: Select all

<input type="file" size=40 name="userfile">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 

<?php 

$_POST&#1111;'userfile'];
$_POST&#1111;'MAX_FILE_SIZE']; 

?>

<input type="Submit" name="Submit" value="Submit Form" class="textfield">
... and as I mentioned, this transfers the variable as "C:/windows/desktop/filename.jpg" to the next page ... not the temporary file on the server.

Once again, isn't the uploaded file supposed to be temporarily placed on the server, and the variable tranfered ($userfile) should be where this file is on the server, NOT on the users computer .... correct?

Jabbamonkey

Posted: Tue Aug 27, 2002 3:18 pm
by Takuma
Can I see the code please?

Posted: Tue Aug 27, 2002 4:29 pm
by jabbamonkey
I got it. When I transfered the code from one page to another, I never adapted the form page.... and forgot the enctype...

<form method=post action="research/grantapply_confirm.php" enctype="multipart/form-data">

Posted: Thu Aug 29, 2002 3:09 am
by noguru
jabba, would you mind sharing the script that does the uploading? I'm also trying to upload files to the server, but got no idea what components or functions to use.

Thanks

Posted: Thu Aug 29, 2002 9:48 am
by jabbamonkey
Your first page should consist of the form field (make sure you dont forget the ENCTYPE) ...

Code: Select all

<form action="uploadfile2.php" method="post" ENCTYPE="multipart/form-data"> 
<input type="file" size=40 name="file"><br> 
<input type="hidden" name="MAX_FILE_SIZE" value="100000"> 
<input type="submit" value="upload"> 
</form>
The second page will take the file from the form ($file) and perform the actions (upload the file)...

Code: Select all

$pathonserver = "/usr/home/a1234567/html/";
// Web Root Directory - not the url. Your web host
// should have this information for you.

$folder = "images/";
// Folder to put the file

$fileonserver = $pathonserver.$folder.$file_name;
// Tells where to upload the file, and the file name.
// $file_name is given (if $file exists, $file_name is created 
// automatically - you wont need to declare it yourself)

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

Posted: Fri Aug 30, 2002 5:35 am
by noguru
Thanks jabba,

I've actually managed to use fopen, fread and fputs to upload files to the server, but the problem is permission. When I chmod the directory to o+w on the unix command line, it works, but when I remove write access, I get "permission denied". I also tried to use the php function chmod(<directoryname>,0646); just to give write permission to the directory, but then I get "not owner". I have managed to change existing files' permissions this way, but I can't change permissions for files that do not exist yet and neither for directories.

Is there a way to specify the owner's username & password in php before I try to do a chmod or is there another way to write to a write-protected directory?

I hope I make sense...