Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14752 > unrolled thread

PHP CSV

Started byJoydeep Chakrabarty <chalao.adda@gmail.com>
First post2014-12-18 17:09 +0530
Last post2014-12-18 15:16 +0100
Articles 20 on this page of 63 — 13 participants

Back to article view | Back to comp.lang.php


Contents

  PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 17:09 +0530
    Re: PHP CSV Markus Heinz <markus.heinz@uni-dortmund.de> - 2014-12-18 13:11 +0100
      Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-18 13:27 +0100
        Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 18:26 +0530
          Re: PHP CSV Markus Heinz <markus.heinz@uni-dortmund.de> - 2014-12-18 14:43 +0100
            Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2014-12-18 09:24 -0500
            Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 20:56 +0530
              Re: PHP CSV Markus Heinz <markus.heinz@uni-dortmund.de> - 2014-12-18 16:57 +0100
                Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-18 17:04 +0100
                Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 21:52 +0530
                  Re: PHP CSV Markus Heinz <markus.heinz@uni-dortmund.de> - 2014-12-18 17:38 +0100
                    Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 16:49 +0000
                  Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2014-12-18 21:34 -0500
                    Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2014-12-19 09:43 +0000
                      Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-19 14:05 +0100
                        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2014-12-19 10:59 -0500
                          Re: PHP CSV Matthew Carter <m@ahungry.com> - 2014-12-19 14:21 -0500
                            Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2014-12-19 18:17 -0500
                            Re: PHP CSV "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-12-20 00:35 +0100
                          Re: PHP CSV Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-02-04 13:19 +0100
                            Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 08:03 -0500
                              Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2015-02-04 13:29 +0000
                                Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 09:06 -0500
                                  Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2015-02-04 15:26 +0000
                                    Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 10:40 -0500
                                      Re: PHP CSV Paul Herber <paul@pherber.com> - 2015-02-04 16:11 +0000
                                        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 11:23 -0500
                                          Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2015-02-04 17:08 +0000
                                            Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 12:48 -0500
                                      Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2015-02-04 17:05 +0000
                                        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 12:49 -0500
                                          Re: PHP CSV Tim Streater <timstreater@greenbee.net> - 2015-02-04 18:14 +0000
                                            Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 13:37 -0500
                                              Re: PHP CSV Matthew Carter <m@ahungry.com> - 2015-02-04 15:23 -0500
                                                Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-04 21:54 +0100
                                                  Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 16:32 -0500
                                                Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 16:31 -0500
                                  Re: PHP CSV Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-02-04 16:39 +0100
                                    Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 10:57 -0500
                                      Re: PHP CSV Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-02-04 18:07 +0100
                                        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-04 12:55 -0500
                                    Re: PHP CSV Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-06 23:26 +0100
                                      Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 01:38 +0100
                                        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-06 19:44 -0500
                                          Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 14:49 +0100
                                        Re: PHP CSV Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-07 14:28 +0100
                                      Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-06 19:42 -0500
                                        Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 04:33 +0100
                                          Re: PHP CSV Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-07 14:26 +0100
                                            Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 14:45 +0100
                                              Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-07 08:54 -0500
                                                Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 16:54 +0100
                                                  Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-07 11:00 -0500
                                          Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-07 08:51 -0500
                                            Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-07 14:55 +0100
          Re: PHP CSV Olaf Schmitt <thesys@gmx.de> - 2014-12-19 01:16 +0100
          Re: PHP CSV Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-19 02:14 +0000
      Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 18:13 +0530
        Re: PHP CSV Derek Turner <frderek@cesmail.net> - 2014-12-18 12:55 +0000
          Re: PHP CSV Joydeep Chakrabarty <chalao.adda@gmail.com> - 2014-12-18 18:33 +0530
            Re: PHP CSV Derek Turner <frderek@cesmail.net> - 2014-12-18 13:10 +0000
        Re: PHP CSV Jerry Stuckle <jstucklex@attglobal.net> - 2014-12-18 08:17 -0500
        Re: PHP CSV "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-18 15:16 +0100

Page 1 of 4  [1] 2 3 4  Next page →


#14752 — PHP CSV

FromJoydeep Chakrabarty <chalao.adda@gmail.com>
Date2014-12-18 17:09 +0530
SubjectPHP CSV
Message-ID<m6ueck$e7a$1@dont-email.me>
Hello,

I am trying to read a CSV file from my web server. Here is my code.

<?php
    $file = "http://localhost/x.csv";
    $fp = fopen ($file, "r");
    if (!$file) {
        echo "<p>Unable to open remote file.\n";
        exit;
    }
    while(!feof($fp)) {
        $data = fgetcsv($file, 1024);
        print "Data = " . $data[0] . "<BR>";
    }
    fclose($fp);
?>

It can access my csv file in my document root folder. But it gets into 
infinite loop and does not display anything from $data array. Is there 
something I am missing?
Please help.

Thanks.

[toc] | [next] | [standalone]


#14753

FromMarkus Heinz <markus.heinz@uni-dortmund.de>
Date2014-12-18 13:11 +0100
Message-ID<m6ug9r$pni$1@speranza.aioe.org>
In reply to#14752
Hello.

On 2014-12-18 at 12:39 Joydeep Chakrabarty wrote:
> Hello,
>
> I am trying to read a CSV file from my web server. Here is my code.
>
> <?php
>      $file = "http://localhost/x.csv";
>      $fp = fopen ($file, "r");
>      if (!$file) {
>          echo "<p>Unable to open remote file.\n";
>          exit;
>      }
>      while(!feof($fp)) {
>          $data = fgetcsv($file, 1024);
>          print "Data = " . $data[0] . "<BR>";
>      }
>      fclose($fp);
> ?>
>
> It can access my csv file in my document root folder. But it gets into
> infinite loop and does not display anything from $data array. Is there
> something I am missing?
> Please help.

You do not check correctly if the file has been opened. It looks like 
you have interchanged $file and $fp in the if statement. So my 
assumption is the file is not opened at all and this causes the infinite 
loop because feof will return false if the file pointer is not valid.

Furthermore you should consider using a local path in the fopen call if 
the CSV file is on the same server anyways.

Regards

Markus

[toc] | [prev] | [next] | [standalone]


#14754

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-18 13:27 +0100
Message-ID<m6uh6o$qna$1@solani.org>
In reply to#14753
Markus Heinz wrote:

> Furthermore you should consider using a local path in the fopen call if
> the CSV file is on the same server anyways.

ACK

Furthermore I would suggest to avoid feof(), and instead check the
return value of fgetcsv():

  while (($data = fgetcsv($handle, 1024)) !== FALSE) {

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14757

FromJoydeep Chakrabarty <chalao.adda@gmail.com>
Date2014-12-18 18:26 +0530
Message-ID<m6uit8$vi5$1@dont-email.me>
In reply to#14754
Christoph M. Becker wrote:

> Markus Heinz wrote:
> 
>> Furthermore you should consider using a local path in the fopen call if
>> the CSV file is on the same server anyways.
> 
> ACK
> 
> Furthermore I would suggest to avoid feof(), and instead check the
> return value of fgetcsv():
> 
>   while (($data = fgetcsv($handle, 1024)) !== FALSE) {
> 

I changed my code to that. Now, my code is - 

<?php
    $file = "http://localhost/x.csv";
    $fp = fopen ($file, "r");
    if (!$file) {
        echo "<p>Unable to open remote file.\n";
        exit;
    }
    while (($data = fgetcsv($fp, 1024)) !== FALSE) {
        print "Data = " . $data[0] . "<BR>";
    }
    fclose($fp);
?>

Same problem.

Thanks.

[toc] | [prev] | [next] | [standalone]


#14761

FromMarkus Heinz <markus.heinz@uni-dortmund.de>
Date2014-12-18 14:43 +0100
Message-ID<m6ulle$bc5$1@speranza.aioe.org>
In reply to#14757
Hello.

On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
> I changed my code to that. Now, my code is -
>
> <?php
>      $file = "http://localhost/x.csv";
>      $fp = fopen ($file, "r");
>      if (!$file) {
>          echo "<p>Unable to open remote file.\n";
>          exit;
>      }
>      while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>          print "Data = " . $data[0] . "<BR>";
>      }
>      fclose($fp);
> ?>
>
> Same problem.

This code snippet still contains the same error as the original one 
regarding the if statement.

If the fopen fails it should issue a warning. If the warning is not 
displayed on screen, it should be found in the server logs. For apache 
this is the file "error.log".

Regards

Markus

[toc] | [prev] | [next] | [standalone]


#14763

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-12-18 09:24 -0500
Message-ID<m6uo22$k3n$1@dont-email.me>
In reply to#14761
On 12/18/2014 8:43 AM, Markus Heinz wrote:
> Hello.
> 
> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>> I changed my code to that. Now, my code is -
>>
>> <?php
>>      $file = "http://localhost/x.csv";
>>      $fp = fopen ($file, "r");
>>      if (!$file) {
>>          echo "<p>Unable to open remote file.\n";
>>          exit;
>>      }
>>      while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>          print "Data = " . $data[0] . "<BR>";
>>      }
>>      fclose($fp);
>> ?>
>>
>> Same problem.
> 
> This code snippet still contains the same error as the original one
> regarding the if statement.
> 
> If the fopen fails it should issue a warning. If the warning is not
> displayed on screen, it should be found in the server logs. For apache
> this is the file "error.log".
>

Whether it shows up in the server log and where the log is located is
completely dependent on the PHP configuration.  There may or may not be
messages in the file, and it may or may not be known as "error.log".

> Regards
> 
> Markus
> 
> 




