use fopen unknown resource

Started by ourdiasporaover 4 years ago8 messagesgeneral
Jump to latest
#1ourdiaspora
ourdiaspora@protonmail.com

Readers,

Objective, to import a csv file into postgresql database. If understood correctly, the manual section 'fopen' (https://www.php.net/manual/en/function.fopen.php) shows that the file has a "known resource" extant name.

Please what is the syntax to assign an unknown file name to the 'fopen' function?

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: ourdiaspora (#1)
Re: use fopen unknown resource

On 8/27/21 2:59 PM, ourdiaspora wrote:

Readers,

Objective, to import a csv file into postgresql database. If understood correctly, the manual section 'fopen' (https://www.php.net/manual/en/function.fopen.php) shows that the file has a "known resource" extant name.

Please what is the syntax to assign an unknown file name to the 'fopen' function?

Why not use?:

https://www.php.net/manual/en/pdo.pgsqlcopyfromfile.php

--
Adrian Klaver
adrian.klaver@aklaver.com

#3ourdiaspora
ourdiaspora@protonmail.com
In reply to: Adrian Klaver (#2)
Re: use fopen unknown resource

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Friday, August 27th, 2021 at 11:10 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

https://www.php.net/manual/en/pdo.pgsqlcopyfromfile.php

"
public PDO::pgsqlCopyFromFile(
string $table_name,
string $filename,
"

Sorry but do not understand; the line does not explain what to write in the php file.

So far have written:
"
<?php
$userfile=file(./
$dbusertemporary = pg_connect("dbname=cpacweb user=cpaca");
$usertemporarydata = pg_copy_from (
resource $dbusertemporary,
string $userdata,
array $rows,
string $delimiter = ',',
string $null_as = '\\N'
): array;
"

The plan is to write an html file for a user to select a csv file to import into a database. The manual suggests that the file name is already known (e.g. https://www.php.net/manual/en/function.fgetcsv.php)

Instead of:
"
...
fopen("test.csv", "r"))
...
"

it would _not_ be possible to write, correct?:
"
...
fopen("", "r"))

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: ourdiaspora (#3)
Re: use fopen unknown resource

On 8/27/21 3:50 PM, ourdiaspora wrote:

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Friday, August 27th, 2021 at 11:10 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

https://www.php.net/manual/en/pdo.pgsqlcopyfromfile.php

"
public PDO::pgsqlCopyFromFile(
string $table_name,
string $filename,
"

Sorry but do not understand; the line does not explain what to write in the php file.

I'm not a PHP programmer, but I'm going to say it is filename as
described here:

https://www.php.net/manual/en/function.fopen.php

" filename

If filename is of the form ...
"

The plan is to write an html file for a user to select a csv file to import into a database. The manual suggests that the file name is already known (e.g. https://www.php.net/manual/en/function.fgetcsv.php)

You are asking the user to select a file, so there should be some sort
of file reference at that point, correct?

Same for below.

Instead of:
"
...
fopen("test.csv", "r"))
...
"

it would _not_ be possible to write, correct?:
"
...
fopen("", "r"))

--
Adrian Klaver
adrian.klaver@aklaver.com

#5Peter J. Holzer
hjp-pgsql@hjp.at
In reply to: Adrian Klaver (#4)
Re: use fopen unknown resource

On 2021-08-28 09:52:45 -0700, Adrian Klaver wrote:

On 8/27/21 3:50 PM, ourdiaspora wrote:

On Friday, August 27th, 2021 at 11:10 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

https://www.php.net/manual/en/pdo.pgsqlcopyfromfile.php

"
public PDO::pgsqlCopyFromFile(
string $table_name,
string $filename,
"

Sorry but do not understand; the line does not explain what to write in the php file.

I'm not a PHP programmer, but I'm going to say it is filename as described
here:

The use of PHP suggests that this is a web application. So most likely
the user is supposed to upload the file via an HTML form. I would
suggest to the OP to learn how to process file uploads in PHP. Once
they've figured that out it will probably be clear what file name to
use.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

#6ourdiaspora
ourdiaspora@protonmail.com
In reply to: Adrian Klaver (#4)
Re: use fopen unknown resource

On Saturday, August 28th, 2021 at 5:52 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

You are asking the user to select a file, so there should be some sort

of file reference at that point, correct?

This is what causes confusion. If a user has a file named 'mydatafile.csv', why does the manual make reference to open 'test.csv'???:

https://www.php.net/manual/en/function.fgetcsv.php
"
...
if (($handle = fopen("test.csv", "r")) !== FALSE) {
...
"

#7David G. Johnston
david.g.johnston@gmail.com
In reply to: ourdiaspora (#1)
Re: use fopen unknown resource

On Fri, Aug 27, 2021 at 2:59 PM ourdiaspora <ourdiaspora@protonmail.com>
wrote:

Please what is the syntax to assign an unknown file name to the 'fopen'
function?

There isn't one as it would make no sense - how is it supposed to open a
file if there is no filename provided to open?

You have to set things up yourself so that the content you want to open
exists in a file at a known location and then pass that location to the
function. In particular, in a web server context, that means taking the
request input from the client that represents a file and saving it
somewhere first - then opening that saved content. If the user is
supplying a suggested file name for the content you can incorporate that.
You can also ignore it and just make something up. It doesn't matter
(other than concerns about overwriting existing files). But whatever name
you choose to give to the newly created file on the server is the one you
pass to fopen. The name/location of the file on the client machine is
irrelevant - the server cannot even see that machine (or, at least should
not care even if it technically can) per the fundamental architectural
principle of "client-server" design.

David J.

#8Adrian Klaver
adrian.klaver@aklaver.com
In reply to: ourdiaspora (#6)
Re: use fopen unknown resource

On 8/28/21 2:55 PM, ourdiaspora wrote:

On Saturday, August 28th, 2021 at 5:52 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

You are asking the user to select a file, so there should be some sort

of file reference at that point, correct?

This is what causes confusion. If a user has a file named 'mydatafile.csv', why does the manual make reference to open 'test.csv'???:

Because that is a 'dummy' file name for the purposes of illustrating
what sort of information needs to be provided. Just substitute in
whatever file name is actually being fetched in the production code.

https://www.php.net/manual/en/function.fgetcsv.php
"
...
if (($handle = fopen("test.csv", "r")) !== FALSE) {
...
"

--
Adrian Klaver
adrian.klaver@aklaver.com