473,508 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Upload files and provide the url - header error

5 New Member
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

Expand|Select|Wrap|Line Numbers
  1. <?php #script 10.52 - show_image.php
  2. $name = FALSE;//flag variable:
  3. //check for an image in the URL:
  4. if (isset($_GET['image'])){
  5.     //Full image path:
  6.     $image = "c://xampp/uploads/{$_GET['image']}";
  7.     //check that the image exists and is a a file:
  8.     if(file_exists ($image)&& (is_file($image))){
  9.         //make sure it has and image extension
  10.  
  11.     $ext = strtolower (substr ($_GET['image'], -4));
  12.     if (($ext=='.jpg')OR ($ext=='jpeg')OR( $ext== 'gif') OR ($ext=='pdf') OR ($ext == 'txt')) {
  13.         //Set the name as this image:
  14.         $name = $_GET['image'];
  15.     }//End of $ext IF.
  16.     }//End of file_exists() IF.
  17. }//end of isset ($_GET['image'])IF.
  18.     //if there was a problem, use the default image:
  19.     if (!$name){
  20.         $image = 'images/unvaliable.png';
  21.  
  22.         $name = 'unvailable.png';}
  23.         //Get the image information:
  24.         $info = getimagesize($image);
  25.         $fs = filesize($image);
  26.         //send the content information:
  27.         header("Content-Type: " . $file['mime_type']); 
  28.         print($image.$file['name']);
  29.         //Send the file:
  30.         //readfile ($image);
  31. ?>
  32.  
  33. </body>
  34. </html>
And this one to display the images
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. <!--//hide from old browsers.
  3. //Make a pop up window function:
  4.  
  5. function create_window (image, width, height){
  6.     //add some pixels to the width and height:
  7.     width = width + 10;
  8.     height = height + 10;
  9.     //if the window is already open,
  10.     //resize it to the new dimensions;
  11.     if (window.popup && !window.popup.closed) {
  12.         window.popup.resizeTo(widht, height);
  13.     }//Set the window properties
  14. var specs = "location=no,
  15. scrollbars=no, menubars=no,
  16. toolbars=no, resizable=yes, left=0, top=0, width="+ width +", height="+ height;
  17.  
  18. //Set the URL:
  19. var url = "show_image.php?image="+ image;
  20.  
  21. //create the popup window:
  22. popup = window.open(url,"imagewindow", specs);
  23. popup.focus();
  24. }//end of function
  25. //--></script>
  26. </head>
  27.  
  28. <body>
  29.  
  30. <p>click on and image to view it in a separate window.</p>
  31. <table align="center"
  32.  cellpadding="5" cellspacing="5" border="1">
  33.  <tr>
  34.  <td align="center"><b>image name</b></td>
  35.  <td align="center"><b>image size</b></td>
  36.  </tr>
  37.  
  38.  <?php #script 10.4 - images.php
  39.  // this script lists the images in the upload directory
  40.  $dir = 'c://xampp/uploads';//define the directory to view
  41.  
  42.  $files = scandir($dir);//Read all the images into an array.
  43.  
  44.  //display each image caption as a link to the javascript function:
  45.  foreach ($files as $image){
  46.      if(substr($image,0 ,1)!='.'){//ignore anything starting with a period
  47.                //get the image's size in pixels
  48.                $image_size = getimagesize("$dir/$image");
  49.                //calculate the image's size in kilobytes:
  50.                $file_size = round (( filesize ("$dir/$image"))/1024). "kb";
  51.                //make the image's name url -safe:
  52.                $image= urlencode($image);
  53.                //print the information:
  54.                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";
  55.                }//End of the IF.
  56.  
  57.  }//End of foreach loop
  58.  ?>
  59.  </table>
  60.  
  61.  
  62.  
  63. </body>
  64. </html>
Any help would be great

Thanks
Jan 2 '10 #1
2 2662
Atli
5,058 Recognized Expert Expert
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.
Jan 2 '10 #2
kelvinwebdesigner
5 New Member
Hi thanks for the help! I solved the problem
Jan 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2722
by: Bob Bedford | last post by:
I've a page where the user may enter some values and upload some pictures. Unfortunately, some pictures coming from digital camera are very big, and take a lot of time for uploading. I've...
5
5434
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
9
20877
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
2
2139
by: Event Horizon | last post by:
Hi, I'm trying to add an simple upload applet to shopping cart script. My new applet form sends all needed post fields ( quantity, product, etc... ) but the "file" post field is hardcoded in...
21
34327
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
5
3263
by: camphor | last post by:
hi, I have found an upload script in hotscripts and have implemented it into the website, I followed the installation steps to 'give write permissions to php on the upload folder (which is...
5
1876
by: petershaman | last post by:
Hi I have a gorgeous flash image gallery, it would work perfectly fine but there's a small problem I can't upload anything thru it's upload script, since I'm getting an error 403. It's so because...
4
4854
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
1
4860
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.