filesize() errors...help

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
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

filesize() errors...help

Post by tristanlee85 »

Code: Select all

<?php 
        $uploaddir = $_SERVER['DOCUMENT_ROOT'] . "\\saturnspot";  //replace the $uploaddir to wherever you want the files to go 
        $filename = $_FILES['file']['name'];  // get the filename 
        $filetype = $_FILES['file']['type'];  // get the file MIME type 
       	$uploadfile = $uploaddir . $filename;  //this is full path to where the file will go 
        ?> 
<HTML>
<HEAD>
<TITLE> SaturnSpot Image Hosting </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>

<BODY bgcolor="#000000" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<div align="center"><img src="logo.gif" width="277" height="105"> </div>
<table width="90%" border="0" align="center">
  <tr> 
    <td> <p><font color="#FFFFFF"><? if (empty($filename)) {  //make sure the user selected a file 
            echo "Please select a file to upload!"; 
	}
        		if ($filetype == "image/jpeg" || $filetype == "image/pjpeg" || $filetype == "image/jpg" || $filetype == "image/gif")  {  //make sure file is .jpg or .gif 
          if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { // move the file 
          echo "Thank you. The files was successfully uploaded.<br><br> To link to this files, please copy the following URL: <b>http://24.197.74.135/saturnspot/$filename</b><br><br>Make sure to copy the URL <b>exactly</b> how it is printed above (filenames are cAsE sensitive)."; 
          } 
          else { 
          echo "File upload failed"; 
          } 
        } 
        else  { 
        echo " Remember to upload only image files."; 
        } 
?></font></p>
        <p><font color="#FFFFFF">To upload images, simply click "Browse" 
        to search for files locally on your computer. You may only upload 1 file 
        at a time. Once the file has been successfully uploaded, you will see 
        a image URL that you can use to directly link to you image. If you have 
        any questions or comments, please e-mail them to <a href="mailto:97saturnsohc@charter.net">97saturnsohc@charter.net</a>.</font></p></td>
    <td><form action="upload.php" method="post" ENCTYPE="multipart/form-data"> 
        <p align="center"> 
          <input type="file" size=40 name="file">
          <br>
          <input type="hidden" name="MAX_FILE_SIZE" value="250000">
          <input type="submit" value="Upload image...">
        </p>
        </form></td>
  </tr>
</table>
 

</BODY>
</HTML>
?>
Ok. Here is my problem. When users upload a GIF/JPEG file, PHP will echo this:
-----
Thank you. The files was successfully uploaded.

To link to this files, please copy the following URL: http://24.197.74.135/saturnspot/logo.gif

Make sure to copy the URL exactly how it is printed above (filenames are cAsE sensitive).

To upload images, simply click "Browse" to search for files locally on your computer. You may only upload 1 file at a time. Once the file has been successfully uploaded, you will see a image URL that you can use to directly link to you image. If you have any questions or comments, please e-mail them to 97saturnsohc@charter.net.
-----

This is exctly how I want it. No need to change that. Now, here is the problem. If the person doesn't select a file to upload, I want this to appear:
-----
Please select a file to upload!

To upload images, simply click "Browse" to search for files locally on your computer. You may only upload 1 file at a time. Once the file has been successfully uploaded, you will see a image URL that you can use to directly link to you image. If you have any questions or comments, please e-mail them to 97saturnsohc@charter.net.
-----

Instead, I get this:
-----
Please select a file to upload! Remember to upload only image files.

To upload images, simply click "Browse" to search for files locally on your computer. You may only upload 1 file at a time. Once the file has been successfully uploaded, you will see a image URL that you can use to directly link to you image. If you have any questions or comments, please e-mail them to 97saturnsohc@charter.net.
-----

Can someone direct my how how to only allow "Please select a file to upload!" instead of "Please select a file to upload! Remember to upload only image files." without removing the line:

Code: Select all

<?php
else  { 
        echo " Remember to upload only image files."; 
        } 

?>
Last edited by tristanlee85 on Thu Dec 25, 2003 11:19 pm, edited 1 time in total.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

change this:

Code: Select all

else  { 
  echo " Remember to upload only image files."; 
}
to this:

Code: Select all

} 
else if(empty($filename)) {
  echo "Please Select a file to upload!";
}
else  { 
  echo " Remember to upload only image files."; 
}
and don't forget to delete this from the beggining of the PHP:

Code: Select all