-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14764

FromJoydeep Chakrabarty <chalao.adda@gmail.com>
Date2014-12-18 20:56 +0530
Message-ID<m6urna$3o6$1@dont-email.me>
In reply to#14761
Markus Heinz wrote:

> Hello.
> 
> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>> I changed my code to that. Now, my code is -
>>
>> <?php
>>      $file = "http://localhost/x.csv";
>>      $fp = fopen ($file, "r");
>>      if (!$file) {
>>          echo "<p>Unable to open remote file.\n";
>>          exit;
>>      }
>>      while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>          print "Data = " . $data[0] . "<BR>";
>>      }
>>      fclose($fp);
>> ?>
>>
>> Same problem.
> 
> This code snippet still contains the same error as the original one
> regarding the if statement.
> 
> If the fopen fails it should issue a warning. If the warning is not
> displayed on screen, it should be found in the server logs. For apache
> this is the file "error.log".
> 
> Regards
> 
> Markus
That's right. I changed my code to just - 

$fp = fopen ($file, "r") or die("Unable to open file");

It returned "Unable to open file".
So it was wrong. What is the right way to access the file?

Thanks.

[toc] | [prev] | [next] | [standalone]


#14765

FromMarkus Heinz <markus.heinz@uni-dortmund.de>
Date2014-12-18 16:57 +0100
Message-ID<m6utgf$rv$1@speranza.aioe.org>
In reply to#14764
On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
> Markus Heinz wrote:
>
>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>> I changed my code to that. Now, my code is -
>>>
>>> <?php
>>>       $file = "http://localhost/x.csv";
>>>       $fp = fopen ($file, "r");
>>>       if (!$file) {
>>>           echo "<p>Unable to open remote file.\n";
>>>           exit;
>>>       }
>>>       while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>           print "Data = " . $data[0] . "<BR>";
>>>       }
>>>       fclose($fp);
>>> ?>
>>>
>>> Same problem.
>>
>> This code snippet still contains the same error as the original one
>> regarding the if statement.
>>
>> If the fopen fails it should issue a warning. If the warning is not
>> displayed on screen, it should be found in the server logs. For apache
>> this is the file "error.log".
>>
> That's right. I changed my code to just -
>
> $fp = fopen ($file, "r") or die("Unable to open file");
>
> It returned "Unable to open file".
> So it was wrong. What is the right way to access the file?

Do you have PHP warnings enabled in your configuration? If so, did you 
check the server logs for a message about the failed fopen? There should 
be a hint about the cause.

Is it required that the CSV files can be fetched from different servers 
or will they always be on the same server your script runs on?

If the CVS files are always on the same server you should be able to 
open them with a local path:

$file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
$fp = fopen($file, 'r') or die("Unable to open file");

Regards

Markus

[toc] | [prev] | [next] | [standalone]


#14766

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-18 17:04 +0100
Message-ID<m6utu1$92f$1@solani.org>
In reply to#14765
Markus Heinz wrote:

> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>
>> Markus Heinz wrote:
>>
>>> If the fopen fails it should issue a warning. If the warning is not
>>> displayed on screen, it should be found in the server logs. For apache
>>> this is the file "error.log".
>>>
>> That's right. I changed my code to just -
>>
>> $fp = fopen ($file, "r") or die("Unable to open file");
>>
>> It returned "Unable to open file".
>> So it was wrong. What is the right way to access the file?
> 
> Do you have PHP warnings enabled in your configuration? If so, did you
> check the server logs for a message about the failed fopen? There should
> be a hint about the cause.

From php.ini (PHP 5.4):

    ; display_errors
    ;   Development Value: On

    ; display_startup_errors
    ;   Development Value: On

    ; error_reporting
    ;   Development Value: E_ALL


I suggest to use the suggested development values in a development
environment.  Then there is no need to consult the error log.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14767

FromJoydeep Chakrabarty <chalao.adda@gmail.com>
Date2014-12-18 21:52 +0530
Message-ID<m6uuve$hfr$1@dont-email.me>
In reply to#14765
Markus Heinz wrote:

> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>> Markus Heinz wrote:
>>
>>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>>> I changed my code to that. Now, my code is -
>>>>
>>>> <?php
>>>>       $file = "http://localhost/x.csv";
>>>>       $fp = fopen ($file, "r");
>>>>       if (!$file) {
>>>>           echo "<p>Unable to open remote file.\n";
>>>>           exit;
>>>>       }
>>>>       while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>>           print "Data = " . $data[0] . "<BR>";
>>>>       }
>>>>       fclose($fp);
>>>> ?>
>>>>
>>>> Same problem.
>>>
>>> This code snippet still contains the same error as the original one
>>> regarding the if statement.
>>>
>>> If the fopen fails it should issue a warning. If the warning is not
>>> displayed on screen, it should be found in the server logs. For apache
>>> this is the file "error.log".
>>>
>> That's right. I changed my code to just -
>>
>> $fp = fopen ($file, "r") or die("Unable to open file");
>>
>> It returned "Unable to open file".
>> So it was wrong. What is the right way to access the file?
> 
> Do you have PHP warnings enabled in your configuration? If so, did you
> check the server logs for a message about the failed fopen? There should
> be a hint about the cause.
> 
> Is it required that the CSV files can be fetched from different servers
> or will they always be on the same server your script runs on?
> 
> If the CVS files are always on the same server you should be able to
> open them with a local path:
> 
> $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
> $fp = fopen($file, 'r') or die("Unable to open file");
> 
> Regards
> 
> Markus

My CSV file is in the same server. I changed my code according to your 
suggestion. But it gives me "Unable to open file".

<?php
    $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
    echo $file . "<br />\n";
    $fp = fopen ($file, "r") or die("Unable to open file");
    fclose($fp);
?>


Thanks.

[toc] | [prev] | [next] | [standalone]


#14768

FromMarkus Heinz <markus.heinz@uni-dortmund.de>
Date2014-12-18 17:38 +0100
Message-ID<m6uvul$7ok$1@speranza.aioe.org>
In reply to#14767
On 2014-12-18 at 17:22 Joydeep Chakrabarty wrote:
> Markus Heinz wrote:
>
>> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>>> Markus Heinz wrote:
>>>
>>>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>>>> I changed my code to that. Now, my code is -
>>>>>
>>>>> <?php
>>>>>        $file = "http://localhost/x.csv";
>>>>>        $fp = fopen ($file, "r");
>>>>>        if (!$file) {
>>>>>            echo "<p>Unable to open remote file.\n";
>>>>>            exit;
>>>>>        }
>>>>>        while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>>>            print "Data = " . $data[0] . "<BR>";
>>>>>        }
>>>>>        fclose($fp);
>>>>> ?>
>>>>>
>>>>> Same problem.
>>>>
>>>> This code snippet still contains the same error as the original one
>>>> regarding the if statement.
>>>>
>>>> If the fopen fails it should issue a warning. If the warning is not
>>>> displayed on screen, it should be found in the server logs. For apache
>>>> this is the file "error.log".
>>>>
>>> That's right. I changed my code to just -
>>>
>>> $fp = fopen ($file, "r") or die("Unable to open file");
>>>
>>> It returned "Unable to open file".
>>> So it was wrong. What is the right way to access the file?
>>
>> Do you have PHP warnings enabled in your configuration? If so, did you
>> check the server logs for a message about the failed fopen? There should
>> be a hint about the cause.
>>
>> Is it required that the CSV files can be fetched from different servers
>> or will they always be on the same server your script runs on?
>>
>> If the CVS files are always on the same server you should be able to
>> open them with a local path:
>>
>> $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>> $fp = fopen($file, 'r') or die("Unable to open file");
>>
> My CSV file is in the same server. I changed my code according to your
> suggestion. But it gives me "Unable to open file".
>
> <?php
>      $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>      echo $file . "<br />\n";
>      $fp = fopen ($file, "r") or die("Unable to open file");
>      fclose($fp);
> ?>

There are at least two possibilities. The most obvious are:

1. The path to the file is wrong. Is it really located in the top folder 
of the document root of your webserver as you wrote in the first code 
snippet?
2. The web server process does not have read permission on the file.

Regards

Markus

[toc] | [prev] | [next] | [standalone]


#14769

FromJoydeep Chakrabarty <chalao.adda@gmail.com>
Date2014-12-18 16:49 +0000
Message-ID<m6v0j8$irg$1@dont-email.me>
In reply to#14768
On Thu, 18 Dec 2014 17:38:49 +0100, Markus Heinz wrote:

