Jump to content

Script *sometimes* shows html in browser without rendering


mobiius

Recommended Posts

Hello all, I'm new to PHP and to these forums and i'm having trouble with a little script I'm working on.

 

I have to point out at the start that the script actually works, other than for the error I'm reporting.

 

What it does, is lets the user upload an image to the server and creates a thumbnail imageof it in another folder. This works as intended. (I.e. the images gets uploaded and the thumbnail is created) The issue i'm having is when the work is completed successfully...

 

If there's an error, I update a variable $msg with an error mesage. (File too large, or invalid filetype) If this is the case, the script will render the html code correctly, display the error, then redirect you using javascript after 4 seconds to the original page.

 

No worries here, everything works so far.

 

However, if there is no errors to report and the script is successfull, I update $msg to say "it worked"(or whatever) but instead of displaying the formatted webpage displaying the message, then redirecting after 4 seconds, all that happens is the html code is displayed instead.

 

(If I cut and paste this code into a new htmlfile, it renders correctly.)

 

Attached below is the php code which does all the work. (But not the main php file which calls it)

 

Can someone please point me in the right direction as to what I'm doing wrong! lol

 

(Again, I'm new here so a big hello from me!!!)

 

<?php
   print( "<html>\n");
   print( "   <head>\n");
   print( "      <title>Random banner and file upload</title>\n");
   print( "      <link rel=StyleSheet href=\"styles.css\" type=\"text/css\">\n");
   print( "   </head>\n");
   print( "   <body>\n" );
   
   $file_upload = "true";
   $filetype = $_FILES[ "file_up" ][ "type" ];
   $file_name = $_FILES[ "file_up" ][ "name" ];
   $startimage = $_FILES[ "file_up" ][ "tmp_name" ];
   $largeimage = "images/uploads/$file_name";
   $thumbimage = "images/uploadthumbs/thumb-$file_name";
   $msg = "      ";
   $invalidfilesize = "<p class='centeralign'>Your uploaded file size is more than 250KB so please reduce the file size and then upload.<br>Visit the help page to know how to reduce the file size.</p>\n";
   $invalidfiletype = "<p class='centeralign'>Your uploaded file must be of PNG, JPG or GIF. Other file types are not allowed.</p>\n";
   $successfullupload = "<p class='centeralign'>File uploaded successfully! </p>\n";   
   $failedupload = "<p class='centeralign'>File not uploaded successfully! </p>\n";   
   $notinstalled = "<p class='centeralign'>The requested function is not installed on this web server. </p>\n";
     
   //If the image is larger than 1Mb, do not upload.
   if ( $_FILES[ "file_up" ][ "size" ] > 1048576 )
   {
      $msg = $msg.$invalidfilesize;
      $file_upload="false";
   }
   
   //If the image is an invalid file type, do not upload.
   if ( ! ( $filetype == "image/x-png" || $filetype == "image/pjpeg"  || $filetype == "image/jpeg" || $filetype == "image/gif" ) )
   {
      $msg = $msg.$invalidfiletype;
      $file_upload = "false";
   }

   //Start the thumbnail generation.
   if( $file_upload == "true" )
   {
      $n_width = 160; // Fix the width of the thumbnail images
      $n_height = 120; // Fix the height of the thumbnail imaage
      
      switch ( $filetype )
      {
     case "image/x-png";
        $FromFormat = "ImageCreateFromPNG";
	    $ToFormat = "ImagePNG";
		break;
	 case "image/jpeg";
        $FromFormat = "ImageCreateFromJPEG";
	    $ToFormat = "ImageJPEG";
		break;
	 case "image/pjpeg";
        $FromFormat = "ImageCreateFromJPEG";
	    $ToFormat = "ImageJPEG";
		break;
         case "image/gif";
        $FromFormat = "ImageCreateFromGIF";
	    $ToFormat = "ImageGIF";
		break;
      }
  
      if ( function_exists( "$FromFormat" ) )// Generate the thumbnail.
      {            
         $im = $FromFormat( $startimage );
         $width = ImageSx( $im ); // Original picture width is stored
         $height = ImageSy( $im ); // Original picture height is stored
         $newimage = imagecreatetruecolor( $n_width, $n_height );
         imageCopyResized( $newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height );
         Header( "Content-type: $filetype" );
         $ToFormat( $newimage, $thumbimage );
      }
  else { $msg = $msg.$notinstalled; }// Unable to generate the thumbnail. (GD library not installed on web server?)

      //Move the uploaded file to the correct place..
      if( move_uploaded_file ( $startimage, $largeimage ) ) { $msg = $msg.$successfullupload; }
      else { $msg = $msg.$failedupload; }//Unable to move uploaded file.
   }
   Print( "$msg" );   
   print( "      <script type=\"text/javascript\"><!--\n" );
   print( "         setTimeout('Redirect()',4000);\n" );
   print( "         function Redirect()\n" );
   print( "         {\n" );
   print( "            location.href = 'index.php';\n" );
   print( "         }// -->\n" );
   print( "      </script>\n" );   
   print( "   </body>\n" );
   print( "</html>\n" );
?>

Link to comment
Share on other sites

I didn't really read through your code, but chances are if the raw HTML is being displayed instead of the page, there is a header problem.  Are you setting the content-type of the page to an image by accident?

 

Edit: Welcome to PHPFreaks  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.