Jump to content

php code to download CSV file from a library located above Wwwroot


webactor

Recommended Posts

Hello

 

I have made a php code that import a CSV file into a table in MySQL

database on the same website.

 

My problem is that I want the php code to download CSV file from a

library located above Wwwroot

 

Is there anyone who knows a solution to this?

Or maybe can guide me in the right direction

 

Hope can help me

 

Kimo

Link to comment
Share on other sites

The full path to the script is /wwwroot/impot.php

 

And to the csv file, the full path is /import/file.csv

 

 

 

Attributes to /import is 666

 

And to /wwwroot also 666

 

 

 

The code is

 

$query = "truncate table testkart";

 

$query = @mysql_query($query,$link);

 

 

 

$handle = fopen ('..\..\..\import\file.csv', 'r');

 

                                            while (($data = fgetcsv($handle, 1000, ';', '"')) !== FALSE)

 

                                            {

 

                                                                $query = "INSERT INTO testkart VALUES ('". implode("','", $data)."')";

 

                                                                $query = @mysql_query($query,$link);

 

                                            }

 

 

 

mysql_close($link);       

 

 

 

 

 

the error I get is

 

Warning: fopen(..\..\..\import\file.csv) [function.fopen]: failed to open stream: No such file or directory in

 

 

 

And the file is there… I have tried with path as \import and ..\import  and so on,, same error every time…

 

Best regards Kimo

Link to comment
Share on other sites

Rhodesa answered your question, I just want to add some info into this.  You have fallen prey to a common problem when dealing with files and directories.  Long ago I use to get very confused when it came to directories and how to access certain files in certain places. I use to get confused on using ../ and ../../ and ./ and everything else when I first started working with files and directories.

It took me awhile to finally learn exactly how the structure would go in every situation.

Basically let's say you are trying to accessing

 

ROOT/tempfolder/tempfolder2/file.php

In this situation you are trying to access file.php.  The following legend is ALWAYS application

"/" before what you are seeking will always start at root directory. So "/tempfolder" will access the tempfolder directory. 

Not using "/" before hand automatically means you are trying to access whatever is in the DIRECTORY YOU ARE CURRENTLY IN.

So..if you are in tempfolder and you do "tempfolder2/file.php" you can access the file.php.  However if you do "/tempfolder2/file.php" you cannot access it.  You would have to do "/tempfolder/tempfolder2/file.php" instead.  The reason being is because if you are in that folder and you use the "/" in front of what you are looking for then that means you are starting your search at the root level.  Not putting the "/" is signifying that you want to start your url from the folder you are currently in.  So if there was a file under tempfolder that stated "index.php" and you were IN the tempfolder location you could access that file either "/tempfolder/tempfolder2/index.php" or you could simply use "index.php".  Again the difference is with the "/" symbol.  When you use it you start your search at root and have to specify the exact location of your file..if you are wanting to look in your same folder then do not use that and you can just climb from the one you are in.

 

There are other symbols as well.  If you are in tempfolder2 and want to backup just to access something behind you then you can actually use the "/" to put in the full url or you can go ahead and access it via the backup command. So for example you have a file in tempfolder instead called "index.php" and your in tempfolder2.  You can either access the full url "/tempfolder/index.php" or you can back up and grab it instead using "../index.php".  The ../ will kick you back one folder then you can access whatever is in that folder.  The ../ is a repeatable command.  Everytime it is used it takes you back one folder.  So if you want to go back 2 folders you use ../../ 3 folders would be ../../../ and generally you don't need to go to the trouble when you can access the folder directly using "/" command.  But it is nice to know and thinking on this can help solve your directory issues.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.