if (empty($filename)) {  //make sure the user selected a file 
            echo "Please select a file to upload!"; 
   }
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Ok. Thank you. That worked. Now, I have a MAX_FILE_SIZE vaule in my HTML form of 250000 (250 KB). How do I use that in PHP so it actually checks the files size and will give me an error?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

You check the file size with: http://sg.php.net/manual/en/function.filesize.php

Hope that helps,

-Nay
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Ok. That helped me out a good bit now. Now, this is what I'm trying to do:

Code: Select all

<? 
			
if (filesize($filename) > $size) {
echo "The file <b>$filename</b> is (number_format((150000 - $file_size ))) bytes too big. Please make sure the images is under 150 KB.";
}
?>
How do I get it to do subtraction and division w/o it showing up as text?

If I go like it is now, I'll get this:

Code: Select all

The file bullets.gif is (number_format((150000 - 1114 ))) bytes too big. Please make sure the images is under 150 KB.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

:)

PHP Recognizes that as a string. You'll need to do this:

Code: Select all

"The file <b>$filename</b> is " . (number_format((150000 - $file_size ))) . "bytes too big. Please make sure the images is under 150 KB.";
-Nay
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Ok. Well, here is my new code. Everything seems to be working fine, except for 1 thing:

Code: Select all

<?php 
        $uploaddir = $_SERVER['DOCUMENT_ROOT'] . "\\saturnspot";  //replace the $uploaddir to wherever you want the files to go 
        $filename = $_FILES['file']['name'];  // get the filename 
        $filetype = $_FILES['file']['type'];  // get the file MIME type 
       	$uploadfile = $uploaddir . $filename;  //this is full path to where the file will go
		$size = 150*1024; //What do you want size limited to be if there is one 
		$file_size = filesize($filename)
        ?> 
<HTML>
<HEAD>
<TITLE> SaturnSpot Image Hosting </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>

<BODY bgcolor="#000000" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" text="#FFFFFF">
<div align="center"><img src="logo.gif" width="277" height="105"> </div>
<table width="90%" border="0" align="center">
  <tr> 
    <td> <p><font color="#FFFFFF"><? 
			
			if (filesize($filename) > $size) {
			echo "The file <b>$filename</b> is " . (number_format(($file_size - $size)/1024)) . " kylobytes too big. Please make sure the images is under " . (number_format(($size)/1024)) . "KB.";
			}
			else if ($filetype == "image/jpeg" || $filetype == "image/pjpeg" || $filetype == "image/jpg" || $filetype == "image/gif" || $filetype == "image/bmp")  {  //make sure file is .jpg or .gif 
          if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { // move the file 
          echo "Thank you. The files was successfully uploaded.<br><br> To link to this files, please copy the following URL: <b>http://24.197.74.135/saturnspot/$filename</b><br><br>Make sure to copy the URL <b>exactly</b> how it is printed above (filenames are cAsE sensitive)."; 
          } 
          else { 
          echo "File upload failed"; 
          } 
        } 
			
		else if(empty($filename)) { 
		 echo "Please Select a file to upload!"; 
		} 
		else  { 
		echo " Remember to upload only image files. Your filetype was <b>$filetype</b>. "; 
		}  
?></font></p>
        <p><font color="#FFFFFF">To upload images, simply click "Browse" 
        to search for files locally on your computer. You may only upload 1 file 
        at a time. Once the file has been successfully uploaded, you will see 
        a image URL that you can use to directly link to you image. If you have 
        any questions or comments, please e-mail them to <a href="mailto:97saturnsohc@charter.net">97saturnsohc@charter.net</a>.</font></p></td>
    <td><form action="upload.php" method="post" ENCTYPE="multipart/form-data"> 
        <p align="center"> 
          <input type="file" size=40 name="file">
          <br>
          <input type="submit" value="Upload image...">
        </p>
        </form></td>
  </tr>
</table>
 
<p align="center">[ <a href="http://www.saturnspot.com">Back To SaturnSpot</a> ]</p>
</BODY>
</HTML>

?>
Now, when I upload JPEG or GIF files, it works fine, but when I upload files from my digital camera flash card, they are read at image/pjpeg files. So, when I try to upload a PJPEG file, I get this:

Code: Select all

Warning: filesize(): Stat failed for halo.JPG (errno=2 - No such file or directory) in c:\saturnspot\upload.php on line 7

 

Warning: filesize(): Stat failed for halo.JPG (errno=2 - No such file or directory) in c:\saturnspot\upload.php on line 24
Thank you. The files was successfully uploaded.

To link to this files, please copy the following URL: http://24.197.74.135/saturnspot/halo.JPG
What is that code and why am I getting it?
Post Reply