Computing.Net > Forums > Web Development > html input type=file and php

html input type=file and php

Reply to Message Icon

Original Message
Name: eaw8806
Date: March 28, 2004 at 09:16:58 Pacific
Subject: html input type=file and php
OS: win 98
CPU/Ram: pentiumII/128MB
Comment:

recently i've been working on this blogging application, and i wanted a way to allow users to upload pictures to the server, when they are logged in, to a folder. i have 2 problems. i know i should use the input type=file tag, and i think in the form tag, enctype=multipart/form-data method=post is what i need to do, but im not quite clear on how to get the file contents, from the server end in php. if i dont define the enctype, and say input type=file name=fileimg , then in the php, $HTTP_POST_VARS["fileimg"]; will be the path of the file the user selects, but when i do the enctype, that var is blank. is there a different var, or array that it is being written to? im stuck as to where to go on this.

the second problem i have, is creating files. using filezilla, i created a folder, with the properties of everything (777), and used fopen(); fputs() fclose();, but the file is not created. i think this may have to do with the file permissions, but as i said, i set the folder to 777, so i do not think this should be an issue.

any help on either of these problems is appriciated. thanks,
eaw8806


Report Offensive Message For Removal


Response Number 1
Name: anonproxy
Date: March 28, 2004 at 10:03:55 Pacific
Reply: (edit)

You are correct to use Input="file" and Enctype="multipart/form-data". You seem to understand the file is uploaded via a POST transaction. Therefore, it automatically becomes an element, named by the HTML form's value="" attribute, in the superglobal $_FILES (PHP 4.1.x and above). Consider this superglobal like an object which inherits several methods such as tmp_name, name, size, and type.

The only user which needs 777 permissions in the folder is the apache server (remember the server is the proxy for all user access). See if 711, 755, or 644 works (we are only speaking of the applicable directory).

Good documentation at Zend.com and PHP.net. A permissions reference is here.



Report Offensive Follow Up For Removal

Response Number 2
Name: eaw8806
Date: March 28, 2004 at 15:03:06 Pacific
Reply: (edit)

i think i understand what $_FILES is...its kinda like $_POST and $_SESSION....so do i use $_FILES["imgfile"] like its an array? where imgfile is the name of the form? or the input? hmm...well i'll just experiment with it.

i'll try those other permision settings too...thanks for your help on both topics anonproxy.

-eaw8806


Report Offensive Follow Up For Removal

Response Number 3
Name: eaw8806
Date: March 28, 2004 at 15:28:09 Pacific
Reply: (edit)

yup, so i figured it out, i did
$imgfile = $_FILES["imgfile"];
foreach($imgfile as $key=>$value){
echo "imgfile[$key] = $value

";
}
and got the name, type, tmp_name, error and size. so i decided to experiment with tmp_name, and i opened it, and the file i uploaded was there! now i did run into another problem...i think the server is linix, so does that mean files end with a \n? because when i echo what i read from the file, all i get is the first line. i think theres a way to get around this, by specifing the length that is read. and since i have imgfile[size] i think it might work. thanks for your help again.

eaw8806.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: html input type=file and php

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge