SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    interacting with the filesystem

    I am planning to store some pdf articles on the filesystem and access the files via php code. Ive managed to upload some txt files to the filesystem and display the file names with a link once they are uploaded.
    The problem is when I click on a link a 'page cannot be found' error appears.

    Okay, so I can upload files to /tmp/uploads/

    I've got a readdirectory.php script in the public html directory /www/articles_files/

    When I run the script it displays the /tmp/uploads/ directory and lists the files as links but when I click on a link it trys to access the file from the public html diresctory /www/articles_files/file.txt when it's really stored in /tmp/uploads/file.txt

    How do I stop this happening?

    I've tried naming the entire path i.e. /home/user/tmp/uploads but I just get an error message saying the directory doesn't exist.

    I'm going crazy trying to figure this out.

    Daniela.

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    Bishkek
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your server cant access the /tmp/uploads/ directory.
    simply upload files in /www/articles_files and fix your script to display files from /www/articles_files/

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Czech Rep.
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by daniela
    Okay, so I can upload files to /tmp/uploads/
    I've got a readdirectory.php script in the public html directory /www/articles_files/
    When I run the script it displays the /tmp/uploads/ directory and lists the files as links but when I click on a link it trys to access the file from the public html diresctory /www/articles_files/file.txt when it's really stored in /tmp/uploads/file.txt
    How do I stop this happening?
    I've tried naming the entire path i.e. /home/user/tmp/uploads but I just get an error message saying the directory doesn't exist.
    u've to make your links like this
    HTML Code:
    <a href="link.php?file=file.txt">file.txt</a>
    so when user clicks this link your script will do something like this:

    PHP Code:

    $tmp_dir 
    '/tmp/uploads/';

    $file_name is_set($_GET['file'])?$_GET['file']:'';

    if(!
    $file_name) {exit;}

    $filename $tmp_dir.$file_name;

        
    header("Cache-control: private");
        
    header("Content-type: application/force-download");
        
    header("Content-Length: ".filesize($filename));
        
    header("Content-Disposition: filename=".$file_name);

        
    readfile($filename);

    .... 
    Free domain tools: WebBoar - sniffing out domain secrets.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Southern Maine
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what you are trying to do is a directory lister right?
    Working on a new CSS site for RKS and PHP driven?

    Looking for staff, tutorial site for coding, and photshop. PM me.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried the suggested solutions here but nothing has worked.


    This is how I made the dir:

    <?

    if (mkdir ("/pdfs/", 0777
    ))
    {
    echo "Successful!";
    }
    else
    {
    echo "Unsuccessful";
    }
    ?>


    This is the upload script

    <?

    // get some information
    echo "Filename: $upfile_name<br>";
    echo "Temporary filename: $upfile<br>";
    echo "File size: $upfile_size bytes<br>";
    echo "File type: $upfile_type<br>";

    // if upload successful
    if ($upfile)
    {
    echo "Upload successful!<br>";
    // copy file to new location
    if (copy("$upfile", "/tmp/pdfs/" . $upfile_name))
    {
    echo "File copy successful!<br>";
    }
    }
    // else display error
    else
    {
    echo "Upload unsuccessful!<br>";
    }
    ?>

    <?

    if ($upfile_type == "text/plain" || $upfile_type == "text/html" ||
    $upfile_type == "image/gif" || $upfile_type == "text/php")
    {
    // file upload code
    }
    else
    {
    echo "Permission denied!";
    }
    ?>

    This is the read directory script:


    <?

    $tmp_dir = ("/tmp/pdfs/");

    // get directory handle
    $hook = opendir("/tmp/pdfs/");

    // read directory and echo list
    while (readdir($hook))
    {
    $file = readdir($hook);

    echo ('<a href="'.$tmp_dir.$file.'">'.$file."</a><br>");

    }

    // close directory
    closedir($hook);
    ?>

    can ayone see what I've done wrong?

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Czech Rep.
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is an error.
    PHP Code:
    echo ('<a href="'.$tmp_dir.$file.'">'.$file."</a><br>"); 
    you browser knows nothing about the '/tmp' directory on your server.

    u've to let some other script to serve file download.

    so u need to do something like this

    PHP Code:
    echo ('<a href=output.php?file="'.$tmp_dir.$file.'">'.$file."</a><br>"); 
    and the output.php will have to do the following actions [check for errors plz]:

    [output.php]
    PHP Code:
    <?

    $file 
    $_GET['file'];

    $ar = array();

    $ar file($file);

    $content join("\n",$ar);

    print 
    $content;

    ?>
    this has to work with all textual files..
    Free domain tools: WebBoar - sniffing out domain secrets.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •