Open and view image files (in a webbrowser)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Emma1
Forum Newbie
Posts: 1
Joined: Sun Dec 06, 2009 8:29 am

Open and view image files (in a webbrowser)

Post by Emma1 »

Hi,

I have an html document which allows the user to brows and upload a file. When the user press the button "View file" then the php document is called. And its my php document that I have problems with.

The file is stored. The info about the file is printed (in the webbrowser) so the user see for example size and mime-type of the file. The content of the file is also supposed to be viewed and it works for .txt files, but not for any image files. The info about the image files are printed, but not the image itself.

In all tutorials the show how to open files with fopen(). But its only about .txt files, never anything about image files. At least I havn't found it (or understood that it is useful). Is there anyone that knows how to do? I attach the part of the code that I have problems with.

(The content in the header of the php document is multipart/form-data)

Code: Select all

 
 $fileSize = $_FILES['uploadedfile']['size'];
    $fileName = $_FILES['uploadedfile']['name'];
    $contentType = $_FILES['uploadedfile']['type'];
    
    echo "Filstorlek: ".$fileSize."<br/>";      //File size is printed. Works for all types of files.
    echo "Filnamn: ".$fileName."<br/>";        //File name is printed. Works for all types of files.
    echo "Mimetyp: ".$contentType."<br/>";    //The mime-type of the file is printed. Works for all types of files.
    
    header("Content-Type: " .$contentType);    //Changes the mime-type to the uploaded doc's mime? Dont think    this works. 
 
       $file = fopen("uploads/$fileName", 'r'); //Works for .txt files
    
    while(!feof($file)){                  //This works for .txt files.
      $strLine = fgets($file);
        echo "$strLine<br/>";
    }
    
    echo "<p>";
    
    if(!fclose($file))
       echo "<p>Error closing file!</p>";
 
 
Post Reply