> On 2014-12-18 at 17:22 Joydeep Chakrabarty wrote:
>> Markus Heinz wrote:
>>
>>> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>>>> Markus Heinz wrote:
>>>>
>>>>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>>>>> I changed my code to that. Now, my code is -
>>>>>>
>>>>>> <?php
>>>>>>        $file = "http://localhost/x.csv";
>>>>>>        $fp = fopen ($file, "r");
>>>>>>        if (!$file) {
>>>>>>            echo "<p>Unable to open remote file.\n";
>>>>>>            exit;
>>>>>>        }
>>>>>>        while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>>>>            print "Data = " . $data[0] . "<BR>";
>>>>>>        }
>>>>>>        fclose($fp);
>>>>>> ?>
>>>>>>
>>>>>> Same problem.
>>>>>
>>>>> This code snippet still contains the same error as the original one
>>>>> regarding the if statement.
>>>>>
>>>>> If the fopen fails it should issue a warning. If the warning is not
>>>>> displayed on screen, it should be found in the server logs. For
>>>>> apache this is the file "error.log".
>>>>>
>>>> That's right. I changed my code to just -
>>>>
>>>> $fp = fopen ($file, "r") or die("Unable to open file");
>>>>
>>>> It returned "Unable to open file".
>>>> So it was wrong. What is the right way to access the file?
>>>
>>> Do you have PHP warnings enabled in your configuration? If so, did you
>>> check the server logs for a message about the failed fopen? There
>>> should be a hint about the cause.
>>>
>>> Is it required that the CSV files can be fetched from different
>>> servers or will they always be on the same server your script runs on?
>>>
>>> If the CVS files are always on the same server you should be able to
>>> open them with a local path:
>>>
>>> $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>> $fp = fopen($file, 'r') or die("Unable to open file");
>>>
>> My CSV file is in the same server. I changed my code according to your
>> suggestion. But it gives me "Unable to open file".
>>
>> <?php
>>      $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>      echo $file . "<br />\n";
>>      $fp = fopen ($file, "r") or die("Unable to open file");
>>      fclose($fp);
>> ?>
> 
> There are at least two possibilities. The most obvious are:
> 
> 1. The path to the file is wrong. Is it really located in the top folder
> of the document root of your webserver as you wrote in the first code
> snippet?
> 2. The web server process does not have read permission on the file.
> 
> Regards
> 
> Markus

You are right. There was no read permission. I changed it and it's 
working perfectly. Thank you very much.

Thanks.

[toc] | [prev] | [next] | [standalone]


#14772

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-12-18 21:34 -0500
Message-ID<m702rq$1a9$1@dont-email.me>
In reply to#14767
On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
> Markus Heinz wrote:
> 
>> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>>> Markus Heinz wrote:
>>>
>>>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>>>> I changed my code to that. Now, my code is -
>>>>>
>>>>> <?php
>>>>>       $file = "http://localhost/x.csv";
>>>>>       $fp = fopen ($file, "r");
>>>>>       if (!$file) {
>>>>>           echo "<p>Unable to open remote file.\n";
>>>>>           exit;
>>>>>       }
>>>>>       while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>>>           print "Data = " . $data[0] . "<BR>";
>>>>>       }
>>>>>       fclose($fp);
>>>>> ?>
>>>>>
>>>>> Same problem.
>>>>
>>>> This code snippet still contains the same error as the original one
>>>> regarding the if statement.
>>>>
>>>> If the fopen fails it should issue a warning. If the warning is not
>>>> displayed on screen, it should be found in the server logs. For apache
>>>> this is the file "error.log".
>>>>
>>> That's right. I changed my code to just -
>>>
>>> $fp = fopen ($file, "r") or die("Unable to open file");
>>>
>>> It returned "Unable to open file".
>>> So it was wrong. What is the right way to access the file?
>>
>> Do you have PHP warnings enabled in your configuration? If so, did you
>> check the server logs for a message about the failed fopen? There should
>> be a hint about the cause.
>>
>> Is it required that the CSV files can be fetched from different servers
>> or will they always be on the same server your script runs on?
>>
>> If the CVS files are always on the same server you should be able to
>> open them with a local path:
>>
>> $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>> $fp = fopen($file, 'r') or die("Unable to open file");
>>
>> Regards
>>
>> Markus
> 
> My CSV file is in the same server. I changed my code according to your 
> suggestion. But it gives me "Unable to open file".
> 
> <?php
>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>     echo $file . "<br />\n";
>     $fp = fopen ($file, "r") or die("Unable to open file");
>     fclose($fp);
> ?>
> 
> 
> Thanks.
> 

