Page 1 of 2

Read contents of local file?

Posted: Thu Jul 07, 2005 2:17 pm
by IceMetalPunk
I was wondering if it was possible to read the contents of a local file (a file on the user's computer) in PHP. I am trying to make an uploader, but to do this, I need to read the contents of the file the user chose and then write that to a file on the server. I used fopen and I tried using as an argument the entire local path, just the filename, and "file:\\" with the local path after it. none worked (the errors all said it was still looking on the server of the file).

Is there a different function for this, or am I just doing something wrong?

-IMP ;) :)

Posted: Thu Jul 07, 2005 2:36 pm
by hawleyjr
The only way you can read a file on the user's computer is if the user uploads the file. When they do that. use $_FILES to access the file.

Posted: Thu Jul 07, 2005 3:24 pm
by IceMetalPunk
Well I'm trying to make an uploader. So you mean if I have a form in an HTML page with a file type input names filep(<input type="file" name="filep">) and I submit the form to my PHP file online, I can use $_FILES['filep'] will get that for me and I can use fopen to open it?

-IMP ;) :)

Posted: Thu Jul 07, 2005 3:25 pm
by hawleyjr
Yeah give it a try. When you upload the file(s) use the following code to view the contents of the variable $_FILES:

Code: Select all

echo '<HR><PRE>'; print_r($_FILES); echo '</PRE>';

Posted: Thu Jul 07, 2005 3:35 pm
by theda
hawleyjr wrote:Yeah give it a try. When you upload the file(s) use the following code to view the contents of the variable $_FILES:

Code: Select all

echo '<HR><PRE>'; print_r($_FILES); echo '</PRE>';
In XHTML1.0, doesn't the HR have to be closed?

Posted: Thu Jul 07, 2005 3:38 pm
by IceMetalPunk
OK, thanks, I'll try it. But while we're on the subject of files, is there a way to delete a file off the current server? I have looked for something like fdelete or file_delete, or similar functinos, but I can't find them. I need this so that when a file is no longer needed, I can delete it to save server space.

-IMP ;) :)

BTW, I am working on all of these things for a forums community that I have. I am trying to make a money system so members can buy/sell/upload anything they want automatically. I think I know just how to do it, but since I'm new to PHP, I'm running into some bumps in the road, so to speak.

*EDIT* BTW, your code didn't work. It printed this:

Code: Select all

Array
(
)
Huh?

Posted: Thu Jul 07, 2005 3:39 pm
by hawleyjr

Posted: Thu Jul 07, 2005 3:42 pm
by IceMetalPunk
OK, thank you for that code. But please read my edit and maybe you (or someone else) can help me?

-IMP ;) :)

Posted: Thu Jul 07, 2005 3:43 pm
by hawleyjr
theda wrote:
hawleyjr wrote:Yeah give it a try. When you upload the file(s) use the following code to view the contents of the variable $_FILES:

Code: Select all

echo '<HR><PRE>'; print_r($_FILES); echo '</PRE>';
In XHTML1.0, doesn't the HR have to be closed?
I'm not sure how you got on the subject of XHTML...

<hr /> would be the standard for XHTML...

Posted: Thu Jul 07, 2005 3:44 pm
by hawleyjr
If the code suplied returned a blank array then no files were uploaded. What does the HTML form action look like?

Posted: Thu Jul 07, 2005 3:46 pm
by IceMetalPunk
Here;s all I have in the HTML file (I was using it to test this):

Code: Select all

<form name=&quote;myform&quote; method=&quote;get&quote; action=&quote;http://home.ripway.com/2005-6/328190/uploadtest.php&quote;>
<input type=&quote;file&quote; name=&quote;fnamep&quote;>
<input type=&quote;submit&quote;>
The URL is a valid URL to the PHP file with the code you gave me on Ripway.com, which allows PHP.

-IMP ;) :)

Posted: Thu Jul 07, 2005 3:48 pm
by hawleyjr
I meant enctype not action. Use the following: enctype in your form:

Code: Select all

<form action=&quote;gohere.php&quote; method=&quote;post&quote; enctype=&quote;application/x-www-form-urlencoded&quote; name=&quote;form1&quote;>

Posted: Thu Jul 07, 2005 3:51 pm
by IceMetalPunk
Nope, still I get a blank array. Here's the code now:

Code: Select all

<form name=&quote;myform&quote; method=&quote;post&quote; action=&quote;http://home.ripway.com/2005-6/328190/uploadtest.php&quote; enctype=&quote;application/x-www-form-urlencoded&quote;>
<input type=&quote;file&quote; name=&quote;fnamep&quote;>
<input type=&quote;submit&quote;>
-IMP ;) :)

Posted: Thu Jul 07, 2005 3:52 pm
by hawleyjr
I'm an idiot. Sorry use:

Code: Select all

enctype=&quote;multipart/form-data&quote;

Posted: Thu Jul 07, 2005 4:01 pm
by IceMetalPunk
Nope, still blank...?

-IMP ;) :)