Upload files and provide the url - header error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kelvinwebdesigner
    New Member
    • Jan 2010
    • 5

    Upload files and provide the url - header error

    Hi everybody.
    Im currently developing an application where users can upload files and later get acess to the url.
    Im now having this trouble with header Cannot modify header information – headers already sent
    This is my code so far

    Code:
    <?php #script 10.52 - show_image.php
    $name = FALSE;//flag variable:
    //check for an image in the URL:
    if (isset($_GET['image'])){
    	//Full image path:
    	$image = "c://xampp/uploads/{$_GET['image']}";
    	//check that the image exists and is a a file:
    	if(file_exists ($image)&& (is_file($image))){
    		//make sure it has and image extension
    	
    	$ext = strtolower (substr ($_GET['image'], -4));
    	if (($ext=='.jpg')OR ($ext=='jpeg')OR( $ext== 'gif') OR ($ext=='pdf') OR ($ext == 'txt')) {
    		//Set the name as this image:
    		$name = $_GET['image'];
    	}//End of $ext IF.
    	}//End of file_exists() IF.
    }//end of isset ($_GET['image'])IF.
    	//if there was a problem, use the default image:
    	if (!$name){
    		$image = 'images/unvaliable.png';
    		
    		$name = 'unvailable.png';}
    		//Get the image information:
    		$info = getimagesize($image);
    		$fs = filesize($image);
    		//send the content information:
    		header("Content-Type: " . $file['mime_type']); 
    		print($image.$file['name']);
    		//Send the file:
    		//readfile ($image);
    ?>
    
    </body>
    </html>
    And this one to display the images
    Code:
    <script language="javascript">
    <!--//hide from old browsers.
    //Make a pop up window function:
    
    function create_window (image, width, height){
    	//add some pixels to the width and height:
    	width = width + 10;
    	height = height + 10;
    	//if the window is already open,
    	//resize it to the new dimensions;
    	if (window.popup && !window.popup.closed) {
    		window.popup.resizeTo(widht, height);
    	}//Set the window properties
    var specs = "location=no,
    scrollbars=no, menubars=no,
    toolbars=no, resizable=yes, left=0, top=0, width="+ width +", height="+ height;
    
    //Set the URL:
    var url = "show_image.php?image="+ image;
    
    //create the popup window:
    popup = window.open(url,"imagewindow", specs);
    popup.focus();
    }//end of function
    //--></script>
    </head>
    
    <body>
    
    <p>click on and image to view it in a separate window.</p>
    <table align="center"
     cellpadding="5" cellspacing="5" border="1">
     <tr>
     <td align="center"><b>image name</b></td>
     <td align="center"><b>image size</b></td>
     </tr>
     
     <?php #script 10.4 - images.php
     // this script lists the images in the upload directory
     $dir = 'c://xampp/uploads';//define the directory to view
     
     $files = scandir($dir);//Read all the images into an array.
     
     //display each image caption as a link to the javascript function:
     foreach ($files as $image){
    	 if(substr($image,0 ,1)!='.'){//ignore anything starting with a period
    			   //get the image's size in pixels
    			   $image_size = getimagesize("$dir/$image");
    			   //calculate the image's size in kilobytes:
    			   $file_size = round (( filesize ("$dir/$image"))/1024). "kb";
    			   //make the image's name url -safe:
    			   $image= urlencode($image);
    			   //print the information:
    			   echo "\t<tr>\t\t<td><a href=\"show_image.php?image=$image\">$image</a></td>\t\t<td>$file_size</td>\t</tr>\n";
    			   }//End of the IF.
    			   
     }//End of foreach loop
     ?>
     </table>
    
    
    
    </body>
    </html>
    Any help would be great

    Thanks
    Last edited by Atli; Jan 2 '10, 11:14 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    There are a few problems there you need to look into.
    • Line #27. You use a variable $file, which I don't see you create anywhere. What is that supposed to be?
    • You say you get a "headers already sent" error. What is printed before that error? - There must be something, or you would not be getting the error. - It's best to post all the errors, and whole error messages, not just the highlights. (All that info is printed for a reason :])
    • Your show_image.php code is trailed by HTML tags, which it should not. - The file represents and image, and adding HTML tags to the end of an image may corrupt it, causing browsers not to display it properly.

    Comment

    • kelvinwebdesigner
      New Member
      • Jan 2010
      • 5

      #3
      Hi thanks for the help! I solved the problem

      Comment

      Working...