You should never use die() (or exit() on a production server.  It
terminates processing of the script immediately, which produces invalid
(x)html.  Plus it looks like crap to the user.

Rather, use an "if" statement to branch around the code which uses the
unavailable resource and complete other processing.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14773

FromTim Streater <timstreater@greenbee.net>
Date2014-12-19 09:43 +0000
Message-ID<191220140943102761%timstreater@greenbee.net>
In reply to#14772
In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
<jstucklex@attglobal.net> wrote:

>On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>> Markus Heinz wrote:
>> 
>>> On 2014-12-18 at 16:26 Joydeep Chakrabarty wrote:
>>>> Markus Heinz wrote:
>>>>
>>>>> On 2014-12-18 at 13:56 Joydeep Chakrabarty wrote:
>>>>>> I changed my code to that. Now, my code is -
>>>>>>
>>>>>> <?php
>>>>>>       $file = "http://localhost/x.csv";
>>>>>>       $fp = fopen ($file, "r");
>>>>>>       if (!$file) {
>>>>>>           echo "<p>Unable to open remote file.\n";
>>>>>>           exit;
>>>>>>       }
>>>>>>       while (($data = fgetcsv($fp, 1024)) !== FALSE) {
>>>>>>           print "Data = " . $data[0] . "<BR>";
>>>>>>       }
>>>>>>       fclose($fp);
>>>>>> ?>
>>>>>>
>>>>>> Same problem.
>>>>>
>>>>> This code snippet still contains the same error as the original one
>>>>> regarding the if statement.
>>>>>
>>>>> If the fopen fails it should issue a warning. If the warning is not
>>>>> displayed on screen, it should be found in the server logs. For apache
>>>>> this is the file "error.log".
>>>>>
>>>> That's right. I changed my code to just -
>>>>
>>>> $fp = fopen ($file, "r") or die("Unable to open file");
>>>>
>>>> It returned "Unable to open file".
>>>> So it was wrong. What is the right way to access the file?
>>>
>>> Do you have PHP warnings enabled in your configuration? If so, did you
>>> check the server logs for a message about the failed fopen? There should
>>> be a hint about the cause.
>>>
>>> Is it required that the CSV files can be fetched from different servers
>>> or will they always be on the same server your script runs on?
>>>
>>> If the CVS files are always on the same server you should be able to
>>> open them with a local path:
>>>
>>> $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>> $fp = fopen($file, 'r') or die("Unable to open file");
>>>
>>> Regards
>>>
>>> Markus
>> 
>> My CSV file is in the same server. I changed my code according to your 
>> suggestion. But it gives me "Unable to open file".
>> 
>> <?php
>>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>     echo $file . "<br />\n";
>>     $fp = fopen ($file, "r") or die("Unable to open file");
>>     fclose($fp);
>> ?>
>> 
>> 
>> Thanks.
>> 
>
>You should never use die() (or exit() on a production server.  It
>terminates processing of the script immediately, which produces invalid
>(x)html.  Plus it looks like crap to the user.

I dunno why the OP doesn't read the documentation. What I do is:

  $fp = fopen ($file, "r")
  if  ($fp===false)
       {
       // Perform error processing here, log the error in my log file
       // and notify the remote end appropriately
       exit ();
       }

-- 
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#14774

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-19 14:05 +0100
Message-ID<m717q6$ka4$1@solani.org>
In reply to#14773
Tim Streater wrote:

> In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
> <jstucklex@attglobal.net> wrote:
> 
>> On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>>
>>> <?php
>>>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>>     echo $file . "<br />\n";
>>>     $fp = fopen ($file, "r") or die("Unable to open file");
>>>     fclose($fp);
>>> ?>
>>
>> You should never use die() (or exit() on a production server.  It
>> terminates processing of the script immediately, which produces invalid
>> (x)html.  Plus it looks like crap to the user.

While I absolutely agree that it is a very bad idea to terminate the
script in the middle of the output, prohibiting die or exit in a
production environment is too hard and fast a rule, IMO.  It's like
forbidding return, unless as the last statement of a function.

> I dunno why the OP doesn't read the documentation. What I do is:
> 
>  $fp = fopen ($file, "r")
>  if  ($fp===false)
>       {
>       // Perform error processing here, log the error in my log file
>       // and notify the remote end appropriately
>       exit ();
>       }

What I would do is something like:

  read_the_file();
  generate_output();

IOW: I would separate the presentation code from the domain code.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14776

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-12-19 10:59 -0500
Message-ID<m71i0c$ht4$1@dont-email.me>
In reply to#14774
On 12/19/2014 8:05 AM, Christoph M. Becker wrote:
> Tim Streater wrote:
> 
>> In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
>> <jstucklex@attglobal.net> wrote:
>>
>>> On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>>>
>>>> <?php
>>>>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>>>     echo $file . "<br />\n";
>>>>     $fp = fopen ($file, "r") or die("Unable to open file");
>>>>     fclose($fp);
>>>> ?>
>>>
>>> You should never use die() (or exit() on a production server.  It
>>> terminates processing of the script immediately, which produces invalid
>>> (x)html.  Plus it looks like crap to the user.
> 
> While I absolutely agree that it is a very bad idea to terminate the
> script in the middle of the output, prohibiting die or exit in a
> production environment is too hard and fast a rule, IMO.  It's like
> forbidding return, unless as the last statement of a function.
>

return does not create invalid output.  die() and exit() do.

I have *never* found a need for either one in a properly constructed
script.

>> I dunno why the OP doesn't read the documentation. What I do is:
>>
>>  $fp = fopen ($file, "r")
>>  if  ($fp===false)
>>       {
>>       // Perform error processing here, log the error in my log file
>>       // and notify the remote end appropriately
>>       exit ();
>>       }
> 
> What I would do is something like:
> 
>   read_the_file();
>   generate_output();
> 
> IOW: I would separate the presentation code from the domain code.
> 

I don't think it's necessarily a good idea to do this.  If the functions
are only called one from one place, it upsets the flow of the script,
making the reader go back and forth to follow processing.  It also means
you have to read the entire script in before generating any output,
instead of completely processing the input line by line.

I prefer something like:

 $fp = fopen ($file, "r")
 if  ($fp) {
    // Read the file and process it
 }
 else {
   // Perform error processing here, log the error in my log file
   // and notify the remote end appropriately
 }


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14778

FromMatthew Carter <m@ahungry.com>
Date2014-12-19 14:21 -0500
Message-ID<87y4q3wilr.fsf@ahungry.com>
In reply to#14776
Jerry Stuckle <jstucklex@attglobal.net> writes:

> On 12/19/2014 8:05 AM, Christoph M. Becker wrote:
>> Tim Streater wrote:
>> 
>>> In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
>>> <jstucklex@attglobal.net> wrote:
>>>
>>>> On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>>>>
>>>>> <?php
>>>>>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>>>>     echo $file . "<br />\n";
>>>>>     $fp = fopen ($file, "r") or die("Unable to open file");
>>>>>     fclose($fp);
>>>>> ?>
>>>>
>>>> You should never use die() (or exit() on a production server.  It
>>>> terminates processing of the script immediately, which produces invalid
>>>> (x)html.  Plus it looks like crap to the user.
>> 
>> While I absolutely agree that it is a very bad idea to terminate the
>> script in the middle of the output, prohibiting die or exit in a
>> production environment is too hard and fast a rule, IMO.  It's like
>> forbidding return, unless as the last statement of a function.
>>
>
> return does not create invalid output.  die() and exit() do.
>
> I have *never* found a need for either one in a properly constructed
> script.
>
>>> I dunno why the OP doesn't read the documentation. What I do is:
>>>
>>>  $fp = fopen ($file, "r")
>>>  if  ($fp===false)
>>>       {
>>>       // Perform error processing here, log the error in my log file
>>>       // and notify the remote end appropriately
>>>       exit ();
>>>       }
>> 
>> What I would do is something like:
>> 
>>   read_the_file();
>>   generate_output();
>> 
>> IOW: I would separate the presentation code from the domain code.
>> 
>
> I don't think it's necessarily a good idea to do this.  If the functions
> are only called one from one place, it upsets the flow of the script,
> making the reader go back and forth to follow processing.  It also means
> you have to read the entire script in before generating any output,
> instead of completely processing the input line by line.
>
> I prefer something like:
>
>  $fp = fopen ($file, "r")
>  if  ($fp) {
>     // Read the file and process it
>  }
>  else {
>    // Perform error processing here, log the error in my log file
>    // and notify the remote end appropriately
>  }

Exceptions work pretty well (instead of many if/else blocks).  Then you
can handle your output at the end if the process fails (and line by line
if it doesn't).

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [next] | [standalone]


#14779

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-12-19 18:17 -0500
Message-ID<m72bma$tnm$1@dont-email.me>
In reply to#14778
On 12/19/2014 2:21 PM, Matthew Carter wrote:
> Jerry Stuckle <jstucklex@attglobal.net> writes:
> 
>> On 12/19/2014 8:05 AM, Christoph M. Becker wrote:
>>> Tim Streater wrote:
>>>
>>>> In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
>>>> <jstucklex@attglobal.net> wrote:
>>>>
>>>>> On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>>>>>
>>>>>> <?php
>>>>>>     $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>>>>>     echo $file . "<br />\n";
>>>>>>     $fp = fopen ($file, "r") or die("Unable to open file");
>>>>>>     fclose($fp);
>>>>>> ?>
>>>>>
>>>>> You should never use die() (or exit() on a production server.  It
>>>>> terminates processing of the script immediately, which produces invalid
>>>>> (x)html.  Plus it looks like crap to the user.
>>>
>>> While I absolutely agree that it is a very bad idea to terminate the
>>> script in the middle of the output, prohibiting die or exit in a
>>> production environment is too hard and fast a rule, IMO.  It's like
>>> forbidding return, unless as the last statement of a function.
>>>
>>
>> return does not create invalid output.  die() and exit() do.
>>
>> I have *never* found a need for either one in a properly constructed
>> script.
>>
>>>> I dunno why the OP doesn't read the documentation. What I do is:
>>>>
>>>>  $fp = fopen ($file, "r")
>>>>  if  ($fp===false)
>>>>       {
>>>>       // Perform error processing here, log the error in my log file
>>>>       // and notify the remote end appropriately
>>>>       exit ();
>>>>       }
>>>
>>> What I would do is something like:
>>>
>>>   read_the_file();
>>>   generate_output();
>>>
>>> IOW: I would separate the presentation code from the domain code.
>>>
>>
>> I don't think it's necessarily a good idea to do this.  If the functions
>> are only called one from one place, it upsets the flow of the script,
>> making the reader go back and forth to follow processing.  It also means
>> you have to read the entire script in before generating any output,
>> instead of completely processing the input line by line.
>>
>> I prefer something like:
>>
>>  $fp = fopen ($file, "r")
>>  if  ($fp) {
>>     // Read the file and process it
>>  }
>>  else {
>>    // Perform error processing here, log the error in my log file
>>    // and notify the remote end appropriately
>>  }
> 
> Exceptions work pretty well (instead of many if/else blocks).  Then you
> can handle your output at the end if the process fails (and line by line
> if it doesn't).
> 

True, exceptions help a lot.  But unfortunately, this also ends up in a
group of nested blocks - just try/catch instead of if/else.

IMHO the biggest advantage to exceptions is they can cross function
boundaries (i.e throw an exception in a function then handle it in the
called function) - which can be a great help.

Don't get me wrong - I love exceptions.  But, unfortunate, like any
other coding convention, it's not the answer to life, the universe and
everything :)

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14780

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2014-12-20 00:35 +0100
Message-ID<cfjr2iFok9qU1@mid.uni-berlin.de>
In reply to#14778
On 19.12.2014 20:21, Matthew Carter wrote:
> Jerry Stuckle <jstucklex@attglobal.net> writes:
>
>> On 12/19/2014 8:05 AM, Christoph M. Becker wrote:
>>> Tim Streater wrote:
>>>
>>>> In article <m702rq$1a9$1@dont-email.me>, Jerry Stuckle
>>>> <jstucklex@attglobal.net> wrote:
>>>>
>>>>> On 12/18/2014 11:22 AM, Joydeep Chakrabarty wrote:
>>>>>
>>>>>> <?php
>>>>>>      $file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'x.csv';
>>>>>>      echo $file . "<br />\n";
>>>>>>      $fp = fopen ($file, "r") or die("Unable to open file");
>>>>>>      fclose($fp);
>>>>>> ?>
>>>>>
>>>>> You should never use die() (or exit() on a production server.  It
>>>>> terminates processing of the script immediately, which produces invalid
>>>>> (x)html.  Plus it looks like crap to the user.
>>>
>>> While I absolutely agree that it is a very bad idea to terminate the
>>> script in the middle of the output, prohibiting die or exit in a
>>> production environment is too hard and fast a rule, IMO.  It's like
>>> forbidding return, unless as the last statement of a function.
>>>
>>
>> return does not create invalid output.  die() and exit() do.
>>
>> I have *never* found a need for either one in a properly constructed
>> script.
>>
>>>> I dunno why the OP doesn't read the documentation. What I do is:
>>>>
>>>>   $fp = fopen ($file, "r")
>>>>   if  ($fp===false)
>>>>        {
>>>>        // Perform error processing here, log the error in my log file
>>>>        // and notify the remote end appropriately
>>>>        exit ();
>>>>        }
>>>
>>> What I would do is something like:
>>>
>>>    read_the_file();
>>>    generate_output();
>>>
>>> IOW: I would separate the presentation code from the domain code.
>>>
>>
>> I don't think it's necessarily a good idea to do this.  If the functions
>> are only called one from one place, it upsets the flow of the script,
>> making the reader go back and forth to follow processing.  It also means
>> you have to read the entire script in before generating any output,
>> instead of completely processing the input line by line.
>>
>> I prefer something like:
>>
>>   $fp = fopen ($file, "r")
>>   if  ($fp) {
>>      // Read the file and process it
>>   }
>>   else {
>>     // Perform error processing here, log the error in my log file
>>     // and notify the remote end appropriately
>>   }
>
> Exceptions work pretty well (instead of many if/else blocks).  Then you
> can handle your output at the end if the process fails (and line by line
> if it doesn't).
>

I think so as well. In another language (ocaml) they use exceptions for end of file 
and other "regular" conditions, so one gets to understand that it is just another 
structure tool. Provided they are cheap (in Java they are not so cheap).

In PHP, for example when using PDO, it saves you the need to check every return code. 
But you should take care to capture the exception only at the level where you can 
decide if/how to continue.

/Str.

[toc] | [prev] | [next] | [standalone]


#14855

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2015-02-04 13:19 +0100
Message-ID<54d20e47$0$2897$e4fe514c@news2.news.xs4all.nl>
In reply to#14776
On 12/19/2014 4:59 PM, Jerry Stuckle wrote:

<snip>
>
> return does not create invalid output.  die() and exit() do.
>
> I have *never* found a need for either one in a properly constructed
> script.
>

I use exit() after a header location combi.

if (whatever){
	dostuff();
	header("location: ....");
	exit;
}

It is an easy way to redirect, and terminate the script, and not having 
a (possibly huge) else-part.

Do you (Jerry) think that that approach isn't properly?
If so, why?


Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


Page 1 of 4  [1] 2 3 4  Next page →

Back to top | Article view | comp.lang.php


csiph-web