SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File Paths not working

    Hi,

    I'm trying to open a file stored on my local folder using the code I found on a forum:

    Code:
    <?php 
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-type: application/jpg; charset=UTF-8");
    $filename="C:\Documents and Settings\blah\My Documents\extra.jpg";
    //$filename="extra.jpg";
    $bytes = filesize("$filename");
    header("Content-disposition: attachment; filename=\"$filename\"; charset=UTF-8");
    header("Content-length: $bytes");
    @readfile("$filename");
    //Code to record the download in database
    ?>
    The code works perfectly when the file is stored in the same folder as the code but I get these errors if its stored anywhere else:

    <br />
    <b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: Unable to access C:\Documents and Settings\blah\My Documents\extra.jpgin <b>/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php</b> on line <b>8</b><br />
    <br />
    <b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for >/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php in <b>/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php</b> on line <b>8</b><br />
    <br />
    <b>Warning</b>: Cannot modify header information - headers already sent by (output started at >/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php:8) in <b>/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php</b> on line <b>9</b><br />
    <br />
    <b>Warning</b>: Cannot modify header information - headers already sent by (output started at >/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php/openpic.php:8) in <b>/---/blah/blah/blah/phpl/bhoo/xxxx/xxxx/openpic.php</b> on line <b>10</b><br />
    BTW, Line 8 and 9 re these two respectively:
    Code:
    $bytes = filesize("$filename");
    header("Content-disposition: attachment; filename=\"$filename\";

    Thanks for any help you can give.

  2. #2
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put headers before any html output.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This can be one of two scenarios.

    The first is that you're trying to access an image from your computer from within PHP on a live server. This cannot be done because the server has no access to the user's computer; Can you imagine the security problems you'd have if you could?

    The second is that you're trying to get an image that you could get when it was on your localhost. You need to upload the image to the server and change the path to the one on the live server.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BBoardX View Post
    Put headers before any html output.
    Look at his code

    The header issue is because an error was output by the PHP parser.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait, instead of using that direct path why not just do ../ for moving to different directories.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    This can be one of two scenarios.
    You need to upload the image to the server and change the path to the one on the live server.
    If this is the case then the program should work if the image is saved on the server in any folder and not just the folder where my code is right?
    But, it dosent seem to be working.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BBoardX View Post
    Wait, instead of using that direct path why not just do ../ for moving to different directories.
    Tried it too...didn't help

  8. #8
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try replacing

    Code:
    $filename="C:\Documents and Settings\blah\My Documents\extra.jpg";
    //$filename="extra.jpg";
    with

    Code:
    $filename="extra.jpg";
    Assuming the file is in the same path as your code

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If this is the case then the program should work if the image is saved on the server in any folder and not just the folder where my code is right?
    But, it dosent seem to be working.
    It looks like your server is Linux (judging by the path to your directory).

    If so, I can tell you that C:\Documents and Settings\blah\My Documents\extra.jpg Most definitely isn't a path on your server.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have apache installed on your system then it should be placed in your local host folder

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey everyone thanks for all the inputs.
    After trying a little bit of this and that and doing some searching based on all your suggestions, I learnt that you cant open client side files using PHP and for good reasons (arkinstall was right).
    But, I found a way do this in JavaScript and since this is an internal website with only a handful of users security isnt an issue.

    Thanks for all your help.

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dpn View Post
    But, I found a way do this in JavaScript and since this is an internal website with only a handful of users security isnt an issue
    I wouldn't guarantee that any javascript method will work with all browsers, or even different versions of the same browser.

    Watch out as and when any of your users upgrade their IE
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not just put the image on the server?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    I wouldn't guarantee that any javascript method will work with all browsers, or even different versions of the same browser.

    Watch out as and when any of your users upgrade their IE
    Yes...I know. Are there any other ways of doing this?

    Why not just put the image on the server?
    I can't put the image files on the server because I dont upload them. The users do and they can upload it any where they want. I have a form where they can enter the file path into the database and I retreive it later and enable them to open it from the browser.
    I hope that makes sense.

  15. #15
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hopefully IE will stop using an ActiveXObject and start supporting the XMLHttpRequest object

  16. #16
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BBoardX View Post
    Hopefully IE will stop using an ActiveXObject and start supporting the XMLHttpRequest object
    IE 7.0 already does.

  17. #17
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But alot of people still use IE6

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But IE already stopped using ActiveX for XMLHttpRequest.

  19. #19
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE 6 still uses activexobject..

  20. #20
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but you were hoping IE would stop using ActiveX for XMLHTTPRequest. And it did, starting IE 7.0.

    People might still be using 6.0, but IE already moved on.

  21. #21
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know but the point I am making is that around 1/4 of the market still uses ie6.

  22. #22
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And it's never, ever going to be updated - that's a problem we just have to accept.

    dpn, why not just have an image upload system rather than a direct file link?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  23. #23
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for not replying people...got hit by the flu..

    Anyway,Most of you will be happy to know I didn't use ActiveX after all.

    I don't know how this works exactly but it did:

    Code:
    <input type=button name='link' Value='Link' onClick=window.open('file:$lnk_data');>
    Where $lnk_data stores the path name retreived from the database. The picture is opened in a browser window which is fine for what I'm trying to do. "file:" did the magic!!

    It would be great if anyone could explain what happened here..

    Thanks

  24. #24
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    dpn, why not just have an image upload system rather than a direct file link?
    Arkinstall, Wouldn't I still have the same problem? I mean I'll still have to have access to the files on the client's folders right?


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
  •