open file from database

Discussion in 'PHP' started by anisFarim, Oct 8, 2010.

  1. #1
    i want to open file from database. that file already upload and save in database and open it with new window. i try with this code: $file=fopen("$name","r"); but it not work.
    my coding:

    <?php
    $db = mysql_connect("localhost","root")
    or die ("Could Not Connect to Server" . mysql_error());

    /* select database */
    mysql_select_db("ppk4a", $db)
    or die ("Could Not Connect to Server" . mysql_error());

    if (isset($_GET['open'])){

    $id=mysql_real_escape_string($_GET['open']);
    $select=mysql_query("SELECT name FROM aktivity WHERE id='$id'");
    $result = mysql_fetch_array($select);
    $name = $result['name'];

    $file=fopen("$name","r");
    }
    ?>

    i get this error: Warning: fopen(url layout css.txt) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\PPK4A\aktivity.php on line 139

    please help me......
     
    anisFarim, Oct 8, 2010 IP
  2. raaj.developer

    raaj.developer Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This error shows that your path is incorrect. Please check that.
    I think
    "url" needs to be dynamic there or some path.
     
    raaj.developer, Oct 9, 2010 IP
  3. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please give us some sample data (like the "url layout css.txt" record). Also check and make sure you have fopen enabled in your PHP.INI.

    A bit off topic to your request to fix the issue, but I'd like to recommend some code for you. :)

    When you have a function that opens a file, url, or similar, you should always verify it exists to make sure you don't have any PHP errors during production.
    If it was applied to your code above, an example solution would be:

    
    if(is_readable($name))
    {
    $file=fopen("$name","r");
    }
    else
    {
    echo 'An error occurred while fetching the file.';
    }
    Code (markup):
     
    JoelLarson, Oct 9, 2010 IP
  4. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    file layout css.txt already have in database. Must i write a full url where layout css.txt record? that layout css.txt file record at my desktop. user upload that file and save it into database. after that they can click the name of file and that file will open. it is possible to open a file from database?
     
    anisFarim, Oct 9, 2010 IP
  5. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When you read a file with PHP, you must have either the relative or absolute path to it. An example of a relative path is:
    ./file/layout/css.txt
    Code (markup):
    Say that my php directory is: /var/www

    This would start from the directory of the php file called (/var/www/index.php), then go to /var/www/file/layout/css.txt.

    If I did absolute file path, my path would be:
    /var/www/file/layout/css.txt
    Code (markup):
    Absolute is faster when finding a file, but relative makes it easy to migrate scripts and keeps it localised.
     
    JoelLarson, Oct 9, 2010 IP