Read contents of local file?

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

User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Read contents of local file?

Post 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 ;) :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>';
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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?
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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?
Last edited by IceMetalPunk on Thu Jul 07, 2005 3:40 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

OK, thank you for that code. But please read my edit and maybe you (or someone else) can help me?

-IMP ;) :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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...
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

If the code suplied returned a blank array then no files were uploaded. What does the HTML form action look like?
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;>
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I'm an idiot. Sorry use:

Code: Select all

enctype=&quote;multipart/form-data&quote;
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Nope, still blank...?

-IMP ;) :)
Post Reply