CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Write uploaded file directly into a file opened by fopen [PHP 4]

    Hi. I have a problem where I have to be able to write an uploaded file directly into a file opened by fopen (Or more specifically, tmpfile). The only way I have found for saving an uploaded file, is move_uploaded_file, but that won't let you write through a file handle.

    Is there some other way of making this happen?
    Insert entertaining phrase here

  2. #2
    Join Date
    Aug 2002
    Posts
    879

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    I have to be able to write an uploaded file directly into a file opened by fopen
    I didn't get what you want to do in particular.

    http://de3.php.net/manual/en/functio...oaded-file.php
    have you checked out the comment of andrew@euperia,com?
    or try to use copy
    http://de3.php.net/manual/en/function.copy.php

  3. #3
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    Sorry about that. Early start this morning.

    I want to write an uploaded file directly into a file created through the tmpfile() function. This function only returns a file-handle, and you can't use move_uploaded_file to write into a file-handle (It only takes a filename parameter).

    Is there any other way of accomplishing this, or do I have to resort to my 1337 h4x0ring skillz?
    Insert entertaining phrase here

  4. #4
    Join Date
    Aug 2002
    Posts
    879

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    If the uploaded files are textfiles you can use something like this
    PHP Code:
    <?
     $file_stream=$_FILES['userfile']['tmp_name'];
     $fp = tmpfile();
     fwrite($fp,file_get_contents($file_stream));
     rewind($fp);
     echo fread($fp, 1024);
     ?>
    Is there a special reason why you want to save the uploaded file
    in a tmpfile() ?

  5. #5
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    Yep, there is actually. The files being uploaded are zip files, and I simply want the contents of them, I don't need the zip itself. Therefore I need to temporarily store the ZIP until I have extracted it. Would that work with the code you posted?

    One idea I just though of was to simply call fopen on $_FILES['userfile']['tmp_name'] directly. Would that work, or is there something preventing access to that file?
    Insert entertaining phrase here

  6. #6
    Join Date
    Aug 2002
    Posts
    879

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    for unzipping I'm using this free lib
    http://www.phpconcept.net/pclzip/index.en.php
    $file_stream is the file selected by the user.

    e.g.
    PHP Code:
    <?
     require_once('pclzip.lib.php');
     $file_stream=$_FILES['userfile']['tmp_name'];

     $archive = new PclZip($file_stream);
     if ($archive->extract() == 0) {
        die("Error : ".$archive->errorInfo(true));
      }
     ?>

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Write uploaded file directly into a file opened by fopen [PHP 4]

    Thanks, I'll take a look at it.
    Insert entertaining phrase here

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured